You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2010/10/08 02:10:44 UTC

svn commit: r1005676 - in /commons/proper/io/trunk/src: main/java/org/apache/commons/io/monitor/FilesystemEntry.java main/java/org/apache/commons/io/monitor/FilesystemObserver.java test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java

Author: niallp
Date: Fri Oct  8 00:10:43 2010
New Revision: 1005676

URL: http://svn.apache.org/viewvc?rev=1005676&view=rev
Log:
IO-196 FilesystemObserverTestCase failures - revert r1002909 to test case and change how FilesystemMonitor compares files for changes to see if this resolves the original test failures

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java?rev=1005676&r1=1005675&r2=1005676&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java Fri Oct  8 00:10:43 2010
@@ -43,7 +43,7 @@ public class FilesystemEntry implements 
 
     private final FilesystemEntry parent;
     private FilesystemEntry[] children;
-    private File file;
+    private final File file;
     private String name;
     private boolean exists;
     private boolean directory;
@@ -79,7 +79,7 @@ public class FilesystemEntry implements 
      * This implementation refreshes the <code>name</code>, <code>exists</code>
      * <code>directory</code> and <code>lastModified</code> properties.
      */
-    public void refresh() {
+    public void refresh(File file) {
         name = file.getName();
         exists = file.exists();
         if (exists) {
@@ -109,7 +109,7 @@ public class FilesystemEntry implements 
      *
      * @return whether the file has changed or not
      */
-    public boolean hasChanged() {
+    public boolean hasChanged(File file) {
         return (lastModified != file.lastModified());
     }
 
@@ -161,15 +161,6 @@ public class FilesystemEntry implements 
     }
 
     /**
-     * Set the file being monitored.
-     *
-     * @param file the file being monitored
-     */
-    public void setFile(File file) {
-        this.file = file;
-    }
-
-    /**
      * Return the file name.
      *
      * @return the file name

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java?rev=1005676&r1=1005675&r2=1005676&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java Fri Oct  8 00:10:43 2010
@@ -304,7 +304,7 @@ public class FilesystemObserver implemen
      * @throws Exception if an error occurs
      */
     public void initialize() throws Exception {
-        rootEntry.refresh();
+        rootEntry.refresh(rootEntry.getFile());
         File[] files = listFiles(rootEntry.getFile());
         FilesystemEntry[] children = files.length > 0 ? new FilesystemEntry[files.length] : EMPTY_ENTRIES;
         for (int i = 0; i < files.length; i++) {
@@ -389,7 +389,7 @@ public class FilesystemObserver implemen
      */
     private FilesystemEntry createFileEntry(FilesystemEntry parent, File file) {
         FilesystemEntry entry = parent.newChildInstance(file);
-        entry.refresh();
+        entry.refresh(file);
         File[] files = listFiles(file);
         FilesystemEntry[] children = files.length > 0 ? new FilesystemEntry[files.length] : EMPTY_ENTRIES;
         for (int i = 0; i < files.length; i++) {
@@ -425,17 +425,16 @@ public class FilesystemObserver implemen
      * @param file The current file
      */
     private void doMatch(FilesystemEntry entry, File file) {
-        if (entry.hasChanged()) {
+        if (entry.hasChanged(file)) {
             for (FilesystemListener listener : listeners) {
                 if (entry.isDirectory()) {
-                    listener.onDirectoryChange(entry.getFile());
+                    listener.onDirectoryChange(file);
                 } else {
-                    listener.onFileChange(entry.getFile());
+                    listener.onFileChange(file);
                 }
             }
-            entry.refresh();
+            entry.refresh(file);
         }
-        entry.setFile(file);
     }
 
     /**

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java?rev=1005676&r1=1005675&r2=1005676&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java Fri Oct  8 00:10:43 2010
@@ -114,8 +114,7 @@ public class FilesystemObserverTestCase 
             File testDirBFile1 = touch(new File(testDirB, "B-file1.java"));
  
             checkAndNotify();
-            checkDirectoryCounts("B", 3, 0, 0);
-            checkFileCounts("B", 4, 0, 0);
+            checkCollectionSizes("B", 3, 0, 0, 4, 0, 0);
             assertTrue("B testDirA",   listener.getCreatedDirectories().contains(testDirA));
             assertTrue("B testDirB",   listener.getCreatedDirectories().contains(testDirB));
             assertTrue("B testDirC",   listener.getCreatedDirectories().contains(testDirC));
@@ -131,16 +130,14 @@ public class FilesystemObserverTestCase 
             testDirAFile4 = touch(testDirAFile4);
             FileUtils.deleteDirectory(testDirB);
             checkAndNotify();
-            checkDirectoryCounts("D", 0, 0, 1);
-            checkFileCounts("D", 0, 1, 1);
+            checkCollectionSizes("D", 0, 0, 1, 0, 1, 1);
             assertTrue("D testDirB",   listener.getDeletedDirectories().contains(testDirB));
             assertTrue("D testDirAFile4", listener.getChangedFiles().contains(testDirAFile4));
             assertTrue("D testDirBFile1", listener.getDeletedFiles().contains(testDirBFile1));
 
             FileUtils.deleteDirectory(testDir);
             checkAndNotify();
-            checkDirectoryCounts("E", 0, 0, 2);
-            checkFileCounts("E", 0, 0, 3);
+            checkCollectionSizes("E", 0, 0, 2, 0, 0, 3);
             assertTrue("E testDirA",   listener.getDeletedDirectories().contains(testDirA));
             assertTrue("E testDirAFile1", listener.getDeletedFiles().contains(testDirAFile1));
             assertFalse("E testDirAFile2", listener.getDeletedFiles().contains(testDirAFile2));
@@ -177,8 +174,7 @@ public class FilesystemObserverTestCase 
             File testDirAFile5 =       new File(testDirA, "A-file5.java");
  
             checkAndNotify();
-            checkDirectoryCounts("B", 1, 0, 0);
-            checkFileCounts("B", 2, 0, 0);
+            checkCollectionSizes("B", 1, 0, 0, 2, 0, 0);
             assertFalse("B testDirAFile1", listener.getCreatedFiles().contains(testDirAFile1));
             assertTrue("B testDirAFile2",  listener.getCreatedFiles().contains(testDirAFile2));
             assertFalse("B testDirAFile3", listener.getCreatedFiles().contains(testDirAFile3));
@@ -196,22 +192,25 @@ public class FilesystemObserverTestCase 
 
             // Create file with name < first entry
             testDirAFile1 = touch(testDirAFile1);
+            testDirA      = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("D", 1, 0, 0);
+            checkCollectionSizes("D", 0, 1, 0, 1, 0, 0);
             assertTrue("D testDirAFile1 exists", testDirAFile1.exists());
             assertTrue("D testDirAFile1",  listener.getCreatedFiles().contains(testDirAFile1));
 
             // Create file with name between 2 entries
             testDirAFile3 = touch(testDirAFile3);
+            testDirA      = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("E", 1, 0, 0);
+            checkCollectionSizes("E", 0, 1, 0, 1, 0, 0);
             assertTrue("E testDirAFile3 exists", testDirAFile3.exists());
             assertTrue("E testDirAFile3",  listener.getCreatedFiles().contains(testDirAFile3));
 
             // Create file with name > last entry
             testDirAFile5 = touch(testDirAFile5);
+            testDirA      = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("F", 1, 0, 0);
+            checkCollectionSizes("F", 0, 1, 0, 1, 0, 0);
             assertTrue("F testDirAFile5 exists", testDirAFile5.exists());
             assertTrue("F testDirAFile5",  listener.getCreatedFiles().contains(testDirAFile5));
         } catch (Exception e) {
@@ -237,8 +236,7 @@ public class FilesystemObserverTestCase 
             File testDirAFile5 = touch(new File(testDirA, "A-file5.java"));
  
             checkAndNotify();
-            checkDirectoryCounts("B", 1, 0, 0);
-            checkFileCounts("B", 5, 0, 0);
+            checkCollectionSizes("B", 1, 0, 0, 5, 0, 0);
             assertTrue("B testDirAFile1", listener.getCreatedFiles().contains(testDirAFile1));
             assertTrue("B testDirAFile2", listener.getCreatedFiles().contains(testDirAFile2));
             assertTrue("B testDirAFile3", listener.getCreatedFiles().contains(testDirAFile3));
@@ -256,20 +254,23 @@ public class FilesystemObserverTestCase 
 
             // Update first entry
             testDirAFile1 = touch(testDirAFile1);
+            testDirA      = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("D", 0, 1, 0);
+            checkCollectionSizes("D", 0, 1, 0, 0, 1, 0);
             assertTrue("D testDirAFile1",  listener.getChangedFiles().contains(testDirAFile1));
 
             // Update file with name between 2 entries
             testDirAFile3 = touch(testDirAFile3);
+            testDirA      = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("E", 0, 1, 0);
+            checkCollectionSizes("E", 0, 1, 0, 0, 1, 0);
             assertTrue("E testDirAFile3",  listener.getChangedFiles().contains(testDirAFile3));
 
             // Update last entry
             testDirAFile5 = touch(testDirAFile5);
+            testDirA      = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("F", 0, 1, 0);
+            checkCollectionSizes("F", 0, 1, 0, 0, 1, 0);
             assertTrue("F testDirAFile5",  listener.getChangedFiles().contains(testDirAFile5));
         } catch (Exception e) {
             fail("Threw " + e);
@@ -300,8 +301,7 @@ public class FilesystemObserverTestCase 
             assertTrue("B testDirAFile5 exists", testDirAFile5.exists());
 
             checkAndNotify();
-            checkDirectoryCounts("B", 1, 0, 0);
-            checkFileCounts("B", 5, 0, 0);
+            checkCollectionSizes("B", 1, 0, 0, 5, 0, 0);
             assertTrue("B testDirAFile1", listener.getCreatedFiles().contains(testDirAFile1));
             assertTrue("B testDirAFile2", listener.getCreatedFiles().contains(testDirAFile2));
             assertTrue("B testDirAFile3", listener.getCreatedFiles().contains(testDirAFile3));
@@ -313,22 +313,25 @@ public class FilesystemObserverTestCase 
 
             // Delete first entry
             FileUtils.deleteQuietly(testDirAFile1);
+            testDirA = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("D", 0, 0, 1);
+            checkCollectionSizes("D", 0, 1, 0, 0, 0, 1);
             assertFalse("D testDirAFile1 exists", testDirAFile1.exists());
             assertTrue("D testDirAFile1",  listener.getDeletedFiles().contains(testDirAFile1));
 
             // Delete file with name between 2 entries
             FileUtils.deleteQuietly(testDirAFile3);
+            testDirA = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("E", 0, 0, 1);
+            checkCollectionSizes("E", 0, 1, 0, 0, 0, 1);
             assertFalse("E testDirAFile3 exists", testDirAFile3.exists());
             assertTrue("E testDirAFile3",  listener.getDeletedFiles().contains(testDirAFile3));
 
             // Delete last entry
             FileUtils.deleteQuietly(testDirAFile5);
+            testDirA = touch(testDirA);
             checkAndNotify();
-            checkFileCounts("F", 0, 0, 1);
+            checkCollectionSizes("F", 0, 1, 0, 0, 0, 1);
             assertFalse("F testDirAFile5 exists", testDirAFile5.exists());
             assertTrue("F testDirAFile5",  listener.getDeletedFiles().contains(testDirAFile5));
 
@@ -360,8 +363,7 @@ public class FilesystemObserverTestCase 
             assertTrue("B testDirAFile2 exists", testDirAFile2.exists());
             assertTrue("B testDirAFile3 exists", testDirAFile3.exists());
             checkAndNotify();
-            checkDirectoryCounts("C", 0, 0, 0);
-            checkFileCounts("C", 1, 0, 0);
+            checkCollectionSizes("C", 0, 0, 0, 1, 0, 0);
             assertTrue("C created", listener.getCreatedFiles().contains(testDirAFile1));
             assertFalse("C created", listener.getCreatedFiles().contains(testDirAFile2));
             assertFalse("C created", listener.getCreatedFiles().contains(testDirAFile3));
@@ -371,8 +373,7 @@ public class FilesystemObserverTestCase 
             testDirAFile2 = touch(testDirAFile2);
             testDirAFile3 = touch(testDirAFile3);
             checkAndNotify();
-            checkDirectoryCounts("D", 0, 0, 0);
-            checkFileCounts("D", 0, 1, 0);
+            checkCollectionSizes("D", 0, 0, 0, 0, 1, 0);
             assertTrue("D changed", listener.getChangedFiles().contains(testDirAFile1));
             assertFalse("D changed", listener.getChangedFiles().contains(testDirAFile2));
             assertFalse("D changed", listener.getChangedFiles().contains(testDirAFile3));
@@ -385,8 +386,7 @@ public class FilesystemObserverTestCase 
             assertFalse("E testDirAFile2 exists", testDirAFile2.exists());
             assertFalse("E testDirAFile3 exists", testDirAFile3.exists());
             checkAndNotify();
-            checkDirectoryCounts("E", 0, 0, 0);
-            checkFileCounts("E", 0, 0, 1);
+            checkCollectionSizes("E", 0, 0, 0, 0, 0, 1);
             assertTrue("E deleted", listener.getDeletedFiles().contains(testDirAFile1));
             assertFalse("E deleted", listener.getDeletedFiles().contains(testDirAFile2));
             assertFalse("E deleted", listener.getDeletedFiles().contains(testDirAFile3));
@@ -409,29 +409,22 @@ public class FilesystemObserverTestCase 
      * Check all the Collections are empty
      */
     private void checkCollectionsEmpty(String label) {
-        checkDirectoryCounts("EMPTY-" + label, 0, 0, 0);
-        checkFileCounts("EMPTY-" + label, 0, 0, 0);
+        checkCollectionSizes("EMPTY-" + label, 0, 0, 0, 0, 0, 0);
     }
 
     /**
-     * Check all the Directory Collections have the expected sizes.
+     * Check all the Collections have the expected sizes.
      */
-    private void checkDirectoryCounts(String label, int dirCreate, int dirChange, int dirDelete) {
+    private void checkCollectionSizes(String label, int dirCreate, int dirChange, int dirDelete, int fileCreate, int fileChange, int fileDelete) {
         label = label + "[" + listener.getCreatedDirectories().size() +
                         " " + listener.getChangedDirectories().size() +
-                        " " + listener.getDeletedDirectories().size() + "]";
+                        " " + listener.getDeletedDirectories().size() +
+                        " " + listener.getCreatedFiles().size() +
+                        " " + listener.getChangedFiles().size() +
+                        " " + listener.getDeletedFiles().size() + "]";
         assertEquals(label + ": No. of directories created",  dirCreate,  listener.getCreatedDirectories().size());
         assertEquals(label + ": No. of directories changed",  dirChange,  listener.getChangedDirectories().size());
         assertEquals(label + ": No. of directories deleted",  dirDelete,  listener.getDeletedDirectories().size());
-    }
-
-    /**
-     * Check all the File Collections have the expected sizes.
-     */
-    private void checkFileCounts(String label, int fileCreate, int fileChange, int fileDelete) {
-        label = label + "[" + listener.getCreatedFiles().size() +
-                        " " + listener.getChangedFiles().size() +
-                        " " + listener.getDeletedFiles().size() + "]";
         assertEquals(label + ": No. of files created", fileCreate, listener.getCreatedFiles().size());
         assertEquals(label + ": No. of files changed", fileChange, listener.getChangedFiles().size());
         assertEquals(label + ": No. of files deleted", fileDelete, listener.getDeletedFiles().size());