commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r949390 - in /commons/proper/configuration/trunk/src: changes/ java/org/apache/commons/configuration/ test/org/apache/commons/configuration/
Date Sat, 29 May 2010 14:07:38 GMT
Author: oheger
Date: Sat May 29 14:07:38 2010
New Revision: 949390

URL: http://svn.apache.org/viewvc?rev=949390&view=rev
Log:
CONFIGURATION-415: Files with a plus character in their names are now handled correctly. Copied
code from the FileUtils class of Commons IO to ConfigurationUtils.fileFromURL() to achieve
this.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=949390&r1=949389&r2=949390&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Sat May 29 14:07:38 2010
@@ -23,6 +23,9 @@
 
   <body>
     <release version="1.7" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-415">
+        Files with a plus character in their names are now handled correctly.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-413" due-to="Alexander Prishchepov">
         SubsetConfiguration now produces correct events.
       </action>

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=949390&r1=949389&r2=949390&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
Sat May 29 14:07:38 2010
@@ -21,12 +21,10 @@ import java.io.File;
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLDecoder;
 import java.util.Iterator;
 
 import org.apache.commons.configuration.event.ConfigurationErrorEvent;
@@ -62,9 +60,6 @@ public final class ConfigurationUtils
     /** Constant for the name of the clone() method.*/
     private static final String METHOD_CLONE = "clone";
 
-    /** Constant for the encoding for URLs. */
-    private static final String ENCODING = "UTF-8";
-
     /** The logger.*/
     private static Log log = LogFactory.getLog(ConfigurationUtils.class);
 
@@ -687,28 +682,33 @@ public final class ConfigurationUtils
 
     /**
      * Tries to convert the specified URL to a file object. If this fails,
-     * <b>null</b> is returned.
+     * <b>null</b> is returned. Note: This code has been copied from the
+     * <code>FileUtils</code> class from <em>Commons IO</em>.
      *
      * @param url the URL
      * @return the resulting file object
      */
     public static File fileFromURL(URL url)
     {
-        if (PROTOCOL_FILE.equals(url.getProtocol()))
+        if (url == null || !url.getProtocol().equals(PROTOCOL_FILE))
         {
-            try
-            {
-                return new File(URLDecoder.decode(url.getPath(), ENCODING));
-            }
-            catch (UnsupportedEncodingException uex)
-            {
-                // should not happen because UTF-8 should be supported
-                throw new AssertionError("Encoding not supported: " + uex);
-            }
+            return null;
         }
         else
         {
-            return null;
+            String filename = url.getFile().replace('/', File.separatorChar);
+            int pos = 0;
+            while ((pos = filename.indexOf('%', pos)) >= 0)
+            {
+                if (pos + 2 < filename.length())
+                {
+                    String hexStr = filename.substring(pos + 1, pos + 3);
+                    char ch = (char) Integer.parseInt(hexStr, 16);
+                    filename = filename.substring(0, pos) + ch
+                            + filename.substring(pos + 3);
+                }
+            }
+            return new File(filename);
         }
     }
 

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java?rev=949390&r1=949389&r2=949390&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
Sat May 29 14:07:38 2010
@@ -18,6 +18,7 @@
 package org.apache.commons.configuration;
 
 import java.io.File;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -194,6 +195,28 @@ public class TestConfigurationUtils exte
                 reference.getAbsolutePath()));
     }
 
+    /**
+     * Tests whether a "+" character in the file name is handled correctly by
+     * fileFromURL(). This test is related to CONFIGURATION-415.
+     */
+    public void testFileFromURLWithPlus() throws MalformedURLException
+    {
+        File file = new File(new File("target"), "foo+bar.txt")
+                .getAbsoluteFile();
+        URL fileURL = file.toURI().toURL();
+        File file2 = ConfigurationUtils.fileFromURL(fileURL);
+        assertEquals("Wrong file", file, file2);
+    }
+
+    /**
+     * Tests whether fileFromURL() handles null URLs correctly.
+     */
+    public void testFileFromURLNull() throws MalformedURLException
+    {
+        assertNull("Wrong file for null URL", ConfigurationUtils
+                .fileFromURL(null));
+    }
+
     public void testLocateWithNullTCCL() throws Exception
     {
         ClassLoader cl = Thread.currentThread().getContextClassLoader();

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?rev=949390&r1=949389&r2=949390&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
Sat May 29 14:07:38 2010
@@ -383,6 +383,29 @@ public class TestFileConfiguration exten
     }
 
     /**
+     * Tests whether file names containing a "+" character are handled
+     * correctly. This test is related to CONFIGURATION-415.
+     */
+    public void testPathWithPlus() throws ConfigurationException, IOException
+    {
+        File saveFile = new File(TARGET_DIR, "test+config.properties")
+                .getAbsoluteFile();
+        saveFile.createNewFile();
+        try
+        {
+            FileConfiguration config = new PropertiesConfiguration(saveFile);
+            config.addProperty("test", Boolean.TRUE);
+            config.save();
+            File configFile = config.getFile();
+            assertEquals("Wrong configuration file", saveFile, configFile);
+        }
+        finally
+        {
+            assertTrue("Could not remove test file", saveFile.delete());
+        }
+    }
+
+    /**
      * Tests the getFile() method.
      */
     public void testGetFile() throws ConfigurationException



Mime
View raw message