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 2013/07/13 21:42:21 UTC
svn commit: r1502867 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/ConfigurationConverter.java
test/java/org/apache/commons/configuration/TestConfigurationConverter.java
Author: oheger
Date: Sat Jul 13 19:42:20 2013
New Revision: 1502867
URL: http://svn.apache.org/r1502867
Log:
Reworked the handling of list properties in ConfigurationConverter.getProperties().
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationConverter.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationConverter.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationConverter.java?rev=1502867&r1=1502866&r2=1502867&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationConverter.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationConverter.java Sat Jul 13 19:42:20 2013
@@ -35,6 +35,9 @@ import org.apache.commons.lang3.StringUt
*/
public final class ConfigurationConverter
{
+ /** Constant for the default separator for properties with multiple values. */
+ private static final char DEFAULT_SEPARATOR = ',';
+
/**
* Private constructor prevents instances from being created.
*/
@@ -94,8 +97,9 @@ public final class ConfigurationConverte
/**
* Convert a Configuration class into a Properties class. List properties
- * are joined into a string using the delimiter of the configuration if it
- * extends AbstractConfiguration, and a comma otherwise.
+ * are joined into a string using either the list delimiter handler of the
+ * configuration (if it extends AbstractConfiguration) or with a comma as
+ * delimiter otherwise.
*
* @param config Configuration object to convert
* @return Properties created from the Configuration
@@ -103,17 +107,47 @@ public final class ConfigurationConverte
public static Properties getProperties(Configuration config)
{
Properties props = new Properties();
+ ListDelimiterHandler listHandler;
+ boolean useDelimiterHandler;
- char delimiter = (config instanceof AbstractConfiguration)
- ? ((AbstractConfiguration) config).getListDelimiter() : ',';
+ if(config instanceof AbstractConfiguration)
+ {
+ listHandler = ((AbstractConfiguration) config).getListDelimiterHandler();
+ useDelimiterHandler = true;
+ }
+ else
+ {
+ listHandler = null;
+ useDelimiterHandler = false;
+ }
for (Iterator<String> keys = config.getKeys(); keys.hasNext();)
{
String key = keys.next();
List<Object> list = config.getList(key);
- // turn the list into a string
- props.setProperty(key, StringUtils.join(list.iterator(), delimiter));
+ String propValue;
+ if (useDelimiterHandler)
+ {
+ try
+ {
+ propValue =
+ String.valueOf(listHandler.escapeList(list,
+ ListDelimiterHandler.NOOP_TRANSFORMER));
+ }
+ catch (Exception ex)
+ {
+ // obviously, the list handler does not support splitting
+ useDelimiterHandler = false;
+ propValue = listToString(list);
+ }
+ }
+ else
+ {
+ propValue = listToString(list);
+ }
+
+ props.setProperty(key, propValue);
}
return props;
@@ -130,4 +164,15 @@ public final class ConfigurationConverte
return new ConfigurationMap(config);
}
+ /**
+ * Helper method for joining all elements of a list to a string using the
+ * default value separator.
+ *
+ * @param list the list
+ * @return the resulting string
+ */
+ private static String listToString(List<?> list)
+ {
+ return StringUtils.join(list, DEFAULT_SEPARATOR);
+ }
}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationConverter.java?rev=1502867&r1=1502866&r2=1502867&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationConverter.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationConverter.java Sat Jul 13 19:42:20 2013
@@ -25,6 +25,8 @@ import java.util.Map;
import java.util.Properties;
import org.apache.commons.collections.ExtendedProperties;
+import org.easymock.EasyMock;
+import org.easymock.IAnswer;
import org.junit.Test;
/**
@@ -90,8 +92,11 @@ public class TestConfigurationConverter
assertEquals("This returns 123", 123, eprops.getInt("int"));
}
- @Test
- public void testConfigurationToProperties()
+ /**
+ * Creates a configuration object with some test values.
+ * @return the test configuration
+ */
+ private static BaseConfiguration createTestConfiguration()
{
BaseConfiguration config = new BaseConfiguration();
config.addProperty("string", "teststring");
@@ -100,7 +105,17 @@ public class TestConfigurationConverter
config.addProperty("interpolated", "${string}");
config.addProperty("interpolated-array", "${interpolated}");
config.addProperty("interpolated-array", "${interpolated}");
+ return config;
+ }
+ /**
+ * Tests a conversion to Properties if the default list delimiter handler
+ * is used (which does not support list joining).
+ */
+ @Test
+ public void testConfigurationToPropertiesDefaultListHandling()
+ {
+ BaseConfiguration config = createTestConfiguration();
Properties props = ConfigurationConverter.getProperties(config);
assertNotNull("null properties", props);
@@ -108,14 +123,48 @@ public class TestConfigurationConverter
assertEquals("'interpolated' property", "teststring", props.getProperty("interpolated"));
assertEquals("'array' property", "item 1,item 2", props.getProperty("array"));
assertEquals("'interpolated-array' property", "teststring,teststring", props.getProperty("interpolated-array"));
+ }
- // change the list delimiter
- config.setListDelimiter(';');
- props = ConfigurationConverter.getProperties(config);
+ /**
+ * Tests a conversion to Properties if the list delimiter handler supports
+ * list joining.
+ */
+ @Test
+ public void testConfigurationToPropertiesListDelimiterHandler()
+ {
+ BaseConfiguration config = createTestConfiguration();
+ config.setListDelimiterHandler(new DefaultListDelimiterHandler(';'));
+ Properties props = ConfigurationConverter.getProperties(config);
assertEquals("'array' property", "item 1;item 2", props.getProperty("array"));
}
/**
+ * Tests a conversion to Properties if the source configuration does not
+ * extend AbstractConfiguration. In this case, properties with multiple
+ * values have to be handled in a special way.
+ */
+ @Test
+ public void testConfigurationToPropertiesNoAbstractConfiguration()
+ {
+ Configuration src = EasyMock.createMock(Configuration.class);
+ final BaseConfiguration config = createTestConfiguration();
+ EasyMock.expect(src.getKeys()).andReturn(config.getKeys());
+ src.getList(EasyMock.anyObject(String.class));
+ EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
+ {
+ public Object answer() throws Throwable
+ {
+ String key = (String) EasyMock.getCurrentArguments()[0];
+ return config.getList(key);
+ }
+ }).anyTimes();
+ EasyMock.replay(src);
+ Properties props = ConfigurationConverter.getProperties(src);
+ assertEquals("'array' property", "item 1,item 2",
+ props.getProperty("array"));
+ }
+
+ /**
* Tests the conversion of a configuration object to properties if scalar
* values are involved. This test is related to CONFIGURATION-432.
*/