Author: oheger
Date: Wed Jun 21 13:37:04 2006
New Revision: 416102

URL: http://svn.apache.org/viewvc?rev=416102&view=rev
Log:
ConfigurationUtils.locate() now takes the path into account when trying to 
locate a file from classpath; fix for issue CONFIGURATION-216

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

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?rev=416102&r1=416101&r2=416102&view=diff
==============================================================================
--- 
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
 Wed Jun 21 13:37:04 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2005 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License")
  * you may not use this file except in compliance with the License.
@@ -127,7 +127,7 @@
         this();
 
         // store the file name
-        setPath(fileName);
+        setFileName(fileName);
 
         // load the file
         load();

Modified: 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=416102&r1=416101&r2=416102&view=diff
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
 Wed Jun 21 13:37:04 2006
@@ -46,6 +46,9 @@
     /** Constant for the file URL protocol.*/
     static final String PROTOCOL_FILE = "file";
 
+    /** Constant for the resource path separator.*/
+    static final String RESOURCE_PATH_SEPARATOR = "/";
+
     /** The logger.*/
     private static Log log = LogFactory.getLog(ConfigurationUtils.class);
 
@@ -401,17 +404,30 @@
             }
         }
 
+        String resourceName = null;
+        if (url == null)
+        {
+            if (base != null)
+            {
+                resourceName = base + RESOURCE_PATH_SEPARATOR + name;
+            }
+            else
+            {
+                resourceName = name;
+            }
+        }
+
         // attempt to load from the context classpath
         if (url == null)
         {
             ClassLoader loader = 
Thread.currentThread().getContextClassLoader();
             if (loader != null)
             {
-                url = loader.getResource(name);
+                url = loader.getResource(resourceName);
 
                 if (url != null)
                 {
-                    log.debug("Configuration loaded from the context classpath 
(" + name + ")");
+                    log.debug("Configuration loaded from the context classpath 
(" + resourceName + ")");
                 }
             }
         }
@@ -419,11 +435,11 @@
         // attempt to load from the system classpath
         if (url == null)
         {
-            url = ClassLoader.getSystemResource(name);
+            url = ClassLoader.getSystemResource(resourceName);
 
             if (url != null)
             {
-                log.debug("Configuration loaded from the system classpath (" + 
name + ")");
+                log.debug("Configuration loaded from the system classpath (" + 
resourceName + ")");
             }
         }
 

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?rev=416102&r1=416101&r2=416102&view=diff
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
 Wed Jun 21 13:37:04 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2005 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License")
  * you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration;
 
 import java.net.URL;
+import java.util.Iterator;
 import java.util.Properties;
 import java.io.File;
 import java.io.FileInputStream;
@@ -37,6 +38,8 @@
 {
     private static final File TARGET_DIR = new File("target");
 
+    private static final String RESOURCE_NAME = 
"config/deep/deeptest.properties";
+
     public void testSetURL() throws Exception
     {
         // http URL
@@ -472,6 +475,26 @@
         catch (ConfigurationException e)
         {
             // ok
+        }
+    }
+
+    /**
+     * Tests whether the constructor behaves the same as setFileName() when the
+     * configuration source is in the classpath.
+     */
+    public void testInitFromClassPath() throws ConfigurationException
+    {
+        PropertiesConfiguration config1 = new PropertiesConfiguration();
+        config1.setFileName(RESOURCE_NAME);
+        config1.load();
+        PropertiesConfiguration config2 = new PropertiesConfiguration(
+                RESOURCE_NAME);
+
+        for (Iterator it = config1.getKeys(); it.hasNext();)
+        {
+            String key = (String) it.next();
+            assertEquals("Wrong value for key " + key,
+                    config1.getProperty(key), config2.getProperty(key));
         }
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=416102&r1=416101&r2=416102&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Wed Jun 21 
13:37:04 2006
@@ -23,6 +23,11 @@
   <body>
 
     <release version="1.3-SNAPSHOT" date="in SVN">
+      <action dev="oheger" type="update" issue="CONFIGURATION-216">
+        If a configuration file was to be loaded from classpath, the
+        constructor of AbstractFileConfiguration dropped the file's path. The
+        path is now taken into account.
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-214">
         The getter methods for numeric data types in AbstractConfiguration now
         support conversions between different Number types, e.g. you can now



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

Reply via email to