下面列出了怎么用org.apache.commons.io.monitor.FileAlterationListenerAdaptor的API类实例代码及写法,或者点击链接到github查看源代码。
public void start() {
String gamePath = Configuration.get().applicationConfiguration().get().getGamePath();
File folder = new File(gamePath + "logs");
this.fileHandler = new MessageFileHandler(gamePath + "logs/Client.txt");
FileAlterationObserver observer = new FileAlterationObserver(folder);
monitor = new FileAlterationMonitor(POLLING_INTERVAL);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
@Override
public void onFileChange(File file) {
fileHandler.parse();
}
};
observer.addListener(listener);
monitor.addObserver(observer);
try {
monitor.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void shouldBuild() {
try {
final TorrentFileWatcher watcher = new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath);
watcher.start();
watcher.stop();
} catch (final Throwable e) {
fail("Should Not fail");
}
}
@Test
public void shouldFaiToStartWithNonExistingPath() throws IOException {
final Path directory = Files.createDirectory(torrentsPath.resolve("sub-folder"));
final TorrentFileWatcher torrentFileWatcher = new TorrentFileWatcher(
new FileAlterationListenerAdaptor(),
directory,
10
);
Files.delete(directory);
assertThatThrownBy(torrentFileWatcher::start)
.isInstanceOf(IllegalStateException.class);
}
@Test
public void shouldNotBuildWithNullMonitoredFolder() {
assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("monitoredFolder cannot be null");
}
@Test
public void shouldNotBuildWithNonExistingMonitoredFolder() {
assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath.resolve("nop")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Folder '" + torrentsPath.resolve("nop").toAbsolutePath() + "' does not exists.");
}
@Test
public void shouldNotBuildWithNullInterval() {
assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath, null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("interval cannot be null");
}
@Test
public void shouldNotBuildWithIntervalLessThan1() {
assertThatThrownBy(() -> new TorrentFileWatcher(new FileAlterationListenerAdaptor(), torrentsPath, 0))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("interval cannot be less than 1");
}