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:46:30 UTC

svn commit: r1638382 - 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:46:30 2014
New Revision: 1638382

URL: http://svn.apache.org/r1638382
Log:
Implemented getEncodedString() using a default decoder.

A property for the default decoder was added to AbstractConfiguration.

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=1638382&r1=1638381&r2=1638382&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:46:30 2014
@@ -122,6 +122,9 @@ public abstract class AbstractConfigurat
     /** The object responsible for synchronization. */
     private volatile Synchronizer synchronizer;
 
+    /** The object used for dealing with encoded property values. */
+    private ConfigurationDecoder configurationDecoder;
+
     /** Stores the logger.*/
     private Log log;
 
@@ -379,6 +382,30 @@ public abstract class AbstractConfigurat
     }
 
     /**
+     * Sets the {@code ConfigurationDecoder} for this configuration. This object
+     * is used by {@link #getEncodedString(String)}.
+     *
+     * @param configurationDecoder the {@code ConfigurationDecoder}
+     * @since 2.0
+     */
+    public void setConfigurationDecoder(
+            ConfigurationDecoder configurationDecoder)
+    {
+        this.configurationDecoder = configurationDecoder;
+    }
+
+    /**
+     * Returns the {@code ConfigurationDecoder} used by this instance.
+     *
+     * @return the {@code ConfigurationDecoder}
+     * @since 2.0
+     */
+    public ConfigurationDecoder getConfigurationDecoder()
+    {
+        return configurationDecoder;
+    }
+
+    /**
      * Creates a clone of the {@code ConfigurationInterpolator} used by this
      * instance. This method can be called by {@code clone()} implementations of
      * derived classes. Normally, the {@code ConfigurationInterpolator} of a
@@ -1308,10 +1335,25 @@ public abstract class AbstractConfigurat
         return (value != null) ? decoder.decode(value) : null;
     }
 
+    /**
+     * {@inheritDoc} This implementation makes use of the
+     * {@code ConfigurationDecoder} set for this configuration. If no such
+     * object has been set, an {@code IllegalStateException} exception is
+     * thrown.
+     *
+     * @throws IllegalStateException if no {@code ConfigurationDecoder} is set
+     * @see #setConfigurationDecoder(ConfigurationDecoder)
+     */
     @Override
-    public String getEncodedString(String key) {
-        //TODO implementation
-        throw new UnsupportedOperationException("Not yet implemented!");
+    public String getEncodedString(String key)
+    {
+        ConfigurationDecoder decoder = getConfigurationDecoder();
+        if (decoder == null)
+        {
+            throw new IllegalStateException(
+                    "No default ConfigurationDecoder defined!");
+        }
+        return getEncodedString(key, decoder);
     }
 
     /**

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=1638382&r1=1638381&r2=1638382&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:46:30 2014
@@ -995,6 +995,37 @@ public class TestAbstractConfigurationBa
     }
 
     /**
+     * Tries to query an encoded string with the default decoder if this property is not
+     * defined.
+     */
+    @Test(expected = IllegalStateException.class)
+    public void testGetEncodedStringNoDefaultDecoderDefined()
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration();
+        config.getEncodedString(KEY_PREFIX);
+    }
+
+    /**
+     * Tests whether a default decoder can be set which is queried for encoded strings.
+     */
+    @Test
+    public void testGetEncodedStringWithDefaultDecoder()
+    {
+        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.setConfigurationDecoder(decoder);
+        config.addProperty(KEY_PREFIX, value);
+        assertEquals("Wrong decoded value", decodedValue,
+                config.getEncodedString(KEY_PREFIX));
+    }
+
+    /**
      * 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.