This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
commit e88059307a0742eaa00ccc4f9754653b66f9aad3 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jul 16 08:41:11 2021 -0400 Sort members. Formatting nits. --- .../commons/vfs2/impl/DefaultFileMonitorTest.java | 372 +++++++++++---------- 1 file changed, 188 insertions(+), 184 deletions(-) diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java index 9505b4e..2328e52 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java @@ -44,16 +44,137 @@ import org.junit.Test; */ public class DefaultFileMonitorTest { + private static class CountingListener implements FileListener { + private final AtomicLong created = new AtomicLong(); + + @Override + public void fileChanged(final FileChangeEvent event) { + throw new UnsupportedOperationException(); + } + + @Override + public void fileCreated(final FileChangeEvent event) { + created.incrementAndGet(); + } + + @Override + public void fileDeleted(final FileChangeEvent event) { + throw new UnsupportedOperationException(); + } + } + + private enum Status { + CHANGED, DELETED, CREATED + } + + private class TestFileListener implements FileListener { + @Override + public void fileChanged(final FileChangeEvent event) throws Exception { + changeStatus = Status.CHANGED; + } + + @Override + public void fileCreated(final FileChangeEvent event) throws Exception { + changeStatus = Status.CREATED; + } + + @Override + public void fileDeleted(final FileChangeEvent event) throws Exception { + changeStatus = Status.DELETED; + } + } + private static final int DELAY_MILLIS = 100; + + @BeforeClass + public static void beforeClass() { + // Fails randomly on Windows. + assumeFalse(SystemUtils.IS_OS_WINDOWS); + } + private FileSystemManager fsManager; + private File testDir; + private volatile Status changeStatus; + private File testFile; - @BeforeClass - public static void beforeClass() { - // Fails randomly on Windows. - assumeFalse(SystemUtils.IS_OS_WINDOWS); + /** + * VFS-299: Handlers are not removed. One instance is {@link DefaultFileMonitor#removeFile(FileObject)}. + * + * As a result, the file monitor will fire two created events. + */ + @Ignore("VFS-299") + @Test + public void ignore_testAddRemove() throws Exception { + try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toString())) { + final CountingListener listener = new CountingListener(); + final DefaultFileMonitor monitor = new DefaultFileMonitor(listener); + monitor.setDelay(DELAY_MILLIS); + try { + monitor.addFile(fileObject); + monitor.removeFile(fileObject); + monitor.addFile(fileObject); + monitor.start(); + writeToFile(testFile); + Thread.sleep(DELAY_MILLIS * 3); + assertEquals("Created event is only fired once", 1, listener.created.get()); + } finally { + monitor.stop(); + } + } + } + + /** + * VFS-299: Handlers are not removed. There is no API for properly decommissioning a file monitor. + * + * As a result, listeners of stopped monitors still receive events. + */ + @Ignore("VFS-299") + @Test + public void ignore_testStartStop() throws Exception { + try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toString())) { + final CountingListener stoppedListener = new CountingListener(); + final DefaultFileMonitor stoppedMonitor = new DefaultFileMonitor(stoppedListener); + stoppedMonitor.start(); + try { + stoppedMonitor.addFile(fileObject); + } finally { + stoppedMonitor.stop(); + } + + // Variant 1: it becomes documented behavior to manually remove all files after stop() such that all + // listeners + // are removed + // This currently does not work, see DefaultFileMonitorTests#testAddRemove above. + // stoppedMonitor.removeFile(file); + + // Variant 2: change behavior of stop(), which then removes all handlers. + // This would remove the possibility to pause watching files. Resuming watching for the same files via + // start(); + // stop(); start(); would not work. + + // Variant 3: introduce new method DefaultFileMonitor#close which definitely removes all resources held by + // DefaultFileMonitor. + + final CountingListener activeListener = new CountingListener(); + final DefaultFileMonitor activeMonitor = new DefaultFileMonitor(activeListener); + activeMonitor.setDelay(DELAY_MILLIS); + activeMonitor.addFile(fileObject); + activeMonitor.start(); + try { + writeToFile(testFile); + Thread.sleep(DELAY_MILLIS * 10); + + assertEquals("The listener of the active monitor received one created event", 1, + activeListener.created.get()); + assertEquals("The listener of the stopped monitor received no events", 0, + stoppedListener.created.get()); + } finally { + activeMonitor.stop(); + } + } } @Before @@ -76,18 +197,20 @@ public class DefaultFileMonitorTest { } @Test - public void testFileCreated() throws Exception { - try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toURL().toString())) { + public void testChildFileDeletedWithoutRecursiveChecking() throws Exception { + writeToFile(testFile); + try (final FileObject fileObject = fsManager.resolveFile(testDir.toURI().toURL().toString())) { final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener()); - // TestFileListener manipulates changeStatus - monitor.setDelay(DELAY_MILLIS); + monitor.setDelay(2000); + monitor.setRecursive(false); monitor.addFile(fileObject); monitor.start(); try { - writeToFile(testFile); + changeStatus = null; Thread.sleep(DELAY_MILLIS * 5); - assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event", Status.CREATED, changeStatus); + testFile.delete(); + Thread.sleep(DELAY_MILLIS * 30); + assertEquals("Event should not have occurred", null, changeStatus); } finally { monitor.stop(); } @@ -95,44 +218,27 @@ public class DefaultFileMonitorTest { } @Test - public void testFileDeleted() throws Exception { + public void testChildFileRecreated() throws Exception { writeToFile(testFile); - try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toString())) { + try (final FileObject fileObj = fsManager.resolveFile(testDir.toURI().toURL().toString())) { final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener()); - // TestFileListener manipulates changeStatus - monitor.setDelay(DELAY_MILLIS); - monitor.addFile(fileObject); + monitor.setDelay(2000); + monitor.setRecursive(true); + monitor.addFile(fileObj); monitor.start(); try { + changeStatus = null; + Thread.sleep(DELAY_MILLIS * 5); testFile.delete(); - Thread.sleep(500); + Thread.sleep(DELAY_MILLIS * 30); assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event", Status.DELETED, changeStatus); - } finally { - monitor.stop(); - } - } - } - - @Test - public void testFileModified() throws Exception { - writeToFile(testFile); - try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toURL().toString())) { - final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener()); - // TestFileListener manipulates changeStatus - monitor.setDelay(DELAY_MILLIS); - monitor.addFile(fileObject); - monitor.start(); - try { - // Need a long delay to insure the new timestamp doesn't truncate to be the same as - // the current timestammp. Java only guarantees the timestamp will be to 1 second. - Thread.sleep(DELAY_MILLIS * 10); - final long valueMillis = System.currentTimeMillis(); - final boolean rcMillis = testFile.setLastModified(valueMillis); - assertTrue("setLastModified succeeded", rcMillis); + assertEquals("Incorrect event " + changeStatus, Status.DELETED, changeStatus); + changeStatus = null; Thread.sleep(DELAY_MILLIS * 5); + writeToFile(testFile); + Thread.sleep(DELAY_MILLIS * 30); assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event", Status.CHANGED, changeStatus); + assertEquals("Incorrect event " + changeStatus, Status.CREATED, changeStatus); } finally { monitor.stop(); } @@ -140,7 +246,7 @@ public class DefaultFileMonitorTest { } @Test - public void testFileRecreated() throws Exception { + public void testFileCreated() throws Exception { try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toURL().toString())) { final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener()); // TestFileListener manipulates changeStatus @@ -151,19 +257,7 @@ public class DefaultFileMonitorTest { writeToFile(testFile); Thread.sleep(DELAY_MILLIS * 5); assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event " + changeStatus, Status.CREATED, changeStatus); - changeStatus = null; - testFile.delete(); - Thread.sleep(DELAY_MILLIS * 5); - assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event " + changeStatus, Status.DELETED, changeStatus); - changeStatus = null; - Thread.sleep(DELAY_MILLIS * 5); - monitor.addFile(fileObject); - writeToFile(testFile); - Thread.sleep(DELAY_MILLIS * 10); - assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event " + changeStatus, Status.CREATED, changeStatus); + assertEquals("Incorrect event", Status.CREATED, changeStatus); } finally { monitor.stop(); } @@ -171,27 +265,19 @@ public class DefaultFileMonitorTest { } @Test - public void testChildFileRecreated() throws Exception { + public void testFileDeleted() throws Exception { writeToFile(testFile); - try (final FileObject fileObj = fsManager.resolveFile(testDir.toURI().toURL().toString())) { + try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toString())) { final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener()); - monitor.setDelay(2000); - monitor.setRecursive(true); - monitor.addFile(fileObj); + // TestFileListener manipulates changeStatus + monitor.setDelay(DELAY_MILLIS); + monitor.addFile(fileObject); monitor.start(); try { - changeStatus = null; - Thread.sleep(DELAY_MILLIS * 5); testFile.delete(); - Thread.sleep(DELAY_MILLIS * 30); - assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event " + changeStatus, Status.DELETED, changeStatus); - changeStatus = null; - Thread.sleep(DELAY_MILLIS * 5); - writeToFile(testFile); - Thread.sleep(DELAY_MILLIS * 30); + Thread.sleep(500); assertTrue("No event occurred", changeStatus != null); - assertEquals("Incorrect event " + changeStatus, Status.CREATED, changeStatus); + assertEquals("Incorrect event", Status.DELETED, changeStatus); } finally { monitor.stop(); } @@ -199,20 +285,24 @@ public class DefaultFileMonitorTest { } @Test - public void testChildFileDeletedWithoutRecursiveChecking() throws Exception { + public void testFileModified() throws Exception { writeToFile(testFile); - try (final FileObject fileObject = fsManager.resolveFile(testDir.toURI().toURL().toString())) { + try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toURL().toString())) { final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener()); - monitor.setDelay(2000); - monitor.setRecursive(false); + // TestFileListener manipulates changeStatus + monitor.setDelay(DELAY_MILLIS); monitor.addFile(fileObject); monitor.start(); try { - changeStatus = null; + // Need a long delay to insure the new timestamp doesn't truncate to be the same as + // the current timestammp. Java only guarantees the timestamp will be to 1 second. + Thread.sleep(DELAY_MILLIS * 10); + final long valueMillis = System.currentTimeMillis(); + final boolean rcMillis = testFile.setLastModified(valueMillis); + assertTrue("setLastModified succeeded", rcMillis); Thread.sleep(DELAY_MILLIS * 5); - testFile.delete(); - Thread.sleep(DELAY_MILLIS * 30); - assertEquals("Event should not have occurred", null, changeStatus); + assertTrue("No event occurred", changeStatus != null); + assertEquals("Incorrect event", Status.CHANGED, changeStatus); } finally { monitor.stop(); } @@ -247,79 +337,33 @@ public class DefaultFileMonitorTest { } } - /** - * VFS-299: Handlers are not removed. One instance is {@link DefaultFileMonitor#removeFile(FileObject)}. - * - * As a result, the file monitor will fire two created events. - */ - @Ignore("VFS-299") @Test - public void ignore_testAddRemove() throws Exception { - try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toString())) { - final CountingListener listener = new CountingListener(); - final DefaultFileMonitor monitor = new DefaultFileMonitor(listener); + public void testFileRecreated() throws Exception { + try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toURL().toString())) { + final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener()); + // TestFileListener manipulates changeStatus monitor.setDelay(DELAY_MILLIS); + monitor.addFile(fileObject); + monitor.start(); try { - monitor.addFile(fileObject); - monitor.removeFile(fileObject); - monitor.addFile(fileObject); - monitor.start(); writeToFile(testFile); - Thread.sleep(DELAY_MILLIS * 3); - assertEquals("Created event is only fired once", 1, listener.created.get()); - } finally { - monitor.stop(); - } - } - } - - /** - * VFS-299: Handlers are not removed. There is no API for properly decommissioning a file monitor. - * - * As a result, listeners of stopped monitors still receive events. - */ - @Ignore("VFS-299") - @Test - public void ignore_testStartStop() throws Exception { - try (final FileObject fileObject = fsManager.resolveFile(testFile.toURI().toString())) { - final CountingListener stoppedListener = new CountingListener(); - final DefaultFileMonitor stoppedMonitor = new DefaultFileMonitor(stoppedListener); - stoppedMonitor.start(); - try { - stoppedMonitor.addFile(fileObject); - } finally { - stoppedMonitor.stop(); - } - - // Variant 1: it becomes documented behavior to manually remove all files after stop() such that all - // listeners - // are removed - // This currently does not work, see DefaultFileMonitorTests#testAddRemove above. - // stoppedMonitor.removeFile(file); - - // Variant 2: change behavior of stop(), which then removes all handlers. - // This would remove the possibility to pause watching files. Resuming watching for the same files via - // start(); - // stop(); start(); would not work. - - // Variant 3: introduce new method DefaultFileMonitor#close which definitely removes all resources held by - // DefaultFileMonitor. - - final CountingListener activeListener = new CountingListener(); - final DefaultFileMonitor activeMonitor = new DefaultFileMonitor(activeListener); - activeMonitor.setDelay(DELAY_MILLIS); - activeMonitor.addFile(fileObject); - activeMonitor.start(); - try { + Thread.sleep(DELAY_MILLIS * 5); + assertTrue("No event occurred", changeStatus != null); + assertEquals("Incorrect event " + changeStatus, Status.CREATED, changeStatus); + changeStatus = null; + testFile.delete(); + Thread.sleep(DELAY_MILLIS * 5); + assertTrue("No event occurred", changeStatus != null); + assertEquals("Incorrect event " + changeStatus, Status.DELETED, changeStatus); + changeStatus = null; + Thread.sleep(DELAY_MILLIS * 5); + monitor.addFile(fileObject); writeToFile(testFile); Thread.sleep(DELAY_MILLIS * 10); - - assertEquals("The listener of the active monitor received one created event", 1, - activeListener.created.get()); - assertEquals("The listener of the stopped monitor received no events", 0, - stoppedListener.created.get()); + assertTrue("No event occurred", changeStatus != null); + assertEquals("Incorrect event " + changeStatus, Status.CREATED, changeStatus); } finally { - activeMonitor.stop(); + monitor.stop(); } } } @@ -331,44 +375,4 @@ public class DefaultFileMonitorTest { } } - private static class CountingListener implements FileListener { - private final AtomicLong created = new AtomicLong(); - - @Override - public void fileCreated(final FileChangeEvent event) { - created.incrementAndGet(); - } - - @Override - public void fileDeleted(final FileChangeEvent event) { - throw new UnsupportedOperationException(); - } - - @Override - public void fileChanged(final FileChangeEvent event) { - throw new UnsupportedOperationException(); - } - } - - private enum Status { - CHANGED, DELETED, CREATED - } - - private class TestFileListener implements FileListener { - @Override - public void fileChanged(final FileChangeEvent event) throws Exception { - changeStatus = Status.CHANGED; - } - - @Override - public void fileDeleted(final FileChangeEvent event) throws Exception { - changeStatus = Status.DELETED; - } - - @Override - public void fileCreated(final FileChangeEvent event) throws Exception { - changeStatus = Status.CREATED; - } - } - }