From a773bbc3203c8d59bf3e51ab39c32f3e5f64a978 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Wed, 5 Oct 2011 13:47:38 +0200
Subject: [PATCH] Unit tests for UnixRemovableDriveMonitor.

---
 test/build.xml                                |  1 +
 .../file/UnixRemovableDriveMonitorTest.java   | 80 +++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100644 test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java

diff --git a/test/build.xml b/test/build.xml
index bb35591483..84cbbdfff1 100644
--- a/test/build.xml
+++ b/test/build.xml
@@ -28,6 +28,7 @@
 			<test name='net.sf.briar.plugins.file.LinuxRemovableDriveFinderTest'/>
 			<test name='net.sf.briar.plugins.file.MacRemovableDriveFinderTest'/>
 			<test name='net.sf.briar.plugins.file.RemovableDrivePluginTest'/>
+			<test name='net.sf.briar.plugins.file.UnixRemovableDriveMonitorTest'/>
 			<test name='net.sf.briar.protocol.AckReaderTest'/>
 			<test name='net.sf.briar.protocol.BatchReaderTest'/>
 			<test name='net.sf.briar.protocol.ConsumersTest'/>
diff --git a/test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java b/test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java
new file mode 100644
index 0000000000..b4294b1adc
--- /dev/null
+++ b/test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java
@@ -0,0 +1,80 @@
+package net.sf.briar.plugins.file;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+import net.sf.briar.TestUtils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UnixRemovableDriveMonitorTest extends TestCase {
+
+	private final File testDir = TestUtils.getTestDirectory();
+
+	@Before
+	public void setUp() {
+		testDir.mkdirs();
+	}
+
+	@Test
+	public void testNonexistentDir() throws Exception {
+		File doesNotExist = new File(testDir, "doesNotExist");
+		RemovableDriveMonitor monitor = createMonitor(doesNotExist);
+		monitor.start();
+		monitor.stop();
+	}
+
+	@Test
+	public void testOneCallbackPerFile() throws Exception {
+		final CountDownLatch latch = new CountDownLatch(1);
+		final List<File> detected = new ArrayList<File>();
+		// Create a monitor that will wait for two files before stopping
+		new Thread() {
+			@Override
+			public void run() {
+				try {
+					RemovableDriveMonitor monitor = createMonitor(testDir);
+					monitor.start();
+					detected.add(monitor.waitForInsertion());
+					detected.add(monitor.waitForInsertion());
+					monitor.stop();
+					latch.countDown();
+				} catch(IOException e) {
+					fail();
+				}
+			}
+		}.start();
+		// Create two files in the test directory
+		File file1 = new File(testDir, "1");
+		File file2 = new File(testDir, "2");
+		assertTrue(file1.createNewFile());
+		assertTrue(file2.createNewFile());
+		// Wait for the monitor to detect the files
+		latch.await(1, TimeUnit.SECONDS);
+		// Check that both files were detected
+		assertEquals(2, detected.size());
+		assertTrue(detected.contains(file1));
+		assertTrue(detected.contains(file2));
+	}
+
+	@After
+	public void tearDown() {
+		TestUtils.deleteTestDirectory(testDir);
+	}
+
+	private RemovableDriveMonitor createMonitor(final File dir) {
+		return new UnixRemovableDriveMonitor() {
+			@Override
+			protected String[] getPathsToWatch() {
+				return new String[] { dir.getPath() };
+			}
+		};
+	}
+}
-- 
GitLab