commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1090168 - in /commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/DefaultConfigurationBuilder.java test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
Date Fri, 08 Apr 2011 09:31:07 GMT
Author: oheger
Date: Fri Apr  8 09:31:07 2011
New Revision: 1090168

URL: http://svn.apache.org/viewvc?rev=1090168&view=rev
Log:
[CONFIGURATION-437] Some properties of DefaultConfigurationBuilder are now inherited by child
builders.

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java?rev=1090168&r1=1090167&r2=1090168&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
Fri Apr  8 09:31:07 2011
@@ -30,6 +30,8 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.beanutils.BeanHelper;
 import org.apache.commons.configuration.beanutils.DefaultBeanFactory;
 import org.apache.commons.configuration.beanutils.XMLBeanDeclaration;
+import org.apache.commons.configuration.event.ConfigurationErrorListener;
+import org.apache.commons.configuration.event.ConfigurationListener;
 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
 import org.apache.commons.configuration.resolver.CatalogResolver;
 import org.apache.commons.configuration.resolver.EntityRegistry;
@@ -1671,5 +1673,56 @@ public class DefaultConfigurationBuilder
         {
             return new CombinedConfiguration();
         }
+
+        /**
+         * {@inheritDoc} This implementation ensures that the configuration
+         * builder created by this provider inherits the properties from the
+         * current configuration builder.
+         */
+        protected void initBeanInstance(Object bean, BeanDeclaration data)
+                throws Exception
+        {
+            ConfigurationDeclaration decl = (ConfigurationDeclaration) data;
+            initChildBuilder(decl.getConfigurationBuilder(),
+                    (DefaultConfigurationBuilder) bean);
+            super.initBeanInstance(bean, data);
+        }
+
+        /**
+         * Initializes the given child configuration builder from its parent
+         * builder. This method copies the values of some properties from the
+         * parent builder to the child builder so that the child inherits
+         * properties from its parent.
+         *
+         * @param parent the parent builder
+         * @param child the child builder
+         */
+        private static void initChildBuilder(
+                DefaultConfigurationBuilder parent,
+                DefaultConfigurationBuilder child)
+        {
+            child.setAttributeSplittingDisabled(parent
+                    .isAttributeSplittingDisabled());
+            child.setBasePath(parent.getBasePath());
+            child.setDelimiterParsingDisabled(parent
+                    .isDelimiterParsingDisabled());
+            child.setListDelimiter(parent.getListDelimiter());
+            child.setThrowExceptionOnMissing(parent.isThrowExceptionOnMissing());
+            child.setLogger(parent.getLogger());
+
+            child.clearConfigurationListeners();
+            for (Iterator it = parent.getConfigurationListeners().iterator(); it
+                    .hasNext();)
+            {
+                child.addConfigurationListener((ConfigurationListener) it
+                        .next());
+            }
+            child.clearErrorListeners();
+            for (Iterator it = parent.getErrorListeners().iterator(); it
+                    .hasNext();)
+            {
+                child.addErrorListener((ConfigurationErrorListener) it.next());
+            }
+        }
     }
 }

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java?rev=1090168&r1=1090167&r2=1090168&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
Fri Apr  8 09:31:07 2011
@@ -29,6 +29,7 @@ import java.util.Set;
 import junit.framework.TestCase;
 
 import org.apache.commons.configuration.beanutils.BeanHelper;
+import org.apache.commons.configuration.event.ConfigurationListenerTestImpl;
 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
 import org.apache.commons.configuration.tree.ConfigurationNode;
 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
@@ -751,6 +752,82 @@ public class TestDefaultConfigurationBui
     }
 
     /**
+     * Tests whether settings of the builder are propagated to child builders.
+     */
+    public void testConfigurationBuilderProviderInheritProperties()
+            throws Exception
+    {
+        factory.addProperty("override.configuration[@fileName]",
+                TEST_FILE.getAbsolutePath());
+        factory.setBasePath("conf");
+        factory.setAttributeSplittingDisabled(true);
+        factory.setDelimiterParsingDisabled(true);
+        factory.setListDelimiter('/');
+        factory.setThrowExceptionOnMissing(true);
+        Log log = LogFactory.getLog(getClass());
+        factory.setLogger(log);
+        factory.clearErrorListeners();
+        factory.clearConfigurationListeners();
+        ConfigurationListenerTestImpl l =
+                new ConfigurationListenerTestImpl(factory);
+        factory.addConfigurationListener(l);
+        DefaultConfigurationBuilder.ConfigurationDeclaration decl =
+                new DefaultConfigurationBuilder.ConfigurationDeclaration(
+                        factory,
+                        factory.configurationAt("override.configuration"));
+        DefaultConfigurationBuilder.ConfigurationBuilderProvider provider =
+                new DefaultConfigurationBuilder.ConfigurationBuilderProvider();
+        DefaultConfigurationBuilder child =
+                (DefaultConfigurationBuilder) provider.createBean(
+                        provider.fetchConfigurationClass(), decl, null);
+        assertEquals("Wrong base path", factory.getBasePath(),
+                child.getBasePath());
+        assertEquals("Wrong attribute splitting flag",
+                factory.isAttributeSplittingDisabled(),
+                child.isAttributeSplittingDisabled());
+        assertEquals("Wrong delimiter parsing flag",
+                factory.isDelimiterParsingDisabled(),
+                child.isDelimiterParsingDisabled());
+        assertEquals("Wrong list delimiter", factory.getListDelimiter(),
+                child.getListDelimiter());
+        assertEquals("Wrong exception flag",
+                factory.isThrowExceptionOnMissing(),
+                child.isThrowExceptionOnMissing());
+        assertSame("Wrong logger", log, child.getLogger());
+        assertTrue("Got error listeners", child.getErrorListeners().isEmpty());
+        assertEquals("Wrong number of listeners", 1, child
+                .getConfigurationListeners().size());
+        assertEquals("Wrong listener", l, child.getConfigurationListeners()
+                .iterator().next());
+    }
+
+    /**
+     * Tests whether properties of the parent configuration can be overridden.
+     */
+    public void testConfigurationBuilderProviderOverrideProperties()
+            throws Exception
+    {
+        factory.addProperty("override.configuration[@fileName]",
+                TEST_FILE.getAbsolutePath());
+        factory.addProperty("override.configuration[@basePath]", "base");
+        factory.addProperty("override.configuration[@throwExceptionOnMissing]",
+                "false");
+        factory.setBasePath("conf");
+        factory.setThrowExceptionOnMissing(true);
+        DefaultConfigurationBuilder.ConfigurationDeclaration decl =
+                new DefaultConfigurationBuilder.ConfigurationDeclaration(
+                        factory,
+                        factory.configurationAt("override.configuration"));
+        DefaultConfigurationBuilder.ConfigurationBuilderProvider provider =
+                new DefaultConfigurationBuilder.ConfigurationBuilderProvider();
+        DefaultConfigurationBuilder child =
+                (DefaultConfigurationBuilder) provider.createBean(
+                        provider.fetchConfigurationClass(), decl, null);
+        assertEquals("Wrong base path", "base", child.getBasePath());
+        assertFalse("Wrong exception flag", child.isThrowExceptionOnMissing());
+    }
+
+    /**
      * Tests whether XML settings can be inherited.
      */
     public void testLoadXMLWithSettings() throws ConfigurationException,



Mime
View raw message