Author: oheger Date: Sat May 26 12:30:54 2007 New Revision: 541932 URL: http://svn.apache.org/viewvc?view=rev&rev=541932 Log: CONFIGURATION-215: Added a getSource() method to CompositeConfiguration
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java?view=diff&rev=541932&r1=541931&r2=541932 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java Sat May 26 12:30:54 2007 @@ -421,4 +421,52 @@ .setListDelimiter(listDelimiter); super.setListDelimiter(listDelimiter); } + + /** + * Returns the configuration source, in which the specified key is defined. + * This method will iterate over all existing child configurations and check + * whether they contain the specified key. The following constellations are + * possible: + * <ul> + * <li>If exactly one child configuration contains the key, this + * configuration is returned as the source configuration. This may be the + * <em>in memory configuration</em> (this has to be explicitly checked by + * the calling application).</li> + * <li>If none of the child configurations contain the key, <b>null</b> is + * returned.</li> + * <li>If the key is contained in multiple child configurations or if the + * key is <b>null</b>, a <code>IllegalArgumentException</code> is thrown. + * In this case the source configuration cannot be determined.</li> + * </ul> + * + * @param key the key to be checked + * @return the source configuration of this key + * @throws IllegalArgumentException if the source configuration cannot be + * determined + * @since 1.5 + */ + public Configuration getSource(String key) + { + if (key == null) + { + throw new IllegalArgumentException("Key must not be null!"); + } + + Configuration source = null; + for (Iterator it = configList.iterator(); it.hasNext();) + { + Configuration conf = (Configuration) it.next(); + if (conf.containsKey(key)) + { + if (source != null) + { + throw new IllegalArgumentException("The key " + key + + " is defined by multiple sources!"); + } + source = conf; + } + } + + return source; + } } Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java?view=diff&rev=541932&r1=541931&r2=541932 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java Sat May 26 12:30:54 2007 @@ -40,6 +40,9 @@ */ public class TestCompositeConfiguration extends TestCase { + /** Constant for a test property to be checked.*/ + private static final String TEST_PROPERTY = "test.source.property"; + protected PropertiesConfiguration conf1; protected PropertiesConfiguration conf2; protected XMLConfiguration xmlConf; @@ -504,7 +507,7 @@ /** * Writes a test properties file containing a single property definition. - * + * * @param f the file to write * @param prop the property name * @param value the property value @@ -681,6 +684,85 @@ cc.addProperty("test.property", "a,b,c"); assertEquals("Wrong value of property", "a,b,c", cc .getString("test.property")); + } + + /** + * Prepares a test of the getSource() method. + */ + private void setUpSourceTest() + { + cc.addConfiguration(conf1); + cc.addConfiguration(conf2); + } + + /** + * Tests the getSource() method if the property is defined in a single child + * configuration. + */ + public void testGetSourceSingle() + { + setUpSourceTest(); + conf1.addProperty(TEST_PROPERTY, Boolean.TRUE); + assertSame("Wrong source configuration", conf1, cc + .getSource(TEST_PROPERTY)); + } + + /** + * Tests the getSource() method for an unknown property key. + */ + public void testGetSourceUnknown() + { + setUpSourceTest(); + assertNull("Wrong source for unknown key", cc.getSource(TEST_PROPERTY)); + } + + /** + * Tests the getSource() method for a property contained in the in memory + * configuration. + */ + public void testGetSourceInMemory() + { + setUpSourceTest(); + cc.addProperty(TEST_PROPERTY, Boolean.TRUE); + assertSame("Source not found in in-memory config", cc + .getInMemoryConfiguration(), cc.getSource(TEST_PROPERTY)); + } + + /** + * Tests the getSource() method if the property is defined by multiple child + * configurations. In this case an exception should be thrown. + */ + public void testGetSourceMultiple() + { + setUpSourceTest(); + conf1.addProperty(TEST_PROPERTY, Boolean.TRUE); + cc.addProperty(TEST_PROPERTY, "a value"); + try + { + cc.getSource(TEST_PROPERTY); + fail("Property in multiple configurations did not cause an error!"); + } + catch (IllegalArgumentException iex) + { + // ok + } + } + + /** + * Tests the getSource() method for a null key. This should cause an + * exception. + */ + public void testGetSourceNull() + { + try + { + cc.getSource(null); + fail("Could pass null key to getSource()!"); + } + catch (IllegalArgumentException iex) + { + // ok + } } /** Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=541932&r1=541931&r2=541932 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat May 26 12:30:54 2007 @@ -23,6 +23,11 @@ <body> <release version="1.5-SNAPSHOT" date="in SVN" description=""> + <action dev="oheger" type="add" issue="CONFIGURATION-215"> + A new getSource() method was added to CompositeConfiguration and + CombinedConfiguration, which returns the child configuration, in which + a given property is defined. + </action> <action dev="oheger" type="fix" issue="CONFIGURATION-274"> PropertiesConfiguration now supports escaping the escape character for list delimiters. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]