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


The following commit(s) were added to refs/heads/master by this push:
     new 79a9692  Test stopping file monitor (#56)
79a9692 is described below

commit 79a969283bc7c85f1c4d73ed45e0323e01f4e252
Author: Boris Petrov <[email protected]>
AuthorDate: Thu Mar 21 23:10:25 2019 +0200

    Test stopping file monitor (#56)
    
    [VFS-694] Fix inability to start the DefaultFileMonitor after it has been 
stopped.
    
    * Better handling of DefaultFileMonitor restart
    * Code style
---
 .../commons/vfs2/impl/DefaultFileMonitor.java      | 21 +++++++++++++-------
 .../vfs2/impl/test/DefaultFileMonitorTests.java    | 23 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 7 deletions(-)

diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileMonitor.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileMonitor.java
index 5b80845..99e1e77 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileMonitor.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileMonitor.java
@@ -307,8 +307,15 @@ public class DefaultFileMonitor implements Runnable, 
FileMonitor {
      */
     public void stop() {
         this.shouldRun = false;
-        this.monitorThread.interrupt();
-        this.monitorThread = null;
+        if (this.monitorThread != null) {
+            this.monitorThread.interrupt();
+            try {
+                this.monitorThread.join();
+            } catch (final InterruptedException e) {
+                // ignore
+            }
+            this.monitorThread = null;
+        }
     }
 
     /**
@@ -489,11 +496,11 @@ public class DefaultFileMonitor implements Runnable, 
FileMonitor {
                     } else {
                         // First set of children - Break out the cigars
                         if (newChildren.length > 0) {
-                               this.children = new HashMap<>();
-                               for (final FileObject element : newChildren) {
-                                       this.children.put(element.getName(), 
new Object()); // null?
-                                       this.fireAllCreate(element);
-                               }
+                            this.children = new HashMap<>();
+                            for (final FileObject element : newChildren) {
+                                this.children.put(element.getName(), new 
Object()); // null?
+                                this.fireAllCreate(element);
+                            }
                         }
                     }
                 }
diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/test/DefaultFileMonitorTests.java
 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/test/DefaultFileMonitorTests.java
index 2bfe6ea..fe55b5b 100644
--- 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/test/DefaultFileMonitorTests.java
+++ 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/test/DefaultFileMonitorTests.java
@@ -169,6 +169,29 @@ public class DefaultFileMonitorTests extends 
AbstractVfsTestCase {
         }
     }
 
+    public void testFileMonitorRestarted() throws Exception {
+        final FileObject fileObj = 
fsManager.resolveFile(testFile.toURI().toString());
+        final DefaultFileMonitor monitor = new DefaultFileMonitor(new 
TestFileListener());
+        // TestFileListener manipulates changeStatus
+        monitor.setDelay(100);
+        monitor.addFile(fileObj);
+
+        monitor.start();
+        writeToFile(testFile);
+        Thread.sleep(300);
+        monitor.stop();
+
+        monitor.start();
+        try {
+            testFile.delete();
+            Thread.sleep(300);
+            assertTrue("No event occurred", changeStatus != 0);
+            assertTrue("Incorrect event", changeStatus == 2);
+        } finally {
+            monitor.stop();
+        }
+    }
+
     private void writeToFile(final File file) throws Exception {
         final FileWriter out = new FileWriter(file);
         out.write("string=value1");

Reply via email to