You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/03/24 21:20:54 UTC

svn commit: r1460449 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/SystemConfiguration.java test/java/org/apache/commons/configuration/TestSystemConfiguration.java

Author: oheger
Date: Sun Mar 24 20:20:54 2013
New Revision: 1460449

URL: http://svn.apache.org/r1460449
Log:
Adapted SystemConfiguration to changes on PropertiesConfiguration.
Now a different way of loading the configuration file is used.
The interface was slightly redesigned.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SystemConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SystemConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SystemConfiguration.java?rev=1460449&r1=1460448&r2=1460449&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SystemConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SystemConfiguration.java Sun Mar 24 20:20:54 2013
@@ -19,6 +19,7 @@ package org.apache.commons.configuration
 
 import java.util.Iterator;
 
+import org.apache.commons.configuration.io.FileHandler;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -45,42 +46,50 @@ public class SystemConfiguration extends
     }
 
     /**
-     * The method allows system properties to be set from a property file.
+     * Sets system properties from a file specified by its file name. This is
+     * just a short cut for {@code setSystemProperties(null, fileName)}.
+     *
      * @param fileName The name of the property file.
      * @throws Exception if an error occurs.
      * @since 1.6
      */
-    public static void setSystemProperties(String fileName) throws Exception
+    public static void setSystemProperties(String fileName)
+            throws ConfigurationException
     {
         setSystemProperties(null, fileName);
     }
 
     /**
-     * The method allows system properties to be set from a property file.
+     * Sets system properties from a file specified using its base path and
+     * file name. The file can either be a properties file or an XML properties
+     * file. It is loaded, and all properties it contains are added to system
+     * properties.
+     *
      * @param basePath The base path to look for the property file.
      * @param fileName The name of the property file.
-     * @throws Exception if an error occurs.
+     * @throws ConfigurationException if an error occurs.
      * @since 1.6
      */
-    public static void setSystemProperties(String basePath, String fileName) throws Exception
+    public static void setSystemProperties(String basePath, String fileName)
+            throws ConfigurationException
     {
-        PropertiesConfiguration config = fileName.endsWith(".xml")
-            ? new XMLPropertiesConfiguration() : new PropertiesConfiguration();
-        if (basePath != null)
-        {
-            config.setBasePath(basePath);
-        }
-        config.setFileName(fileName);
-        config.load();
+        FileBasedConfiguration config =
+                fileName.endsWith(".xml") ? new XMLPropertiesConfiguration()
+                        : new PropertiesConfiguration();
+
+        FileHandler handler = new FileHandler(config);
+        handler.setBasePath(basePath);
+        handler.setFileName(fileName);
+        handler.load();
         setSystemProperties(config);
     }
 
     /**
-     * Set System properties from a configuration file.
+     * Set System properties from a configuration object.
      * @param systemConfig The configuration containing the properties to be set.
      * @since 1.6
      */
-    public static void setSystemProperties(PropertiesConfiguration systemConfig)
+    public static void setSystemProperties(Configuration systemConfig)
     {
         Iterator<String> iter = systemConfig.getKeys();
         while (iter.hasNext())

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java?rev=1460449&r1=1460448&r2=1460449&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java Sun Mar 24 20:20:54 2013
@@ -18,10 +18,16 @@
 package org.apache.commons.configuration;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.Properties;
 
+import org.apache.commons.configuration.io.FileHandler;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 
 /**
  * Tests for {@code SystemConfiguration}.
@@ -31,6 +37,10 @@ import org.junit.Test;
  */
 public class TestSystemConfiguration
 {
+    /** An object for creating temporary files. */
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
     @Test
     public void testSystemConfiguration()
     {
@@ -49,4 +59,23 @@ public class TestSystemConfiguration
         SystemConfiguration.setSystemProperties(props);
         assertEquals("System Properties", "Apache", System.getProperty("test.name"));
     }
+
+    /**
+     * Tests whether system properties can be set from a configuration file.
+     */
+    @Test
+    public void testSetSystemPropertiesFromPropertiesFile()
+            throws ConfigurationException, IOException
+    {
+        File file = folder.newFile("sys.properties");
+        PropertiesConfiguration pconfig = new PropertiesConfiguration();
+        FileHandler handler = new FileHandler(pconfig);
+        pconfig.addProperty("fromFile", Boolean.TRUE);
+        handler.setFile(file);
+        handler.save();
+        SystemConfiguration.setSystemProperties(handler.getBasePath(),
+                handler.getFileName());
+        SystemConfiguration sconf = new SystemConfiguration();
+        assertTrue("Property from file not found", sconf.getBoolean("fromFile"));
+    }
 }