diff --git a/AbstractTest.java b/AbstractTest.java new file mode 100644 index 0000000000000000000000000000000000000000..967b8aa45eb52602efe3642538943208da955e3a --- /dev/null +++ b/AbstractTest.java @@ -0,0 +1,43 @@ +package com.javacodegeeks.nio.large_file_transfer; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; + +public abstract class AbstractTest { + + protected static final String SRC = "/tmp/input.tar.gz"; + protected static final String TARGET = "/tmp/output"; + protected static final long AWAIT_TEST_COMPLETE = 20000l; + + protected File srcFile; + protected File targetFile; + + @BeforeClass + public static void init() { + Thread.currentThread().getContextClassLoader().setDefaultAssertionStatus(true); + } + + @AfterClass + public static void destroy() { + Thread.currentThread().getContextClassLoader().setDefaultAssertionStatus(false); + } + + @Before + public void setUp() throws IOException { + this.srcFile = new File(SRC); + this.targetFile = new File(TARGET + "/" + this.srcFile.getName()); + + Files.deleteIfExists(this.targetFile.toPath()); + } + + protected final void compare() { + assertEquals("file did not copy completely", this.srcFile.length(), this.targetFile.length()); + } +} diff --git a/FileCopyTest.java b/FileCopyTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d7248a9581eb9d7a8ca4a4bfd659e6ab656be057 --- /dev/null +++ b/FileCopyTest.java @@ -0,0 +1,37 @@ +package com.javacodegeeks.nio.large_file_transfer.remote; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +import org.junit.Test; + +import com.javacodegeeks.nio.large_file_transfer.AbstractTest; + +public class FileCopyTest extends AbstractTest { + + private static final int PORT = 9999; + + @Test + public void copyLargeFile() throws IOException, InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + final FileReceiver receiver = new FileReceiver(PORT, new FileWriter(TARGET + "/" + super.srcFile.getName()), super.srcFile.length()); + + new Thread() { + public void run() { + try { + receiver.receive(); + } catch (IOException e) { + + } finally { + latch.countDown(); + } + } + }.start(); + + final FileReader reader = new FileReader(new FileSender(PORT), SRC); + reader.read(); + + latch.await(); + compare(); + } +}