Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests and slightly updated code for convenience #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/main/java/com/turn/ttorrent/common/Torrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

import jargs.gnu.CmdLineParser;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
Expand Down Expand Up @@ -408,15 +409,7 @@ public boolean isSeeder() {
* @throws IOException If an I/O error occurs while writing the file.
*/
public void save(File file) throws IOException {
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
fOut.write(this.getEncoded());
} finally {
if (fOut != null){
fOut.close();
}
}
FileUtils.writeByteArrayToFile(file, this.getEncoded());
}

public static byte[] hash(byte[] data) throws NoSuchAlgorithmException {
Expand Down
56 changes: 0 additions & 56 deletions src/test/java/com/turn/ttorrent/FileUtil.java

This file was deleted.

23 changes: 8 additions & 15 deletions src/test/java/com/turn/ttorrent/TempFiles.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.turn.ttorrent;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.*;

public class TempFiles {
private static final File ourCurrentTempDir = FileUtil.getTempDirectory();
private static final File ourCurrentTempDir = FileUtils.getTempDirectory();
private final File myCurrentTempDir;

private static Random ourRandom;

static {
ourRandom = new Random();
ourRandom.setSeed(System.currentTimeMillis());
}
private static Random ourRandom = new Random();

private final List<File> myFilesToDelete = new ArrayList<File>();
private final Thread myShutdownHook;
Expand All @@ -23,7 +20,7 @@ public TempFiles() {
if (!myCurrentTempDir.isDirectory()) {

throw new IllegalStateException("Temp directory is not a directory, was deleted by some process: " + myCurrentTempDir.getAbsolutePath() +
"\njava.io.tmpdir: " + FileUtil.getTempDirectory());
"\njava.io.tmpdir: " + FileUtils.getTempDirectory());
}

myShutdownHook = new Thread(new Runnable() {
Expand Down Expand Up @@ -55,12 +52,6 @@ private File doCreateTempFile(String prefix, String suffix) throws IOException {
return file;
}

public final File createTempFile(String content) throws IOException {
File tempFile = createTempFile();
FileUtil.writeFile(tempFile, content);
return tempFile;
}

public final File createTempFile() throws IOException {
File tempFile = doCreateTempFile("test", null);
registerAsTempFile(tempFile);
Expand Down Expand Up @@ -123,10 +114,12 @@ public File getCurrentTempDir() {
public void cleanup() {
try {
for (File file : myFilesToDelete) {
FileUtil.delete(file);
FileUtils.forceDelete(file);
}

myFilesToDelete.clear();
} catch (IOException e) {

} finally {
if (!myInsideShutdownHook) {
Runtime.getRuntime().removeShutdownHook(myShutdownHook);
Expand Down
17 changes: 3 additions & 14 deletions src/test/java/com/turn/ttorrent/WaitFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,15 @@ protected WaitFor() {
}

protected WaitFor(long timeout) {
long started = System.currentTimeMillis();
long maxWaitMoment = System.currentTimeMillis() + timeout;
try {
while(true) {
if (condition()) return;
if (System.currentTimeMillis() - started < timeout) {
Thread.sleep(myPollInterval);
} else {
break;
}
while(!condition() && System.currentTimeMillis() < maxWaitMoment){
Thread.sleep(myPollInterval);
}

} catch (InterruptedException e) {
//NOP
}
}

protected WaitFor(long timeout, long pollInterval) {
this(timeout);
myPollInterval = pollInterval;
}

protected abstract boolean condition();
}