commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r787144 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/VFSFileSystem.java test/java/org/apache/commons/configuration2/TestWebdavConfigurationBuilder.java
Date Mon, 22 Jun 2009 05:45:48 GMT
Author: rgoers
Date: Mon Jun 22 05:45:48 2009
New Revision: 787144

URL: http://svn.apache.org/viewvc?rev=787144&view=rev
Log:
VFSFileReloadingStrategy fails when filename is a full url

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/VFSFileSystem.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestWebdavConfigurationBuilder.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/VFSFileSystem.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/VFSFileSystem.java?rev=787144&r1=787143&r2=787144&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/VFSFileSystem.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/VFSFileSystem.java
Mon Jun 22 05:45:48 2009
@@ -196,7 +196,11 @@
                 }
             }
 
-            if (basePath != null)
+            if (UriParser.extractScheme(fileName) != null)
+            {
+                return fileName;
+            }
+            else if (basePath != null)
             {
                 FileName base = fsManager.resolveURI(basePath);
                 return fsManager.resolveName(base, fileName).getURI();

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestWebdavConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestWebdavConfigurationBuilder.java?rev=787144&r1=787143&r2=787144&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestWebdavConfigurationBuilder.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestWebdavConfigurationBuilder.java
Mon Jun 22 05:45:48 2009
@@ -18,6 +18,10 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.Reader;
+import java.io.FileReader;
+import java.io.Writer;
+import java.io.OutputStreamWriter;
 import java.util.Collection;
 import java.util.Set;
 import java.util.HashMap;
@@ -27,8 +31,16 @@
 
 import org.apache.commons.configuration2.beanutils.BeanHelper;
 import org.apache.commons.configuration2.reloading.FileChangedReloadingStrategy;
+import org.apache.commons.configuration2.reloading.VFSFileMonitorReloadingStrategy;
 import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
 import org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine;
+import org.apache.commons.configuration2.event.ConfigurationEvent;
+import org.apache.commons.configuration2.event.ConfigurationListener;
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemManager;
+import org.apache.commons.vfs.VFS;
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileSystemOptions;
 
 /**
  * Test class for DefaultConfigurationBuilder.
@@ -36,7 +48,8 @@
  * @author Oliver Heger
  * @version $Id$
  */
-public class TestWebdavConfigurationBuilder extends TestCase implements FileOptionsProvider
+public class TestWebdavConfigurationBuilder extends TestCase implements FileOptionsProvider,
+        ConfigurationListener
 {
     /** Test configuration definition file. */
     private static final String TEST_FILE =
@@ -75,6 +88,15 @@
     private static final String MULTI_TENENT_FILE =
             "testMultiTenentConfigurationBuilder.xml";
 
+    private static final String FILEMONITOR2_FILE =
+            "testFileMonitorConfigurationBuilder2.xml";
+
+    private static final String FILEMONITOR_1001_FILE =
+            "testwrite/testMultiConfiguration_1001.xml";
+
+    private static final String FILEMONITOR_1002_FILE =
+            "testwrite/testMultiConfiguration_1002.xml";
+
     private static final String TEST_PROPERTIES = "test.properties.xml";
 
     private static final String TEST_SAVE = "testsave.xml";
@@ -86,6 +108,9 @@
 
     private Map options;
 
+    /** true when a file is changed */
+    private boolean configChanged = false;
+
     private String getBasePath()
     {
         String path = System.getProperty("test.webdav.base");
@@ -114,7 +139,7 @@
     {
         FileSystem.resetDefaultFileSystem();
         super.tearDown();
-    }    
+    }
 
     /**
      * Tests the isReservedNode() method of ConfigurationDeclaration.
@@ -855,6 +880,65 @@
         verify("1005", config, 50);
     }
 
+    public void testFileMonitor1() throws Exception
+    {
+        // create a new configuration
+        File input = new File("target/test-classes/testMultiConfiguration_1001.xml");
+        FileObject output = getFile(getBasePath() + FILEMONITOR_1001_FILE);
+        output.delete();
+        output.getParent().createFolder();
+        copyFile(input, output);
+
+        factory.setFileName(getBasePath() + FILEMONITOR2_FILE);
+        System.getProperties().remove("Id");
+
+        CombinedConfiguration config = factory.getConfiguration(true);
+        assertNotNull(config);
+        config.addConfigurationListener(this);
+        verify("1001", config, 15);
+
+        // Allow time for FileMonitor to set up.
+        Thread.sleep(1000);
+        XMLConfiguration x = new XMLConfiguration(getBasePath() + FILEMONITOR_1001_FILE);
+        x.setProperty("rowsPerPage", "50");
+        x.save();
+        // Let FileMonitor detect the change.
+        //Thread.sleep(2000);
+        waitForChange();
+        verify("1001", config, 50);
+        output.delete();
+        VFSFileMonitorReloadingStrategy.stopMonitor();
+    }
+
+    public void testFileMonitor2() throws Exception
+    {
+        // create a new configuration
+        File input = new File("target/test-classes/testMultiConfiguration_1002.xml");
+        FileObject output = getFile(getBasePath() + FILEMONITOR_1002_FILE);
+        output.delete();
+
+        factory.setFileName(getBasePath() + FILEMONITOR2_FILE);
+        System.getProperties().remove("Id");
+
+        CombinedConfiguration config = factory.getConfiguration(true);
+        assertNotNull(config);
+        config.addConfigurationListener(this);
+
+        verify("1002", config, 50);
+        Thread.sleep(1000);
+
+        output.getParent().createFolder();
+        copyFile(input, output);
+
+        // Allow time for the monitor to notice the change.
+        //Thread.sleep(2000);
+        waitForChange();
+        verify("1002", config, 25);
+        output.delete();
+        VFSFileMonitorReloadingStrategy.stopMonitor();
+    }
+
+
     private void verify(String key, CombinedConfiguration config, int rows)
     {
         System.setProperty("Id", key);
@@ -881,4 +965,62 @@
         ConfigurationAssert.assertEquals(conf, newConfig);
     }
 
+    private FileObject getFile(String fileName) throws Exception
+    {
+        FileSystemManager manager = VFS.getManager();
+        FileName file = manager.resolveURI(fileName);
+        FileName base = file.getParent();
+        FileName path = manager.resolveName(base, file.getBaseName());
+        FileSystemOptions opts = new FileSystemOptions();
+        return manager.resolveFile(path.getURI(), opts);
+    }
+
+    private void copyFile(File input, FileObject output) throws IOException
+    {
+        Reader reader = new FileReader(input);
+        Writer writer = new OutputStreamWriter(output.getContent().getOutputStream());
+        char[] buffer = new char[4096];
+        int n = 0;
+        while (-1 != (n = reader.read(buffer)))
+        {
+            writer.write(buffer, 0, n);
+        }
+        reader.close();
+        writer.close();
+    }
+
+    private void waitForChange()
+    {
+        synchronized(this)
+        {
+            try
+            {
+                int count = 0;
+                while (!configChanged && count++ <= 3)
+                {
+                    this.wait(5000);
+                }
+            }
+            catch (InterruptedException ie)
+            {
+                throw new IllegalStateException("wait timed out");
+            }
+            finally
+            {
+                configChanged = false;
+            }
+        }
+    }
+
+    public void configurationChanged(ConfigurationEvent event)
+    {
+        if (event.getType() == AbstractFileConfiguration.EVENT_CONFIG_CHANGED)
+        {
+            synchronized(this)
+            {
+                configChanged = true;
+                this.notify();
+            }
+        }
+    }
 }



Mime
View raw message