Author: ggregory Date: Wed Apr 5 03:44:47 2017 New Revision: 1790173 URL: http://svn.apache.org/viewvc?rev=1790173&view=rev Log: [CONFIGURATION-658] Add API org.apache.commons.configuration2.ImmutableConfiguration.getURI(String).
Modified: commons/proper/configuration/trunk/src/changes/changes.xml commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java Modified: commons/proper/configuration/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1790173&r1=1790172&r2=1790173&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/changes/changes.xml (original) +++ commons/proper/configuration/trunk/src/changes/changes.xml Wed Apr 5 03:44:47 2017 @@ -35,6 +35,9 @@ INIConfiguration can now be configured to use a custom separator between properties and values when writing an ini file. </action> + <action dev="ggregory" type="add" issue="CONFIGURATION-658"> + Add API org.apache.commons.configuration2.ImmutableConfiguration.getURI(String). + </action> </release> <release version="2.1.1" date="2017-02-05" Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java?rev=1790173&r1=1790172&r2=1790173&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java Wed Apr 5 03:44:47 2017 @@ -19,6 +19,7 @@ package org.apache.commons.configuration import java.math.BigDecimal; import java.math.BigInteger; +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -1355,6 +1356,18 @@ public abstract class AbstractConfigurat } @Override + public URI getURI(String key) + { + return convert(URI.class, key, null, true); + } + + @Override + public URI getURI(String key, URI defaultValue) + { + return convert(URI.class, key, defaultValue, false); + } + + @Override public String getString(String key, String defaultValue) { String result = convert(String.class, key, null, false); Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java?rev=1790173&r1=1790172&r2=1790173&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java Wed Apr 5 03:44:47 2017 @@ -18,6 +18,7 @@ package org.apache.commons.configuration import java.math.BigDecimal; import java.math.BigInteger; +import java.net.URI; import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -461,6 +462,32 @@ public interface ImmutableConfiguration String getString(String key, String defaultValue); /** + * Gets a URI associated with the given configuration key. + * + * @param key The configuration key. + * @return The associated string. + * + * @throws org.apache.commons.configuration2.ex.ConversionException is thrown if the key maps to an object that + * is not a String. + */ + URI getURI(String key); + + /** + * Gets a URI associated with the given configuration key. + * If the key doesn't map to an existing object, the default value + * is returned. + * + * @param key The configuration key. + * @param defaultValue The default value. + * @return The associated URI if key is found and has valid + * format, default value otherwise. + * + * @throws org.apache.commons.configuration2.ex.ConversionException is thrown if the key maps to an object that + * is not a String. + */ + URI getURI(String key, URI defaultValue); + + /** * Get the value of a string property that is stored in encoded form in this * configuration. This method obtains the value of the string property * identified by the given key. This value is then passed to the provided @@ -723,4 +750,6 @@ public interface ImmutableConfiguration * @return a subset immutable configuration */ ImmutableConfiguration immutableSubset(String prefix); + + } Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java?rev=1790173&r1=1790172&r2=1790173&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java Wed Apr 5 03:44:47 2017 @@ -25,6 +25,7 @@ import static org.junit.Assert.assertTru import java.math.BigDecimal; import java.math.BigInteger; +import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -35,7 +36,6 @@ import java.util.NoSuchElementException; import java.util.Properties; import java.util.StringTokenizer; -import junitx.framework.ListAssert; import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; import org.apache.commons.configuration2.event.ConfigurationEvent; import org.apache.commons.configuration2.event.EventListener; @@ -47,6 +47,8 @@ import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; +import junitx.framework.ListAssert; + /** * Tests some basic functions of the BaseConfiguration class. Missing keys will * throw Exceptions @@ -276,6 +278,18 @@ public class TestBaseConfiguration assertEquals("Missing key with default value", defaultValue, config.getString("stringNotInConfig", defaultValue)); } + @Test + public void testGetURI() + { + config.setProperty("testURI", URI.create("http://example.com")); + URI uri = URI.create("http://example.com"); + URI defaultValue = URI.create("http://localhost"); + + assertEquals("Existing key", uri, config.getURI("testURI")); + assertEquals("Existing key with default value", uri, config.getURI("testURI", defaultValue)); + assertEquals("Missing key with default value", defaultValue, config.getURI("stringNotInConfig", defaultValue)); + } + @Test(expected = NoSuchElementException.class) public void testGetStringUnknown() {