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 2014/11/11 21:45:38 UTC
svn commit: r1638381 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration2/AbstractConfiguration.java
test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
Author: oheger
Date: Tue Nov 11 20:45:38 2014
New Revision: 1638381
URL: http://svn.apache.org/r1638381
Log:
Implemented getEncodedString() which expects a ConfigurationDecoder.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
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=1638381&r1=1638380&r2=1638381&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 Tue Nov 11 20:45:38 2014
@@ -1288,10 +1288,24 @@ public abstract class AbstractConfigurat
return (result != null) ? result : interpolate(defaultValue);
}
+ /**
+ * {@inheritDoc} This implementation delegates to {@link #getString(String)}
+ * in order to obtain the value of the passed in key. This value is passed
+ * to the decoder. Because {@code getString()} is used behind the scenes all
+ * standard features like handling of missing keys and interpolation work as
+ * expected.
+ */
@Override
- public String getEncodedString(String key, ConfigurationDecoder decoder) {
- //TODO implementation
- throw new UnsupportedOperationException("Not yet implemented!");
+ public String getEncodedString(String key, ConfigurationDecoder decoder)
+ {
+ if (decoder == null)
+ {
+ throw new IllegalArgumentException(
+ "ConfigurationDecoder must not be null!");
+ }
+
+ String value = getString(key);
+ return (value != null) ? decoder.decode(value) : null;
}
@Override
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java?rev=1638381&r1=1638380&r2=1638381&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java Tue Nov 11 20:45:38 2014
@@ -953,6 +953,48 @@ public class TestAbstractConfigurationBa
}
/**
+ * Tries to query an encoded string without a decoder.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetEncodedStringNoDecoder()
+ {
+ PropertiesConfiguration config = new PropertiesConfiguration();
+ config.getEncodedString(KEY_PREFIX, null);
+ }
+
+ /**
+ * Tests whether undefined keys are handled when querying encoded strings.
+ */
+ @Test
+ public void testGetEncodedStringNoValue()
+ {
+ ConfigurationDecoder decoder =
+ EasyMock.createMock(ConfigurationDecoder.class);
+ EasyMock.replay(decoder);
+ PropertiesConfiguration config = new PropertiesConfiguration();
+ assertNull("Got a value", config.getEncodedString(KEY_PREFIX, decoder));
+ }
+
+ /**
+ * Tests whether an encoded value can be retrieved.
+ */
+ @Test
+ public void testGetEncodedStringValue()
+ {
+ ConfigurationDecoder decoder =
+ EasyMock.createMock(ConfigurationDecoder.class);
+ final String value = "original value";
+ final String decodedValue = "decoded value";
+ EasyMock.expect(decoder.decode(value)).andReturn(decodedValue);
+ EasyMock.replay(decoder);
+
+ PropertiesConfiguration config = new PropertiesConfiguration();
+ config.addProperty(KEY_PREFIX, value);
+ assertEquals("Wrong decoded value", decodedValue,
+ config.getEncodedString(KEY_PREFIX, decoder));
+ }
+
+ /**
* Creates the source configuration for testing the copy() and append()
* methods. This configuration contains keys with an odd index and values
* starting with the prefix "src". There are also some list properties.