commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r231504 - in /jakarta/commons/sandbox/jci/trunk/src: java/org/apache/commons/jci/monitor/ test/org/apache/commons/jci/ test/org/apache/commons/jci/monitor/
Date Thu, 11 Aug 2005 19:17:47 GMT
Author: tcurdt
Date: Thu Aug 11 12:17:38 2005
New Revision: 231504

URL: http://svn.apache.org/viewcvs?rev=231504&view=rev
Log:
added testcases,
but something is still wrong


Added:
    jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/AbstractTestCase.java
Modified:
    jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
    jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/CompilingClassLoaderTestCase.java
    jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/FilesystemAlterationMonitorTestCase.java

Modified: jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java?rev=231504&r1=231503&r2=231504&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
Thu Aug 11 12:17:38 2005
@@ -323,6 +323,7 @@
         if (entry.isDirectory()) {
             final Entry[] currentChilds = entry.getChilds();
             if (entry.hasChanged() || create) {
+                log.debug(entry + " has changed");
                 if (!create) {
                     onChange(root, entry);
                     for (int i = 0; i < currentChilds.length; i++) {
@@ -351,6 +352,7 @@
                     check(root, child, true);
                 }
             } else {
+                log.debug(entry + " has not changed");
                 for (int i = 0; i < currentChilds.length; i++) {
                     final Entry child = currentChilds[i];
                     check(root, child, false);

Added: jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/AbstractTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/AbstractTestCase.java?rev=231504&view=auto
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/AbstractTestCase.java
(added)
+++ jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/AbstractTestCase.java
Thu Aug 11 12:17:38 2005
@@ -0,0 +1,100 @@
+package org.apache.commons.jci;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import junit.framework.TestCase;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+public abstract class AbstractTestCase extends TestCase {
+
+    private final static Log log = LogFactory.getLog(AbstractTestCase.class);
+
+    protected File directory;
+
+    public final class Signal {
+        public boolean triggered;
+    }
+
+    /*
+    public void runBare() throws Throwable {
+        try {
+            setUp();
+            runTest();
+        } finally {
+            tearDown();
+        }
+    }
+    */
+    
+    protected void waitForSignal(final Signal pSignal) {
+        log.debug("waiting for signal");
+        int i = 0;
+        while(true) {
+            synchronized(pSignal) {
+                if (!pSignal.triggered) {
+                    try {
+                        pSignal.wait(1000);
+                    } catch (InterruptedException e) {
+                        ;
+                    }
+                    if (++i > 7) {
+                        fail("timeout");
+                    }
+                } else {
+                    pSignal.triggered = false;
+                    break;
+                }
+            }
+        }
+        
+        log.debug("caught signal");
+    }
+    
+
+    
+    protected void setUp() throws Exception {
+        directory = createTempDirectory();
+        assertTrue(directory.exists());
+        assertTrue(directory.isDirectory());
+    }
+    
+    
+    protected void writeFile( final File pFile, final String pText ) throws IOException {
+        final File parent = pFile.getParentFile();
+        if (!parent.exists()) {
+            if (!parent.mkdirs()) {
+                throw new IOException("could not create" + parent);
+            }
+        }
+        final FileWriter writer = new FileWriter(pFile);
+        writer.write(pText);
+        writer.close();
+        
+        assertTrue(pFile.exists());
+    }
+    
+    protected File createTempDirectory() throws IOException {
+        final File tempFile = File.createTempFile("jci", null);
+        
+        if (!tempFile.delete()) {
+            throw new IOException();
+        }
+        
+        if (!tempFile.mkdir()) {
+            throw new IOException();
+        }
+        
+        return tempFile;         
+    }
+
+
+    protected void tearDown() throws Exception {
+        FileUtils.deleteDirectory(directory);
+    }
+
+
+}

Modified: jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/CompilingClassLoaderTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/CompilingClassLoaderTestCase.java?rev=231504&r1=231503&r2=231504&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/CompilingClassLoaderTestCase.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/CompilingClassLoaderTestCase.java
Thu Aug 11 12:17:38 2005
@@ -1,73 +1,41 @@
 package org.apache.commons.jci;
 
 import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import junit.framework.TestCase;
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 
-public final class CompilingClassLoaderTestCase extends TestCase {
+public final class CompilingClassLoaderTestCase extends AbstractTestCase {
 
     private final static Log log = LogFactory.getLog(CompilingClassLoaderTestCase.class);
     
-    private final Object signal = new Object();
+    private final Signal reload = new Signal();
 
     private CompilingClassLoader cl;
     private ReloadingListener listener;
-    private File repository;
-    
-    private boolean reloaded;
-    private void waitForReload() {
-        log.debug("waiting for reload signal");
-        int i = 0;
-        while(true) {
-            synchronized(signal) {
-                if (!reloaded) {
-                    try {
-                        signal.wait(1000);
-                    } catch (InterruptedException e) {
-                        ;
-                    }
-                    if (++i > 7) {
-                        fail("timeout");
-                    }
-                } else {
-                    reloaded = false;
-                    break;
-                }
-            }
-        }
-        
-        log.debug("caught reload signal");
-    }
     
     protected void setUp() throws Exception {
-         repository = createTempDirectory();
-        assertTrue(repository.exists());
-        assertTrue(repository.isDirectory());
-
+        super.setUp();
+        
         listener = new ReloadingListener() {
             public void reload() {
-                synchronized(signal) {
-                    reloaded = true;
-                    signal.notify();
+                synchronized(reload) {
+                    reload.triggered = true;
+                    reload.notify();
                 }
             }
         };
 
-        cl = new CompilingClassLoader(this.getClass().getClassLoader(), repository);
+        cl = new CompilingClassLoader(this.getClass().getClassLoader(), directory);
         cl.addListener(listener);
         cl.start();
     }
 
     private void initialCompile() throws Exception {
 
-        waitForReload();
+        waitForSignal(reload);
 
-        writeFile(new File(repository, "jci/Simple.java"),
+        writeFile(new File(directory, "jci/Simple.java"),
                 "package jci;\n"
                 + "public class Simple { \n"
                 + "  public String toString() { \n"
@@ -76,7 +44,7 @@
                 + "} \n"
         );
         
-        writeFile(new File(repository, "jci/Extended.java"),
+        writeFile(new File(directory, "jci/Extended.java"),
                 "package jci;\n"
                 + "public class Extended extends Simple { \n"
                 + "  public String toString() { \n"
@@ -85,7 +53,7 @@
                 + "} \n"
         );
         
-        waitForReload();
+        waitForSignal(reload);
     }
     
     
@@ -112,7 +80,7 @@
         o = cl.loadClass("jci.Extended").newInstance();        
         assertTrue("Extended:Simple".equals(o.toString()));
 
-        writeFile(new File(repository, "jci/Simple.java"),
+        writeFile(new File(directory, "jci/Simple.java"),
                 "package jci;\n"
                 + "public class Simple { \n"
                 + "  public String toString() { \n"
@@ -121,7 +89,7 @@
                 + "} \n"
         );
 
-        waitForReload();
+        waitForSignal(reload);
     
         o = cl.loadClass("jci.Simple").newInstance();        
         assertTrue("SIMPLE".equals(o.toString()));
@@ -141,9 +109,9 @@
         o = cl.loadClass("jci.Extended").newInstance();        
         assertTrue("Extended:Simple".equals(o.toString()));
         
-        assertTrue(new File(repository, "jci/Extended.java").delete());
+        assertTrue(new File(directory, "jci/Extended.java").delete());
         
-        waitForReload();
+        waitForSignal(reload);
 
         o = cl.loadClass("jci.Simple").newInstance();        
         assertTrue("Simple".equals(o.toString()));
@@ -168,9 +136,9 @@
         o = cl.loadClass("jci.Extended").newInstance();        
         assertTrue("Extended:Simple".equals(o.toString()));
         
-        assertTrue(new File(repository, "jci/Simple.java").delete());
+        assertTrue(new File(directory, "jci/Simple.java").delete());
         
-        waitForReload();
+        waitForSignal(reload);
 
         try {
             o = cl.loadClass("jci.Extended").newInstance();
@@ -180,40 +148,10 @@
         }
         
     }
-    
+
     protected void tearDown() throws Exception {
         cl.stop();
-        FileUtils.deleteDirectory(repository);
+        super.tearDown();
     }
     
-    
-    private static void writeFile( final File pFile, final String pText ) throws IOException
{
-        final File parent = pFile.getParentFile();
-        if (!parent.exists()) {
-            if (!parent.mkdirs()) {
-                throw new IOException("could not create" + parent);
-            }
-            log.debug("created directory " + parent.getAbsolutePath());
-        }
-        final FileWriter writer = new FileWriter(pFile);
-        writer.write(pText);
-        writer.close();
-        
-        assertTrue(pFile.exists());
-    }
-    
-    private static File createTempDirectory() throws IOException {
-        final File tempFile = File.createTempFile("jci", null);
-        
-        if (!tempFile.delete()) {
-            throw new IOException();
-        }
-        
-        if (!tempFile.mkdir()) {
-            throw new IOException();
-        }
-        
-        return tempFile;         
-    }
-
 }

Modified: jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/FilesystemAlterationMonitorTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/FilesystemAlterationMonitorTestCase.java?rev=231504&r1=231503&r2=231504&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/FilesystemAlterationMonitorTestCase.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/FilesystemAlterationMonitorTestCase.java
Thu Aug 11 12:17:38 2005
@@ -1,13 +1,23 @@
 package org.apache.commons.jci.monitor;
 
 import java.io.File;
-import junit.framework.TestCase;
+import org.apache.commons.jci.AbstractTestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 
 
-public final class FilesystemAlterationMonitorTestCase extends TestCase {
+public final class FilesystemAlterationMonitorTestCase extends AbstractTestCase {
 
-    class MyFilesystemAlterationListener implements FilesystemAlterationListener {
+    private final static Log log = LogFactory.getLog(FilesystemAlterationMonitorTestCase.class);
+
+    private final Signal signal = new Signal();
+
+    private FilesystemAlterationMonitor fam;
+    private MyFilesystemAlterationListener listener;
+    private Thread thread; 
+
+    private class MyFilesystemAlterationListener implements FilesystemAlterationListener
{
         private int started;
         private int stopped;
         private int createdFiles;
@@ -47,6 +57,10 @@
         }
         public void onStop() {
             ++stopped;
+            synchronized(signal) {
+                signal.triggered = true;
+                signal.notify();
+            }
         }
         public void onCreateFile( final File file ) {
             ++createdFiles;
@@ -67,26 +81,183 @@
             ++deletedDirs;
         }       
     }
+
+    private void start() throws Exception {
+        fam = new FilesystemAlterationMonitor();
+        listener = new MyFilesystemAlterationListener();        
+        fam.addListener(listener, directory);
+        thread = new Thread(fam); 
+        thread.start();
+
+        waitForSignal(signal);
+    }
+    
+    private void stop() throws Exception {
+        fam.stop();
+        thread.join();        
+    }
     
-    public void testCreateDetection() {
-        final File repository = new File("");
-        final FilesystemAlterationMonitor fam = new FilesystemAlterationMonitor(); 
-        final MyFilesystemAlterationListener listener = new MyFilesystemAlterationListener();
+    public void testCreateFileDetection() throws Exception {
+        start();
         
-        fam.addListener(listener, repository);
+        log.debug("creating file");
         
-        Thread myThread = new Thread(fam); 
-        myThread.start();
+        final File file = new File(directory, "file");
+        writeFile(file, "file");
+        assertTrue(file.exists());
+        assertTrue(file.isFile());
         
-        fam.stop();
+        waitForSignal(signal);
+        
+        assertTrue(listener.createdFiles == 1);
+        
+        stop();
     }
 
-    public void testDeleteDetection() {
+    public void testCreateDirectoryDetection() throws Exception {
+        start();
+        
+        log.debug("creating dir");
+
+        final File newDirectory = new File(directory, "directory");
+        assertTrue(newDirectory.mkdir());
+        assertTrue(newDirectory.exists());
+        assertTrue(newDirectory.isDirectory());
+        
+        waitForSignal(signal);
+        
+        assertTrue(listener.createdDirs == 1);
         
+        stop();
     }
 
-    public void testModifyDetection() {
+    public void testDeleteFileDetection() throws Exception {
+        start();
         
+        log.debug("creating file");
+        
+        final File file = new File(directory, "file");
+        writeFile(file, "file");
+        assertTrue(file.exists());
+        assertTrue(file.isFile());
+        
+        waitForSignal(signal);
+        
+        assertTrue(listener.createdFiles == 1);
+        
+        file.delete();
+        assertTrue(!file.exists());
+
+        waitForSignal(signal);
+        
+        assertTrue(listener.deletedFiles == 1);
+        
+        stop();        
     }
 
+    public void testDeleteDirectoryDetection() throws Exception {
+        start();
+        
+        log.debug("creating dir");
+
+        final File newDirectory = new File(directory, "directory");
+        assertTrue(newDirectory.mkdir());
+        assertTrue(newDirectory.exists());
+        assertTrue(newDirectory.isDirectory());
+        
+        waitForSignal(signal);
+        
+        assertTrue(listener.createdDirs == 1);
+
+        waitForSignal(signal);
+
+        newDirectory.delete();
+        assertTrue(!newDirectory.exists());
+
+        waitForSignal(signal);
+        
+        assertTrue(listener.deletedDirs == 1);
+
+        stop();
+    }
+
+    public void testModifyFileDetection() throws Exception {
+        start();
+        
+        log.debug("creating file");
+        
+        final File file = new File(directory, "file");
+        writeFile(file, "file");
+        assertTrue(file.exists());
+        assertTrue(file.isFile());
+        
+        waitForSignal(signal);
+        
+        assertTrue(listener.createdFiles == 1);
+
+        waitForSignal(signal);
+
+        writeFile(file, "changed file");
+
+        waitForSignal(signal);
+        
+        assertTrue(listener.changedFiles == 1);
+        
+        stop();
+    }
+
+    public void testCreatingLocalDirectoryChangesLastModified() throws Exception {
+        final long modified = directory.lastModified();
+        
+        final File newDirectory = new File(directory, "directory");
+        assertTrue(newDirectory.mkdir());
+        assertTrue(newDirectory.exists());
+        assertTrue(newDirectory.isDirectory());
+
+        assertTrue(directory.lastModified() != modified);
+    }
+
+    public void testCreatingLocalFileChangesLastModified() throws Exception {
+        final long modified = directory.lastModified();
+        
+        final File file = new File(directory, "file");
+        writeFile(file, "file");
+        assertTrue(file.exists());
+        assertTrue(file.isFile());
+
+        assertTrue(directory.lastModified() != modified);
+    }
+
+    public void testCreatingSubDirectoryChangesLastModified() throws Exception {
+        final File newDirectory = new File(directory, "directory");
+        assertTrue(newDirectory.mkdir());
+        assertTrue(newDirectory.exists());
+        assertTrue(newDirectory.isDirectory());
+
+        final long modified = directory.lastModified();
+        
+        final File newSubDirectory = new File(newDirectory, "sub");
+        assertTrue(newSubDirectory.mkdir());
+        assertTrue(newSubDirectory.exists());
+        assertTrue(newSubDirectory.isDirectory());
+
+        assertTrue(directory.lastModified() != modified);
+    }
+
+    public void testCreatingFileInSubDirectoryChangesLastModified() throws Exception {
+        final File newDirectory = new File(directory, "directory");
+        assertTrue(newDirectory.mkdir());
+        assertTrue(newDirectory.exists());
+        assertTrue(newDirectory.isDirectory());
+
+        final long modified = directory.lastModified();
+        
+        final File file = new File(newDirectory, "file");
+        writeFile(file, "file");
+        assertTrue(file.exists());
+        assertTrue(file.isFile());
+
+        assertTrue(directory.lastModified() != modified);
+    }
+    
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message