ObserverFactory { public FileAlterationObserver create(Path path, final PathListener listener) { FileAlterationObserver observer = new FileAlterationObserver(path.toFile()); observer.addListener(new FileAlterationListenerAdaptor() { @Override public void onFileChange(File file) { if (file.length() > 0L) { triggerChange(file, listener); } } @Override public void onFileCreate(File file) { triggerChange(file, listener); } }); return observer; }  FileAlterationObserver create(Path path, final PathListener listener);  }
@Test public void should_create_an_observer_that_notify_when_a_file_is_created() throws Exception { FileAlterationObserver observer = factory.create(monitoredFolder, listener); Path aFile = Files.write(monitoredFolder.resolve("aFile"), "coucou".getBytes()); observer.checkAndNotify(); assertThat(listener.getChanged()).containsExactly(aFile); }
@Test public void should_create_an_observer_that_notify_when_a_file_is_modified() throws Exception { Files.write(monitoredFolder.resolve("aFile"), "coucou".getBytes()); FileAlterationObserver observer = factory.create(monitoredFolder, listener); Files.write(monitoredFolder.resolve("aFile"), "modified content".getBytes()); observer.checkAndNotify(); assertThat(listener.getChanged()).containsExactly(monitoredFolder.resolve("aFile")); }