Author: ebourg
Date: Tue May 22 05:37:13 2007
New Revision: 540562

URL: http://svn.apache.org/viewvc?view=rev&rev=540562
Log:
Added a test case for saving a configuration to a web server using a PUT 
request (CONFIGURATION-249)
Minor code simplification in AbstractFileConfiguration

Modified:
    jakarta/commons/proper/configuration/trunk/project.xml
    
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java

Modified: jakarta/commons/proper/configuration/trunk/project.xml
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/project.xml?view=diff&rev=540562&r1=540561&r2=540562
==============================================================================
--- jakarta/commons/proper/configuration/trunk/project.xml (original)
+++ jakarta/commons/proper/configuration/trunk/project.xml Tue May 22 05:37:13 
2007
@@ -418,6 +418,26 @@
     </dependency>
 
     <dependency>
+      <groupId>jetty</groupId>
+      <artifactId>jetty</artifactId>
+      <version>6.0.2</version>
+      <properties>
+        <scope>test</scope>
+      </properties>
+    </dependency>
+
+    <dependency>
+      <groupId>jetty</groupId>
+      <artifactId>jetty-util</artifactId>
+      <version>6.0.2</version>
+      <properties>
+        <scope>test</scope>
+      </properties>
+    </dependency>
+
+    <!-- Plugins -->
+
+    <dependency>
       <groupId>maven-plugins</groupId>
       <artifactId>maven-tasks-plugin</artifactId>
       <version>1.1.0</version>

Modified: 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?view=diff&rev=540562&r1=540561&r2=540562
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
 Tue May 22 05:37:13 2007
@@ -621,16 +621,13 @@
         {
             return null;
         }
+        else if (sourceURL != null)
+        {
+            return ConfigurationUtils.fileFromURL(sourceURL);
+        }
         else
         {
-            if (sourceURL != null)
-            {
-                return ConfigurationUtils.fileFromURL(sourceURL);
-            }
-            else
-            {
-                return ConfigurationUtils.getFile(getBasePath(), 
getFileName());
-            }
+            return ConfigurationUtils.getFile(getBasePath(), getFileName());
         }
     }
 

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java?view=diff&rev=540562&r1=540561&r2=540562
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
 Tue May 22 05:37:13 2007
@@ -18,6 +18,8 @@
 package org.apache.commons.configuration;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -28,10 +30,18 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
 
 import junit.framework.TestCase;
+import org.mortbay.jetty.Handler;
+import org.mortbay.jetty.Request;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.handler.AbstractHandler;
+import org.mortbay.util.IO;
 
 /**
  * Test for loading and saving properties files.
@@ -172,6 +182,66 @@
         assertEquals("true", config2.getString("configuration.loaded"));       
 
     }
 
+    public void testSaveToHTTPServer() throws Exception
+    {
+        // set up the web server
+        Handler handler = new AbstractHandler()
+        {
+            public void handle(String target, HttpServletRequest request, 
HttpServletResponse response, int dispatch) throws IOException, ServletException
+            {
+                File file = new File("." + target);
+
+                if ("GET".equals(request.getMethod())) {
+                    if (file.exists() && file.isFile()) {
+                        response.setStatus(HttpServletResponse.SC_OK);
+                        response.setContentType("text/plain");
+                        FileInputStream in = new FileInputStream(file);
+                        try
+                        {
+                            IO.copy(in, response.getOutputStream());
+                        }
+                        finally
+                        {
+                            in.close();
+                        }
+
+                    } else {
+                        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+                    }
+
+                } else if ("PUT".equals(request.getMethod())) {
+                    FileOutputStream out = new FileOutputStream(file);
+                    try
+                    {
+                        IO.copy(request.getInputStream(), out);
+                    }
+                    finally
+                    {
+                        out.close();
+                    }
+
+                    response.setStatus(HttpServletResponse.SC_OK);
+                } else {
+                    response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
+                }
+
+                ((Request) request).setHandled(true);
+            }
+        };
+
+        Server server = new Server(65432);
+        server.setHandler(handler);
+        server.start();
+
+        // save the configuration
+        URL url = new 
URL("http://localhost:65432/target/testsave-httpput.properties";);
+        conf.save(url);
+
+        // reload the configuration
+        Configuration config2 = new PropertiesConfiguration(url);
+        assertEquals("true", config2.getString("configuration.loaded"));       
 
+    }
+
     public void testInMemoryCreatedSave() throws Exception
     {
         // remove the file previously saved if necessary
@@ -201,10 +271,8 @@
         for (Iterator i = pc.getKeys(); i.hasNext();)
         {
             String key = (String) i.next();
-            assertTrue("The saved configuration doesn't contain the key '" + 
key + "'",
-                    checkConfig.containsKey(key));
-            assertEquals("Value of the '" + key + "' property",
-                    pc.getProperty(key), checkConfig.getProperty(key));
+            assertTrue("The saved configuration doesn't contain the key '" + 
key + "'", checkConfig.containsKey(key));
+            assertEquals("Value of the '" + key + "' property", 
pc.getProperty(key), checkConfig.getProperty(key));
         }
 
         // Save it again, verifing a save with a filename works.
@@ -349,8 +417,7 @@
         setUpSavedProperties();
         conf.clearProperty("c");
         PropertiesConfiguration checkConfig = checkSavedConfig();
-        assertFalse("The saved configuration contain the key '" + "c" + "'",
-                checkConfig.containsKey("c"));
+        assertFalse("The saved configuration contain the key '" + "c" + "'", 
checkConfig.containsKey("c"));
     }
 
     /**
@@ -359,8 +426,7 @@
      *
      * @throws IOException if an I/O error occurs
      */
-    private void setUpSavedProperties() throws IOException,
-            ConfigurationException
+    private void setUpSavedProperties() throws IOException, 
ConfigurationException
     {
         PrintWriter out = null;
 
@@ -400,15 +466,12 @@
     private PropertiesConfiguration checkSavedConfig()
             throws ConfigurationException
     {
-        PropertiesConfiguration checkConfig = new PropertiesConfiguration(
-                testSavePropertiesFile);
+        PropertiesConfiguration checkConfig = new 
PropertiesConfiguration(testSavePropertiesFile);
         for (Iterator i = conf.getKeys(); i.hasNext();)
         {
             String key = (String) i.next();
-            assertTrue("The saved configuration doesn't contain the key '"
-                    + key + "'", checkConfig.containsKey(key));
-            assertEquals("Value of the '" + key + "' property", conf
-                    .getProperty(key), checkConfig.getProperty(key));
+            assertTrue("The saved configuration doesn't contain the key '" + 
key + "'", checkConfig.containsKey(key));
+            assertEquals("Value of the '" + key + "' property", 
conf.getProperty(key), checkConfig.getProperty(key));
         }
         return checkConfig;
     }
@@ -575,8 +638,7 @@
         String content = out.toString();
         assertTrue("Header could not be found", content.indexOf("# My header"
                 + EOL + EOL) == 0);
-        assertTrue("Property could not be found", content
-                .indexOf("prop = value" + EOL) > 0);
+        assertTrue("Property could not be found", content.indexOf("prop = 
value" + EOL) > 0);
     }
 
     /**
@@ -619,8 +681,7 @@
         conf.setLayout(layout);
         conf.propertyLoaded("layoutLoadedProperty", "yes");
         assertEquals("Layout's load() was called", 0, layout.loadCalls);
-        assertEquals("Property not added", "yes", conf
-                .getString("layoutLoadedProperty"));
+        assertEquals("Property not added", "yes", 
conf.getString("layoutLoadedProperty"));
     }
 
     /**
@@ -630,29 +691,23 @@
     {
         DummyLayout layout = new DummyLayout(conf);
         conf.setLayout(layout);
-        conf.propertyLoaded(PropertiesConfiguration.getInclude(),
-                "testClasspath.properties,testEqual.properties");
-        assertEquals("Layout's load() was not correctly called", 2,
-                layout.loadCalls);
-        assertFalse("Property was added", conf
-                .containsKey(PropertiesConfiguration.getInclude()));
+        conf.propertyLoaded(PropertiesConfiguration.getInclude(), 
"testClasspath.properties,testEqual.properties");
+        assertEquals("Layout's load() was not correctly called", 2, 
layout.loadCalls);
+        assertFalse("Property was added", 
conf.containsKey(PropertiesConfiguration.getInclude()));
     }
 
     /**
      * Tests propertyLoaded() for an include property, when includes are
      * disabled.
      */
-    public void testPropertyLoadedIncludeNotAllowed()
-            throws ConfigurationException
+    public void testPropertyLoadedIncludeNotAllowed() throws 
ConfigurationException
     {
         DummyLayout layout = new DummyLayout(conf);
         conf.setLayout(layout);
         conf.setIncludesAllowed(false);
-        conf.propertyLoaded(PropertiesConfiguration.getInclude(),
-                "testClassPath.properties,testEqual.properties");
+        conf.propertyLoaded(PropertiesConfiguration.getInclude(), 
"testClassPath.properties,testEqual.properties");
         assertEquals("Layout's load() was called", 0, layout.loadCalls);
-        assertFalse("Property was added", conf
-                .containsKey(PropertiesConfiguration.getInclude()));
+        assertFalse("Property was added", 
conf.containsKey(PropertiesConfiguration.getInclude()));
     }
 
     /**
@@ -660,16 +715,11 @@
      */
     public void testIsCommentLine()
     {
-        assertTrue("Comment not detected", PropertiesConfiguration
-                .isCommentLine("# a comment"));
-        assertTrue("Alternative comment not detected", PropertiesConfiguration
-                .isCommentLine("! a comment"));
-        assertTrue("Comment with no space not detected",
-                PropertiesConfiguration.isCommentLine("#a comment"));
-        assertTrue("Comment with leading space not detected",
-                PropertiesConfiguration.isCommentLine("    ! a comment"));
-        assertFalse("Wrong comment", PropertiesConfiguration
-                .isCommentLine("   a#comment"));
+        assertTrue("Comment not detected", 
PropertiesConfiguration.isCommentLine("# a comment"));
+        assertTrue("Alternative comment not detected", 
PropertiesConfiguration.isCommentLine("! a comment"));
+        assertTrue("Comment with no space not detected", 
PropertiesConfiguration.isCommentLine("#a comment"));
+        assertTrue("Comment with leading space not detected", 
PropertiesConfiguration.isCommentLine("    ! a comment"));
+        assertFalse("Wrong comment", PropertiesConfiguration.isCommentLine("   
a#comment"));
     }
 
     /**
@@ -679,22 +729,16 @@
     public void testClone() throws ConfigurationException
     {
         PropertiesConfiguration copy = (PropertiesConfiguration) conf.clone();
-        assertNotSame("Copy has same layout object", conf.getLayout(), copy
-                .getLayout());
-        assertEquals("Wrong number of event listeners for original", 1, conf
-                .getConfigurationListeners().size());
-        assertEquals("Wrong number of event listeners for clone", 1, copy
-                .getConfigurationListeners().size());
-        assertSame("Wrong event listener for original", conf.getLayout(), conf
-                .getConfigurationListeners().iterator().next());
-        assertSame("Wrong event listener for clone", copy.getLayout(), copy
-                .getConfigurationListeners().iterator().next());
+        assertNotSame("Copy has same layout object", conf.getLayout(), 
copy.getLayout());
+        assertEquals("Wrong number of event listeners for original", 1, 
conf.getConfigurationListeners().size());
+        assertEquals("Wrong number of event listeners for clone", 1, 
copy.getConfigurationListeners().size());
+        assertSame("Wrong event listener for original", conf.getLayout(), 
conf.getConfigurationListeners().iterator().next());
+        assertSame("Wrong event listener for clone", copy.getLayout(), 
copy.getConfigurationListeners().iterator().next());
         StringWriter outConf = new StringWriter();
         conf.save(outConf);
         StringWriter outCopy = new StringWriter();
         copy.save(outCopy);
-        assertEquals("Output from copy is different", outConf.toString(),
-                outCopy.toString());
+        assertEquals("Output from copy is different", outConf.toString(), 
outCopy.toString());
     }
 
     /**
@@ -704,8 +748,7 @@
     {
         conf = new PropertiesConfiguration();
         PropertiesConfiguration copy = (PropertiesConfiguration) conf.clone();
-        assertNotSame("Layout objects are the same", conf.getLayout(), copy
-                .getLayout());
+        assertNotSame("Layout objects are the same", conf.getLayout(), 
copy.getLayout());
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to