tracker
Change-Id: I8f8ac81f9c4d7c7650cd64d2dade701dc6c11dce
diff --git a/ttorrent-master/test-api/pom.xml b/ttorrent-master/test-api/pom.xml
new file mode 100644
index 0000000..b407722
--- /dev/null
+++ b/ttorrent-master/test-api/pom.xml
@@ -0,0 +1,26 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>com.turn</groupId>
+ <artifactId>ttorrent</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <name>ttorrent/test-api</name>
+ <url>http://turn.github.com/ttorrent/</url>
+ <artifactId>ttorrent-test-api</artifactId>
+ <version>1.0</version>
+ <packaging>jar</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.turn</groupId>
+ <artifactId>ttorrent-common</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/MockTimeService.java b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/MockTimeService.java
new file mode 100644
index 0000000..80806d7
--- /dev/null
+++ b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/MockTimeService.java
@@ -0,0 +1,17 @@
+package com.turn.ttorrent;
+
+import com.turn.ttorrent.common.TimeService;
+
+public class MockTimeService implements TimeService {
+
+ private volatile long time = 0;
+
+ @Override
+ public long now() {
+ return time;
+ }
+
+ public void setTime(long time) {
+ this.time = time;
+ }
+}
diff --git a/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/TempFiles.java b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/TempFiles.java
new file mode 100644
index 0000000..0ebed92
--- /dev/null
+++ b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/TempFiles.java
@@ -0,0 +1,142 @@
+package com.turn.ttorrent;
+
+import org.apache.commons.io.FileUtils;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * @author Pavel.Sher
+ * Date: 05.03.2008
+ */
+public class TempFiles {
+ private static final File ourCurrentTempDir = FileUtils.getTempDirectory();
+ private final File myCurrentTempDir;
+
+ private static Random ourRandom;
+
+ static {
+ ourRandom = new Random();
+ ourRandom.setSeed(System.currentTimeMillis());
+ }
+
+ private final List<File> myFilesToDelete = new ArrayList<File>();
+ private final Thread myShutdownHook;
+ private volatile boolean myInsideShutdownHook;
+
+ public TempFiles() {
+ myCurrentTempDir = ourCurrentTempDir;
+ if (!myCurrentTempDir.isDirectory()) {
+
+ throw new IllegalStateException("Temp directory is not a directory, was deleted by some process: " + myCurrentTempDir.getAbsolutePath() +
+ "\njava.io.tmpdir: " + FileUtils.getTempDirectory());
+ }
+
+ myShutdownHook = new Thread(new Runnable() {
+ public void run() {
+ myInsideShutdownHook = true;
+ cleanup();
+ }
+ });
+ Runtime.getRuntime().addShutdownHook(myShutdownHook);
+ }
+
+ private File doCreateTempDir(String prefix, String suffix) throws IOException {
+ prefix = prefix == null ? "" : prefix;
+ suffix = suffix == null ? ".tmp" : suffix;
+
+ do {
+ int count = ourRandom.nextInt();
+ final File f = new File(myCurrentTempDir, prefix + count + suffix);
+ if (!f.exists() && f.mkdirs()) {
+ return f.getCanonicalFile();
+ }
+ } while (true);
+
+ }
+
+ private File doCreateTempFile(String prefix, String suffix) throws IOException {
+ final File file = doCreateTempDir(prefix, suffix);
+ file.delete();
+ file.createNewFile();
+ return file;
+ }
+
+ public final File createTempFile() throws IOException {
+ File tempFile = doCreateTempFile("test", null);
+ registerAsTempFile(tempFile);
+ return tempFile;
+ }
+
+ public void registerAsTempFile(final File tempFile) {
+ myFilesToDelete.add(tempFile);
+ }
+
+ public final File createTempFile(int size) throws IOException {
+ File tempFile = createTempFile();
+ int bufLen = Math.min(8 * 1024, size);
+ final Random random = new Random();
+ if (bufLen == 0) return tempFile;
+ final OutputStream fos = new BufferedOutputStream(new FileOutputStream(tempFile));
+ try {
+ byte[] buf = new byte[bufLen];
+
+ int numWritten = 0;
+ for (int i = 0; i < size / buf.length; i++) {
+ random.nextBytes(buf);
+ fos.write(buf);
+ numWritten += buf.length;
+ }
+
+ if (size > numWritten) {
+ random.nextBytes(buf);
+ fos.write(buf, 0, size - numWritten);
+ }
+ } finally {
+ fos.close();
+ }
+
+ return tempFile;
+ }
+
+ /**
+ * Returns a File object for created temp directory.
+ * Also stores the value into this object accessed with {@link #getCurrentTempDir()}
+ *
+ * @return a File object for created temp directory
+ * @throws IOException if directory creation fails.
+ */
+ public final File createTempDir() throws IOException {
+ File f = doCreateTempDir("test", "");
+ registerAsTempFile(f);
+ return f;
+ }
+
+ /**
+ * Returns the current directory used by the test or null if no test is running or no directory is created yet.
+ *
+ * @return see above
+ */
+ public File getCurrentTempDir() {
+ return myCurrentTempDir;
+ }
+
+ public void cleanup() {
+ try {
+ for (File file : myFilesToDelete) {
+ try {
+ FileUtils.forceDelete(file);
+ } catch (IOException e) {
+ }
+ }
+
+ myFilesToDelete.clear();
+ } finally {
+ if (!myInsideShutdownHook) {
+ Runtime.getRuntime().removeShutdownHook(myShutdownHook);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/Utils.java b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/Utils.java
new file mode 100644
index 0000000..a2ce0f1
--- /dev/null
+++ b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/Utils.java
@@ -0,0 +1,13 @@
+package com.turn.ttorrent;
+
+import org.apache.log4j.Level;
+
+public class Utils {
+
+ private final static String LOG_PROPERTY_KEY = "com.turn.ttorrent.logLevel";
+
+ public static Level getLogLevel() {
+ final String levelStr = System.getProperty(LOG_PROPERTY_KEY);
+ return Level.toLevel(levelStr, Level.INFO);
+ }
+}
diff --git a/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/WaitFor.java b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/WaitFor.java
new file mode 100644
index 0000000..d8b939b
--- /dev/null
+++ b/ttorrent-master/test-api/src/main/java/com/turn/ttorrent/WaitFor.java
@@ -0,0 +1,30 @@
+package com.turn.ttorrent;
+
+public abstract class WaitFor {
+ public static final long POLL_INTERVAL = 500;
+
+ private boolean myResult = false;
+
+ protected WaitFor() {
+ this(60 * 1000);
+ }
+
+ protected WaitFor(long timeout) {
+ long maxTime = System.currentTimeMillis() + timeout;
+ try {
+ while (System.currentTimeMillis() < maxTime && !condition()) {
+ Thread.sleep(POLL_INTERVAL);
+ }
+ if (condition()) {
+ myResult = true;
+ }
+ } catch (InterruptedException e) {
+ }
+ }
+
+ public boolean isMyResult() {
+ return myResult;
+ }
+
+ protected abstract boolean condition();
+}