Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added classes for Guice configuration, added test that tests Guice co…

…nfig, as well as listening code made constructor for DirectoryEventWatcherImpl package private
  • Loading branch information...
commit c58c1c80ea30ab2fdaf9420d710405c5708855b9 1 parent 958ab65
@bbejeck authored
View
22 src/main/java/bbejeck/nio/files/directory/event/DirectoryEventModule.java
@@ -0,0 +1,22 @@
+package bbejeck.nio.files.directory.event;
+
+import com.google.common.eventbus.EventBus;
+import com.google.inject.AbstractModule;
+import com.google.inject.matcher.Matchers;
+import com.google.inject.name.Names;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 2/25/12
+ * Time: 9:30 PM
+ */
+
+public class DirectoryEventModule extends AbstractModule {
+
+ @Override
+ protected void configure() {
+ bind(String.class).annotatedWith(Names.named("START_PATH")).toInstance("test-files");
+ bind(DirectoryEventWatcher.class).toProvider(DirectoryEventWatcherProvider.class);
+ }
+}
View
2  src/main/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherImpl.java
@@ -31,7 +31,7 @@
private Path startPath;
- public DirectoryEventWatcherImpl(EventBus eventBus, Path startPath) {
+ DirectoryEventWatcherImpl(EventBus eventBus, Path startPath) {
this.eventBus = Objects.requireNonNull(eventBus);
this.startPath = Objects.requireNonNull(startPath);
}
View
33 src/main/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherProvider.java
@@ -0,0 +1,33 @@
+package bbejeck.nio.files.directory.event;
+
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.name.Named;
+
+import java.nio.file.Paths;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 2/25/12
+ * Time: 9:19 PM
+ */
+
+public class DirectoryEventWatcherProvider implements Provider<DirectoryEventWatcher> {
+
+ private EventBus eventBus;
+ private String startPath;
+
+
+ @Inject
+ public DirectoryEventWatcherProvider(EventBus eventBus, @Named("START_PATH") String startPath) {
+ this.eventBus = eventBus;
+ this.startPath = startPath;
+ }
+
+ @Override
+ public DirectoryEventWatcher get() {
+ return new DirectoryEventWatcherImpl(eventBus, Paths.get(startPath));
+ }
+}
View
19 src/main/java/bbejeck/nio/files/event/EventModule.java
@@ -0,0 +1,19 @@
+package bbejeck.nio.files.event;
+
+import com.google.common.eventbus.EventBus;
+import com.google.inject.AbstractModule;
+import com.google.inject.Singleton;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 2/25/12
+ * Time: 10:09 PM
+ */
+public class EventModule extends AbstractModule {
+
+ @Override
+ protected void configure() {
+ bind(EventBus.class).in(Singleton.class);
+ }
+}
View
95 src/test/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherGuiceTest.java
@@ -0,0 +1,95 @@
+package bbejeck.nio.files.directory.event;
+
+import bbejeck.nio.files.BaseFileTest;
+import bbejeck.nio.files.event.EventModule;
+import bbejeck.nio.files.event.PathEventContext;
+import bbejeck.nio.files.event.PathEventSubscriber;
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.file.Path;
+import java.util.*;
+import java.util.concurrent.CountDownLatch;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 2/25/12
+ */
+public class DirectoryEventWatcherGuiceTest extends BaseFileTest {
+
+ private DirectoryEventWatcher directoryEventWatcher;
+ private CountDownLatch latch;
+ private TestSubscriber subscriber;
+
+ @Before
+ public void setUp() throws Exception {
+ createPaths();
+ cleanUp();
+ createDirectories();
+ latch = new CountDownLatch(3);
+ Injector injector = Guice.createInjector(new EventModule(), new DirectoryEventModule());
+ directoryEventWatcher = injector.getInstance(DirectoryEventWatcher.class);
+ EventBus eventBus = injector.getInstance(EventBus.class);
+ subscriber = new TestSubscriber();
+ eventBus.register(subscriber);
+ directoryEventWatcher.start();
+ }
+
+ @Test
+ public void testDirectoryForWrittenEvents() throws Exception {
+ Map<Path, String> eventResultsMap = new HashMap<>();
+ assertThat(directoryEventWatcher.isRunning(), is(true));
+ generateFile(dir1Path.resolve("newTextFile.txt"), 10);
+ generateFile(basePath.resolve("newTextFileII.txt"), 10);
+ generateFile(dir2Path.resolve("newTextFileIII.txt"), 10);
+ latch.await();
+
+ assertThat(subscriber.pathEvents.size(), is(3));
+ List<PathEventContext> eventContexts = subscriber.pathEvents;
+
+ for (PathEventContext eventContext : eventContexts) {
+ Path dir = eventContext.getWatchedDirectory();
+ assertThat(eventContext.getEvents().size(), is(1));
+ Path target = eventContext.getEvents().get(0).getEventTarget();
+ eventResultsMap.put(dir, target.toString());
+ }
+
+ Set<Path> watchedDirs = eventResultsMap.keySet();
+ assertThat(watchedDirs.size(), is(3));
+ assertThat(watchedDirs.contains(dir1Path), is(true));
+ assertThat(watchedDirs.contains(basePath), is(true));
+ assertThat(watchedDirs.contains(dir2Path), is(true));
+
+ assertThat(eventResultsMap.get(dir1Path), is("newTextFile.txt"));
+ assertThat(eventResultsMap.get(basePath), is("newTextFileII.txt"));
+ assertThat(eventResultsMap.get(dir2Path), is("newTextFileIII.txt"));
+
+ }
+
+ @After
+ public void tearDown() {
+ directoryEventWatcher.stop();
+ }
+
+
+ private class TestSubscriber implements PathEventSubscriber {
+ List<PathEventContext> pathEvents = new ArrayList<>();
+
+ @Override
+ @Subscribe
+ public void handlePathEvents(PathEventContext pathEventContext) {
+ pathEvents.add(pathEventContext);
+ latch.countDown();
+ }
+ }
+
+}
View
42 src/test/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherImplTest.java
@@ -9,8 +9,8 @@
import org.junit.Before;
import org.junit.Test;
-import java.util.ArrayList;
-import java.util.List;
+import java.nio.file.Path;
+import java.util.*;
import java.util.concurrent.CountDownLatch;
import static org.hamcrest.CoreMatchers.is;
@@ -41,20 +41,39 @@ public void setUp() throws Exception {
dirWatcher.start();
subscriber = new TestSubscriber();
eventBus.register(subscriber);
- doneSignal = new CountDownLatch(1);
+ doneSignal = new CountDownLatch(3);
}
@Test
public void testDirectoryForWrittenEvents() throws Exception {
- generateFile(basePath.resolve("newTextFile.txt"), 10);
+ Map<Path,String> eventResultsMap = new HashMap<>();
+ assertThat(dirWatcher.isRunning(), is(true));
+ generateFile(dir1Path.resolve("newTextFile.txt"), 10);
generateFile(basePath.resolve("newTextFileII.txt"), 10);
- generateFile(basePath.resolve("newTextFileIII.txt"), 10);
+ generateFile(dir2Path.resolve("newTextFileIII.txt"), 10);
doneSignal.await();
- assertThat(subscriber.getPathEvents().size(), is(1));
- PathEventContext event = subscriber.getPathEvents().get(0);
- assertThat(event.getWatchedDirectory(), is(basePath));
- assertThat(event.getEvents().size(), is(3));
- assertThat(dirWatcher.isRunning(), is(true));
+
+ assertThat(subscriber.pathEvents.size(), is(3));
+ List<PathEventContext> eventContexts = subscriber.pathEvents;
+
+ for (PathEventContext eventContext : eventContexts) {
+ Path dir = eventContext.getWatchedDirectory();
+ assertThat(eventContext.getEvents().size(),is(1));
+ Path target = eventContext.getEvents().get(0).getEventTarget();
+ eventResultsMap.put(dir,target.toString());
+
+ }
+
+ Set<Path> watchedDirs = eventResultsMap.keySet();
+ assertThat(watchedDirs.size(),is(3));
+ assertThat(watchedDirs.contains(dir1Path), is(true));
+ assertThat(watchedDirs.contains(basePath), is(true));
+ assertThat(watchedDirs.contains(dir2Path), is(true));
+
+ assertThat(eventResultsMap.get(dir1Path), is("newTextFile.txt"));
+ assertThat(eventResultsMap.get(basePath), is("newTextFileII.txt"));
+ assertThat(eventResultsMap.get(dir2Path), is("newTextFileIII.txt"));
+
}
@@ -74,8 +93,5 @@ public void handlePathEvents(PathEventContext pathEventContext) {
doneSignal.countDown();
}
- public List<PathEventContext> getPathEvents() {
- return pathEvents;
- }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.