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/19 22:07:22 UTC

svn commit: r1458516 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/ test/java/org/apache/commons/configuration/

Author: oheger
Date: Tue Mar 19 21:07:22 2013
New Revision: 1458516

URL: http://svn.apache.org/r1458516
Log:
PropertiesConfigurationLayout no longer stores its associated configuration.

This makes it possible to create an instance independent from a
PropertiesConfiguration. It can be associated with a configuration later.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java?rev=1458516&r1=1458515&r2=1458516&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java Tue Mar 19 21:07:22 2013
@@ -237,7 +237,7 @@ public class PropertiesConfiguration ext
      */
     public PropertiesConfiguration()
     {
-        layout = createLayout();
+        layout = getLayout();
     }
 
     /**
@@ -398,7 +398,7 @@ public class PropertiesConfiguration ext
     {
         if (layout == null)
         {
-            layout = createLayout();
+            setLayout(createLayout());
         }
         return layout;
     }
@@ -426,6 +426,7 @@ public class PropertiesConfiguration ext
         {
             this.layout = layout;
         }
+        addConfigurationListener(this.layout);
     }
 
     /**
@@ -438,7 +439,7 @@ public class PropertiesConfiguration ext
      */
     protected PropertiesConfigurationLayout createLayout()
     {
-        return new PropertiesConfigurationLayout(this);
+        return new PropertiesConfigurationLayout();
     }
 
     /**
@@ -495,7 +496,7 @@ public class PropertiesConfiguration ext
 
         try
         {
-            getLayout().load(in);
+            getLayout().load(this, in);
         }
         finally
         {
@@ -519,7 +520,7 @@ public class PropertiesConfiguration ext
         enterNoReload();
         try
         {
-            getLayout().save(writer);
+            getLayout().save(this, writer);
         }
         finally
         {
@@ -556,7 +557,7 @@ public class PropertiesConfiguration ext
         PropertiesConfiguration copy = (PropertiesConfiguration) super.clone();
         if (layout != null)
         {
-            copy.setLayout(new PropertiesConfigurationLayout(copy, layout));
+            copy.setLayout(new PropertiesConfigurationLayout(layout));
         }
         return copy;
     }

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java?rev=1458516&r1=1458515&r2=1458516&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java Tue Mar 19 21:07:22 2013
@@ -122,11 +122,8 @@ public class PropertiesConfigurationLayo
     /** Constant for the default comment prefix. */
     private static final String COMMENT_PREFIX = "# ";
 
-    /** Stores the associated configuration object. */
-    private PropertiesConfiguration configuration;
-
     /** Stores a map with the contained layout information. */
-    private Map<String, PropertyLayoutData> layoutData;
+    private final Map<String, PropertyLayoutData> layoutData;
 
     /** Stores the header comment. */
     private String headerComment;
@@ -147,35 +144,22 @@ public class PropertiesConfigurationLayo
     private boolean forceSingleLine;
 
     /**
-     * Creates a new instance of {@code PropertiesConfigurationLayout}
-     * and initializes it with the associated configuration object.
-     *
-     * @param config the configuration (must not be <b>null</b>)
+     * Creates a new, empty instance of {@code PropertiesConfigurationLayout}.
      */
-    public PropertiesConfigurationLayout(PropertiesConfiguration config)
+    public PropertiesConfigurationLayout()
     {
-        this(config, null);
+        this(null);
     }
 
     /**
-     * Creates a new instance of {@code PropertiesConfigurationLayout}
-     * and initializes it with the given configuration object. The data of the
-     * specified layout object is copied.
+     * Creates a new instance of {@code PropertiesConfigurationLayout} and
+     * copies the data of the specified layout object.
      *
-     * @param config the configuration (must not be <b>null</b>)
      * @param c the layout object to be copied
      */
-    public PropertiesConfigurationLayout(PropertiesConfiguration config,
-            PropertiesConfigurationLayout c)
+    public PropertiesConfigurationLayout(PropertiesConfigurationLayout c)
     {
-        if (config == null)
-        {
-            throw new IllegalArgumentException(
-                    "Configuration must not be null!");
-        }
-        configuration = config;
         layoutData = new LinkedHashMap<String, PropertyLayoutData>();
-        config.addConfigurationListener(this);
 
         if (c != null)
         {
@@ -184,16 +168,6 @@ public class PropertiesConfigurationLayo
     }
 
     /**
-     * Returns the associated configuration object.
-     *
-     * @return the associated configuration
-     */
-    public PropertiesConfiguration getConfiguration()
-    {
-        return configuration;
-    }
-
-    /**
      * Returns the comment for the specified property key in a canonical form.
      * &quot;Canonical&quot; means that either all lines start with a comment
      * character or none. If the {@code commentChar} parameter is <b>false</b>,
@@ -498,26 +472,28 @@ public class PropertiesConfigurationLayo
 
     /**
      * Reads a properties file and stores its internal structure. The found
-     * properties will be added to the associated configuration object.
+     * properties will be added to the specified configuration object.
      *
+     * @param config the associated configuration object
      * @param in the reader to the properties file
      * @throws ConfigurationException if an error occurs
      */
-    public void load(Reader in) throws ConfigurationException
+    public void load(PropertiesConfiguration config, Reader in)
+            throws ConfigurationException
     {
         if (++loadCounter == 1)
         {
-            getConfiguration().removeConfigurationListener(this);
+            config.removeConfigurationListener(this);
         }
-        PropertiesConfiguration.PropertiesReader reader = getConfiguration()
-                .getIOFactory().createPropertiesReader(in,
-                        getConfiguration().getListDelimiter());
+        PropertiesConfiguration.PropertiesReader reader =
+                config.getIOFactory().createPropertiesReader(in,
+                        config.getListDelimiter());
 
         try
         {
             while (reader.nextProperty())
             {
-                if (getConfiguration().propertyLoaded(reader.getPropertyName(),
+                if (config.propertyLoaded(reader.getPropertyName(),
                         reader.getPropertyValue()))
                 {
                     boolean contained = layoutData.containsKey(reader
@@ -559,7 +535,7 @@ public class PropertiesConfigurationLayo
         {
             if (--loadCounter == 0)
             {
-                getConfiguration().addConfigurationListener(this);
+                config.addConfigurationListener(this);
             }
         }
     }
@@ -568,16 +544,17 @@ public class PropertiesConfigurationLayo
      * Writes the properties file to the given writer, preserving as much of its
      * structure as possible.
      *
+     * @param config the associated configuration object
      * @param out the writer
      * @throws ConfigurationException if an error occurs
      */
-    public void save(Writer out) throws ConfigurationException
+    public void save(PropertiesConfiguration config, Writer out) throws ConfigurationException
     {
         try
         {
-            char delimiter = getConfiguration().isDelimiterParsingDisabled() ? 0
-                    : getConfiguration().getListDelimiter();
-            PropertiesConfiguration.PropertiesWriter writer = getConfiguration()
+            char delimiter = config.isDelimiterParsingDisabled() ? 0
+                    : config.getListDelimiter();
+            PropertiesConfiguration.PropertiesWriter writer = config
                     .getIOFactory().createPropertiesWriter(out, delimiter);
             writer.setGlobalSeparator(getGlobalSeparator());
             if (getLineSeparator() != null)
@@ -593,7 +570,7 @@ public class PropertiesConfigurationLayo
 
             for (String key : layoutData.keySet())
             {
-                if (getConfiguration().containsKey(key))
+                if (config.containsKey(key))
                 {
 
                     // Output blank lines before property
@@ -607,9 +584,9 @@ public class PropertiesConfigurationLayo
 
                     // Output the property and its value
                     boolean singleLine = (isForceSingleLine() || isSingleLine(key))
-                            && !getConfiguration().isDelimiterParsingDisabled();
+                            && !config.isDelimiterParsingDisabled();
                     writer.setCurrentSeparator(getSeparator(key));
-                    writer.writeProperty(key, getConfiguration().getProperty(
+                    writer.writeProperty(key, config.getProperty(
                             key), singleLine);
                 }
             }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java?rev=1458516&r1=1458515&r2=1458516&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java Tue Mar 19 21:07:22 2013
@@ -725,7 +725,7 @@ public class TestPropertiesConfiguration
     @Test
     public void testPropertyLoaded() throws ConfigurationException
     {
-        DummyLayout layout = new DummyLayout(conf);
+        DummyLayout layout = new DummyLayout();
         conf.setLayout(layout);
         conf.propertyLoaded("layoutLoadedProperty", "yes");
         assertEquals("Layout's load() was called", 0, layout.loadCalls);
@@ -738,7 +738,7 @@ public class TestPropertiesConfiguration
     @Test
     public void testPropertyLoadedInclude() throws ConfigurationException
     {
-        DummyLayout layout = new DummyLayout(conf);
+        DummyLayout layout = new DummyLayout();
         conf.setLayout(layout);
         conf.propertyLoaded(PropertiesConfiguration.getInclude(), "testClasspath.properties,testEqual.properties");
         assertEquals("Layout's load() was not correctly called", 2, layout.loadCalls);
@@ -752,7 +752,7 @@ public class TestPropertiesConfiguration
     @Test
     public void testPropertyLoadedIncludeNotAllowed() throws ConfigurationException
     {
-        DummyLayout layout = new DummyLayout(conf);
+        DummyLayout layout = new DummyLayout();
         conf.setLayout(layout);
         conf.setIncludesAllowed(false);
         conf.propertyLoaded(PropertiesConfiguration.getInclude(), "testClassPath.properties,testEqual.properties");
@@ -1208,13 +1208,9 @@ public class TestPropertiesConfiguration
         /** Stores the number how often load() was called. */
         public int loadCalls;
 
-        public DummyLayout(PropertiesConfiguration config)
-        {
-            super(config);
-        }
-
         @Override
-        public void load(Reader in) throws ConfigurationException
+        public void load(PropertiesConfiguration config, Reader in)
+                throws ConfigurationException
         {
             loadCalls++;
         }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java?rev=1458516&r1=1458515&r2=1458516&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java Tue Mar 19 21:07:22 2013
@@ -72,7 +72,7 @@ public class TestPropertiesConfiguration
     public void setUp() throws Exception
     {
         config = new LayoutTestConfiguration();
-        layout = new PropertiesConfigurationLayout(config);
+        layout = new PropertiesConfigurationLayout();
         config.setLayout(layout);
         builder = new PropertiesBuilder();
     }
@@ -89,20 +89,18 @@ public class TestPropertiesConfiguration
         assertTrue("No event listener registered", it.hasNext());
         assertSame("Layout not registered as event listener", layout, it.next());
         assertFalse("Multiple event listeners registered", it.hasNext());
-        assertSame("Configuration not stored", config, layout
-                .getConfiguration());
         assertFalse("Force single line flag set", layout.isForceSingleLine());
         assertNull("Got a global separator", layout.getGlobalSeparator());
     }
 
     /**
-     * Tests creating a layout object with a null configuration. This should
-     * cause an exception.
+     * Tests the copy constructor if no other layout object is passed.
      */
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testInitNull()
     {
-        new PropertiesConfigurationLayout(null);
+        layout = new PropertiesConfigurationLayout(null);
+        assertTrue("Object contains keys", layout.getKeys().isEmpty());
     }
 
     /**
@@ -113,7 +111,7 @@ public class TestPropertiesConfiguration
     {
         builder.addComment(TEST_COMMENT);
         builder.addProperty(TEST_KEY, TEST_VALUE);
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertNull("A header comment was found", layout.getHeaderComment());
         assertEquals("Wrong number of properties", 1, layout.getKeys().size());
         assertTrue("Property not found", layout.getKeys().contains(TEST_KEY));
@@ -138,7 +136,7 @@ public class TestPropertiesConfiguration
         builder.addComment(TEST_COMMENT);
         builder.addComment(null);
         builder.addProperty(TEST_KEY, TEST_VALUE);
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertEquals("Wrong number of blanc lines", 2, layout
                 .getBlancLinesBefore(TEST_KEY));
         assertEquals("Wrong comment", TEST_COMMENT + CRNORM, layout
@@ -154,7 +152,7 @@ public class TestPropertiesConfiguration
     public void testIsSingleLine() throws ConfigurationException
     {
         builder.addProperty(TEST_KEY, TEST_VALUE + "," + TEST_VALUE + "2");
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertTrue("Wrong single line flag", layout.isSingleLine(TEST_KEY));
         assertEquals("Wrong number of values", 2, config.getList(TEST_KEY)
                 .size());
@@ -169,7 +167,7 @@ public class TestPropertiesConfiguration
         builder.addProperty(TEST_KEY, TEST_VALUE);
         builder.addProperty("anotherProp", "a value");
         builder.addProperty(TEST_KEY, TEST_VALUE + "2");
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertFalse("Wrong single line flag", layout.isSingleLine(TEST_KEY));
         assertEquals("Wrong number of values", 2, config.getList(TEST_KEY)
                 .size());
@@ -186,7 +184,7 @@ public class TestPropertiesConfiguration
         builder.addComment(null);
         builder.addComment(TEST_COMMENT);
         builder.addProperty(TEST_KEY, TEST_VALUE + "2");
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertEquals("Wrong combined comment",
                 TEST_COMMENT + CRNORM + TEST_COMMENT, layout.getCanonicalComment(
                         TEST_KEY, false));
@@ -203,7 +201,7 @@ public class TestPropertiesConfiguration
         builder.addComment(TEST_COMMENT);
         builder.addComment(null);
         builder.addProperty(TEST_KEY, TEST_VALUE);
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertEquals("Wrong header comment", TEST_COMMENT, layout
                 .getCanonicalHeaderComment(false));
         assertNull("Wrong comment for property", layout.getCanonicalComment(
@@ -221,7 +219,7 @@ public class TestPropertiesConfiguration
         builder.addComment(TEST_COMMENT);
         builder.addComment(null);
         builder.addProperty(TEST_KEY, TEST_VALUE);
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertEquals("Wrong header comment", TEST_COMMENT + CRNORM + CRNORM
                 + TEST_COMMENT, layout.getCanonicalHeaderComment(false));
         assertNull("Wrong comment for property", layout.getComment(TEST_KEY));
@@ -241,7 +239,7 @@ public class TestPropertiesConfiguration
         builder.addComment(null);
         builder.addComment(TEST_COMMENT);
         builder.addProperty(TEST_KEY, TEST_VALUE);
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         assertEquals("Wrong header comment", TEST_COMMENT + CRNORM + CRNORM
                 + TEST_COMMENT, layout.getCanonicalHeaderComment(false));
         assertEquals("Wrong comment for property", TEST_COMMENT, layout
@@ -301,7 +299,7 @@ public class TestPropertiesConfiguration
     {
         builder.addComment(TEST_COMMENT);
         builder.addProperty(TEST_KEY, TEST_VALUE);
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         ConfigurationEvent event = new ConfigurationEvent(this,
                 AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_KEY, TEST_VALUE,
                 false);
@@ -423,7 +421,7 @@ public class TestPropertiesConfiguration
         builder.addComment("Include file");
         builder.addProperty(PropertiesConfiguration.getInclude(), "test");
 
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
 
         assertEquals("Wrong header comment", "Header comment", layout
                 .getCanonicalHeaderComment(false));
@@ -451,7 +449,7 @@ public class TestPropertiesConfiguration
         builder.addComment(null);
         builder.addComment("Another comment");
         builder.addProperty("property", "and a value");
-        layout.load(builder.getReader());
+        layout.load(config, builder.getReader());
         checkLayoutString(builder.toString());
     }
 
@@ -597,8 +595,7 @@ public class TestPropertiesConfiguration
     public void testInitCopy()
     {
         fillLayout();
-        PropertiesConfigurationLayout l2 = new PropertiesConfigurationLayout(
-                config, layout);
+        PropertiesConfigurationLayout l2 = new PropertiesConfigurationLayout(layout);
         assertEquals("Wrong number of keys", layout.getKeys().size(), l2
                 .getKeys().size());
         for (String key : layout.getKeys())
@@ -618,8 +615,7 @@ public class TestPropertiesConfiguration
     public void testInitCopyModify()
     {
         fillLayout();
-        PropertiesConfigurationLayout l2 = new PropertiesConfigurationLayout(
-                config, layout);
+        PropertiesConfigurationLayout l2 = new PropertiesConfigurationLayout(layout);
         assertEquals("Comments are not equal", layout.getComment(TEST_KEY), l2
                 .getComment(TEST_KEY));
         layout.setComment(TEST_KEY, "A new comment");
@@ -705,7 +701,7 @@ public class TestPropertiesConfiguration
         builder.addComment("A footer comment");
         try
         {
-            layout.load(builder.getReader());
+            layout.load(config, builder.getReader());
         }
         catch (ConfigurationException cex)
         {
@@ -723,7 +719,7 @@ public class TestPropertiesConfiguration
     private String getLayoutString() throws ConfigurationException
     {
         StringWriter out = new StringWriter();
-        layout.save(out);
+        layout.save(config, out);
         return out.toString();
     }
 
@@ -832,7 +828,7 @@ public class TestPropertiesConfiguration
             {
                 if (PropertiesConfiguration.getInclude().equals(key))
                 {
-                    getLayout().load(builder.getReader());
+                    getLayout().load(this, builder.getReader());
                     return false;
                 }
                 else