commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r411583 - in /jakarta/commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/SubnodeConfiguration.java test/org/apache/commons/configuration/TestSubnodeConfiguration.java
Date Sun, 04 Jun 2006 16:46:41 GMT
Author: oheger
Date: Sun Jun  4 09:46:41 2006
New Revision: 411583

URL: http://svn.apache.org/viewvc?rev=411583&view=rev
Log:
Modified SubnodeConfiguration to be more independend from its parent configuration; the settings
are no longer shared. Interpolation will now always be delegated to the parent.

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java?rev=411583&r1=411582&r2=411583&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java
Sun Jun  4 09:46:41 2006
@@ -16,7 +16,6 @@
 package org.apache.commons.configuration;
 
 import org.apache.commons.configuration.tree.ConfigurationNode;
-import org.apache.commons.configuration.tree.ExpressionEngine;
 
 /**
  * <p>
@@ -43,12 +42,13 @@
  * configuration's root node is involved.
  * </p>
  * <p>
- * A subnode configuration and its parent configuration work very close
- * together. For instance, some flags used by configuration objects (e.g. the
+ * When a subnode configuration is created, it inherits the settings of its
+ * parent configuration, e.g. some flags like the
  * <code>throwExceptionOnMissing</code> flag or the settings for handling list
- * delimiters) are shared between both. This means if one of these properties is
- * modified for the subnode configuration, it is also changed for the parent and
- * vice versa.
+ * delimiters) or the expression engine. If these settings are changed later in
+ * either the subnode or the parent configuration, the changes are not visible
+ * for each other. So you could create a subnode configuration, change its
+ * expression engine without affecting the parent configuration.
  * </p>
  * <p>
  * From its purpose this class is quite similar to
@@ -91,6 +91,7 @@
 
         setRootNode(root);
         this.parent = parent;
+        initFromParent(parent);
     }
 
     /**
@@ -104,92 +105,6 @@
     }
 
     /**
-     * Returns the expression engine. This method returns the parent's
-     * expression engine.
-     *
-     * @return the expression engine
-     */
-    public ExpressionEngine getExpressionEngine()
-    {
-        return getParent().getExpressionEngine();
-    }
-
-    /**
-     * Sets the expression engine. This method sets the expression engine at the
-     * parent.
-     *
-     * @param expressionEngine the new expression engine
-     */
-    public void setExpressionEngine(ExpressionEngine expressionEngine)
-    {
-        getParent().setExpressionEngine(expressionEngine);
-    }
-
-    /**
-     * Returns the list delimiter. This method returns the parent's delimiter.
-     *
-     * @return the list delimiter
-     */
-    public char getListDelimiter()
-    {
-        return getParent().getListDelimiter();
-    }
-
-    /**
-     * Sets the list delimiter. The delimiter will also be set for the parent.
-     *
-     * @param listDelimiter the new delimiter
-     */
-    public void setListDelimiter(char listDelimiter)
-    {
-        getParent().setListDelimiter(listDelimiter);
-    }
-
-    /**
-     * Returns a flag if list properties should be splitted when they are added.
-     * This implementation returns the corresponding flag of the parent.
-     *
-     * @return the delimiter parsing flag
-     */
-    public boolean isDelimiterParsingDisabled()
-    {
-        return getParent().isDelimiterParsingDisabled();
-    }
-
-    /**
-     * Sets the delimiter parsing disabled flag. This method will also set this
-     * flag at the parent.
-     *
-     * @param delimiterParsingDisabled the new value of the flag
-     */
-    public void setDelimiterParsingDisabled(boolean delimiterParsingDisabled)
-    {
-        getParent().setDelimiterParsingDisabled(delimiterParsingDisabled);
-    }
-
-    /**
-     * Returns the throw exception on missing flag. This implementation returns
-     * the value of the corresponding flag of the parent.
-     *
-     * @return the throw exception on missing flag
-     */
-    public boolean isThrowExceptionOnMissing()
-    {
-        return getParent().isThrowExceptionOnMissing();
-    }
-
-    /**
-     * Sets the throw exception on missing flag. The value is also set for the
-     * parent.
-     *
-     * @param throwExceptionOnMissing the new value of the flag
-     */
-    public void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
-    {
-        getParent().setThrowExceptionOnMissing(throwExceptionOnMissing);
-    }
-
-    /**
      * Returns a hierarchical configuration object for the given sub node.
      * This implementation will ensure that the returned
      * <code>SubnodeConfiguration</code> object will have the same parent than
@@ -212,5 +127,32 @@
     protected Node createNode(String name)
     {
         return getParent().createNode(name);
+    }
+
+    /**
+     * Initializes this subnode configuration from the given parent
+     * configuration. This method is called by the constructor. It will copy
+     * many settings from the parent.
+     *
+     * @param parentConfig the parent configuration
+     */
+    protected void initFromParent(HierarchicalConfiguration parentConfig)
+    {
+        setExpressionEngine(parentConfig.getExpressionEngine());
+        setListDelimiter(parentConfig.getListDelimiter());
+        setDelimiterParsingDisabled(parentConfig.isDelimiterParsingDisabled());
+        setThrowExceptionOnMissing(parentConfig.isThrowExceptionOnMissing());
+    }
+
+    /**
+     * Performs interpolation. This implementation will ask the parent
+     * configuration to perform the interpolation so that variables can be
+     * evaluated in the global context.
+     *
+     * @param value the value to be interpolated
+     */
+    protected Object interpolate(Object value)
+    {
+        return getParent().interpolate(value);
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java?rev=411583&r1=411582&r2=411583&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java
Sun Jun  4 09:46:41 2006
@@ -183,8 +183,8 @@
      */
     public void testSetThrowExceptionOnMissing()
     {
-        setUpSubnodeConfig();
         parent.setThrowExceptionOnMissing(true);
+        setUpSubnodeConfig();
         assertTrue("Exception flag not fetchted from parent", config
                 .isThrowExceptionOnMissing());
         try
@@ -198,7 +198,7 @@
         }
 
         config.setThrowExceptionOnMissing(false);
-        assertFalse("Exception flag not set on parent", parent
+        assertTrue("Exception flag reset on parent", parent
                 .isThrowExceptionOnMissing());
     }
 
@@ -208,33 +208,36 @@
      */
     public void testSetDelimiterParsingDisabled()
     {
-        setUpSubnodeConfig();
         parent.setDelimiterParsingDisabled(true);
+        setUpSubnodeConfig();
+        parent.setDelimiterParsingDisabled(false);
         assertTrue("Delimiter parsing flag was not received from parent",
                 config.isDelimiterParsingDisabled());
         config.addProperty("newProp", "test1,test2,test3");
         assertEquals("New property was splitted", "test1,test2,test3", parent
                 .getString("tables.table(0).newProp"));
+        parent.setDelimiterParsingDisabled(true);
         config.setDelimiterParsingDisabled(false);
-        assertFalse("Delimiter parsing flag was not set on parent", parent
+        assertTrue("Delimiter parsing flag was reset on parent", parent
                 .isDelimiterParsingDisabled());
     }
 
     /**
-     * Tests manipulating the list delimiter. This piece of data is used by both
-     * the parent and the subnode.
+     * Tests manipulating the list delimiter. This piece of data is derived from
+     * the parent.
      */
     public void testSetListDelimiter()
     {
-        setUpSubnodeConfig();
         parent.setListDelimiter('/');
+        setUpSubnodeConfig();
+        parent.setListDelimiter(';');
         assertEquals("List delimiter not obtained from parent", '/', config
                 .getListDelimiter());
         config.addProperty("newProp", "test1,test2/test3");
         assertEquals("List was incorrectly splitted", "test1,test2", parent
                 .getString("tables.table(0).newProp"));
         config.setListDelimiter(',');
-        assertEquals("List delimiter not set at parent", ',', parent
+        assertEquals("List delimiter changed on parent", ';', parent
                 .getListDelimiter());
     }
 
@@ -243,8 +246,8 @@
      */
     public void testSetExpressionEngine()
     {
-        setUpSubnodeConfig();
         parent.setExpressionEngine(new XPathExpressionEngine());
+        setUpSubnodeConfig();
         assertEquals("Wrong field name", TABLE_FIELDS[0][1], config
                 .getString("fields/field[2]/name"));
         Set keys = new HashSet();
@@ -253,23 +256,42 @@
         assertTrue("Key 1 not contained", keys.contains("name"));
         assertTrue("Key 2 not contained", keys.contains("fields/field/name"));
         config.setExpressionEngine(null);
-        assertSame("Expression engine not set on parent",
-                HierarchicalConfiguration.getDefaultExpressionEngine(), parent
-                        .getExpressionEngine());
+        assertTrue("Expression engine reset on parent", parent
+                .getExpressionEngine() instanceof XPathExpressionEngine);
     }
-    
+
     /**
      * Tests the configurationAt() method.
      */
     public void testConfiguarationAt()
     {
         setUpSubnodeConfig();
-        SubnodeConfiguration sub2 = (SubnodeConfiguration) config.configurationAt("fields.field(1)");
-        assertEquals("Wrong value of property", TABLE_FIELDS[0][1], sub2.getString("name"));
+        SubnodeConfiguration sub2 = (SubnodeConfiguration) config
+                .configurationAt("fields.field(1)");
+        assertEquals("Wrong value of property", TABLE_FIELDS[0][1], sub2
+                .getString("name"));
         assertEquals("Wrong parent", config.getParent(), sub2.getParent());
     }
 
     /**
+     * Tests interpolation features. The subnode config should use its parent
+     * for interpolation.
+     */
+    public void testInterpolation()
+    {
+        parent.addProperty("tablespaces.tablespace.name", "default");
+        parent.addProperty("tablespaces.tablespace(-1).name", "test");
+        parent.addProperty("tables.table(0).tablespace",
+                "${tablespaces.tablespace(0).name}");
+        assertEquals("Wrong interpolated tablespace", "default", parent
+                .getString("tables.table(0).tablespace"));
+
+        setUpSubnodeConfig();
+        assertEquals("Wrong interpolated tablespace in subnode", "default",
+                config.getString("tablespace"));
+    }
+
+    /**
      * Initializes the parent configuration. This method creates the typical
      * structure of tables and fields nodes.
      *
@@ -286,7 +308,6 @@
                 nodeCounter++;
                 return super.createNode(name);
             }
-
         };
         for (int i = 0; i < TABLE_NAMES.length; i++)
         {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message