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 23:21:01 UTC

svn commit: r1006027 - in /commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor: FileSystemMonitorTestCase.java FilesystemObserverTestCase.java

Author: niallp
Date: Fri Oct  8 21:20:58 2010
New Revision: 1006027

URL: http://svn.apache.org/viewvc?rev=1006027&view=rev
Log:
Refactor tests to share common code

Modified:
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileSystemMonitorTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileSystemMonitorTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileSystemMonitorTestCase.java?rev=1006027&r1=1006026&r2=1006027&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileSystemMonitorTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileSystemMonitorTestCase.java Fri Oct  8 21:20:58 2010
@@ -20,25 +20,11 @@ import java.io.File;
 import java.util.Collection;
 
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.filefilter.FileFilterUtils;
-import org.apache.commons.io.filefilter.HiddenFileFilter;
-import org.apache.commons.io.filefilter.IOFileFilter;
-
-import junit.framework.TestCase;
 
 /**
  * {@link FilesystemMonitor} Test Case.
  */
-public class FileSystemMonitorTestCase extends TestCase {
-
-    /** Filesystem observer */
-    protected FilesystemObserver observer;
-
-    /** Listener which collects file changes */
-    protected CollectionFilesystemListener listener = new CollectionFilesystemListener(false);
-
-    /** Directory for test files */
-    protected File testDir;
+public class FileSystemMonitorTestCase extends FilesystemObserverTestCase {
 
     /**
      * Construct a new test case.
@@ -47,35 +33,7 @@ public class FileSystemMonitorTestCase e
      */
     public FileSystemMonitorTestCase(String name) {
         super(name);
-    }
-
-    @Override
-    protected void setUp() throws Exception {
-        testDir = new File(new File("."), "test-observer");
-        if (testDir.exists()) {
-            FileUtils.cleanDirectory(testDir);
-        } else {
-            testDir.mkdir();
-        }
-
-        IOFileFilter files = FileFilterUtils.fileFileFilter();
-        IOFileFilter javaSuffix = FileFilterUtils.suffixFileFilter(".java");
-        IOFileFilter fileFilter = FileFilterUtils.and(files, javaSuffix);
-        
-        IOFileFilter directories = FileFilterUtils.directoryFileFilter();
-        IOFileFilter visible = HiddenFileFilter.VISIBLE;
-        IOFileFilter dirFilter = FileFilterUtils.and(directories, visible);
-
-        IOFileFilter filter = FileFilterUtils.or(dirFilter, fileFilter);
-
-        // Create the observer
-        observer = new FilesystemObserver(testDir, filter);
-        observer.addListener(listener);
-        try {
-            observer.initialize();
-        } catch (Exception e) {
-            fail("Observer init() threw " + e);
-        }
+        testDirName = "test-monitor";
     }
 
     @Override
@@ -122,72 +80,13 @@ public class FileSystemMonitorTestCase e
     /**
      * Check all the File Collections have the expected sizes.
      */
-    private void checkFile(String label, File file, long interval, Collection<File> files) throws Exception {
+    private void checkFile(String label, File file, long interval, Collection<File> files) {
         for (int i = 0; i < 5; i++) {
-            Thread.sleep(interval);
             if (files.contains(file)) {
                 return; // found, test passes
             }
+            sleepHandleInterruped(interval);
         }
         fail(label + " " + file + " not found");
     }
-
-    /**
-     * Check all the Collections are empty
-     */
-    private void checkCollectionsEmpty(String label) {
-        checkDirectoryCounts("EMPTY-" + label, 0, 0, 0);
-        checkFileCounts("EMPTY-" + label, 0, 0, 0);
-    }
-
-    /**
-     * Check all the Directory Collections have the expected sizes.
-     */
-    private void checkDirectoryCounts(String label, int dirCreate, int dirChange, int dirDelete) {
-        label = label + "[" + listener.getCreatedDirectories().size() +
-                        " " + listener.getChangedDirectories().size() +
-                        " " + listener.getDeletedDirectories().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());
-    }
-
-    /**
-     * Either creates a file if it doesn't exist or updates the last modified date/time
-     * if it does.
-     *
-     * @param file The file to touch
-     * @return The file
-     */
-    private File touch(File file) {
-        long lastModified = file.exists() ? file.lastModified() : 0;
-        try {
-            FileUtils.touch(file);
-            file = new File(file.getParent(), file.getName());
-            while (lastModified == file.lastModified()) {
-                try {
-                    Thread.sleep(200);
-                } catch(InterruptedException ie) {
-                    // ignore
-                }
-                FileUtils.touch(file);
-                file = new File(file.getParent(), file.getName());
-            }
-        } catch (Exception e) {
-            fail("Touching " + file +": " + e);
-        }
-        return 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=1006027&r1=1006026&r2=1006027&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 21:20:58 2010
@@ -37,9 +37,15 @@ public class FilesystemObserverTestCase 
     /** Listener which collects file changes */
     protected CollectionFilesystemListener listener = new CollectionFilesystemListener(true);
 
+    /** Test diretory name */
+    protected String testDirName = "test-observer";
+
     /** Directory for test files */
     protected File testDir;
 
+    /** Time in milliseconds to pause in tests */
+    protected long pauseTime = 100L;
+
     /**
      * Construct a new test case.
      *
@@ -51,7 +57,7 @@ public class FilesystemObserverTestCase 
 
     @Override
     protected void setUp() throws Exception {
-        testDir = new File(new File("."), "test-monitor");
+        testDir = new File(new File("."), testDirName);
         if (testDir.exists()) {
             FileUtils.cleanDirectory(testDir);
         } else {
@@ -79,8 +85,6 @@ public class FilesystemObserverTestCase 
      */
     protected void createObserver(File file, FileFilter fileFilter) {
         observer = new FilesystemObserver(file, fileFilter);
-        // FilesystemListener debuglistener = new JDKLoggingFilesystemListener(getClass().getSimpleName(), Level.FINEST); 
-        // observer.addListener(debuglistener);
         observer.addListener(listener);
         try {
             observer.initialize();
@@ -408,14 +412,14 @@ public class FilesystemObserverTestCase 
     /**
      * Check all the Collections are empty
      */
-    private void checkCollectionsEmpty(String label) {
+    protected void checkCollectionsEmpty(String label) {
         checkCollectionSizes("EMPTY-" + label, 0, 0, 0, 0, 0, 0);
     }
 
     /**
      * Check all the Collections have the expected sizes.
      */
-    private void checkCollectionSizes(String label, int dirCreate, int dirChange, int dirDelete, int fileCreate, int fileChange, int fileDelete) {
+    protected 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() +
@@ -437,23 +441,30 @@ public class FilesystemObserverTestCase 
      * @param file The file to touch
      * @return The file
      */
-    private File touch(File file) {
+    protected File touch(File file) {
         long lastModified = file.exists() ? file.lastModified() : 0;
         try {
             FileUtils.touch(file);
             file = new File(file.getParent(), file.getName());
             while (lastModified == file.lastModified()) {
-                try {
-                    Thread.sleep(50);
-                } catch(InterruptedException ie) {
-                    // ignore
-                }
+                sleepHandleInterruped(50L);
                 FileUtils.touch(file);
                 file = new File(file.getParent(), file.getName());
             }
         } catch (Exception e) {
-            fail("Touching " + file +": " + e);
+            fail("Touching " + file + ": " + e);
         }
         return file;
     }
+
+    /**
+     * Thread.sleep(timeInMilliseconds) - ignore InterruptedException
+     */
+    protected void sleepHandleInterruped(long timeInMilliseconds) {
+        try {
+            Thread.sleep(timeInMilliseconds);
+        } catch(InterruptedException ie) {
+            // ignore
+        }
+    }
 }