Author: oheger
Date: Wed Oct 29 20:19:37 2014
New Revision: 1635293
URL: http://svn.apache.org/r1635293
Log:
[CONFIGURATION-427] Support saving of arrays.
Arrays added to a XMLPropertyListConfiguration are now handled in a special way
so that their storage format is adapted. Thanks to Ho-jin Lee for the solution
proposal.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java?rev=1635293&r1=1635292&r2=1635293&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java
Wed Oct 29 20:19:37 2014
@@ -28,6 +28,7 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
@@ -190,10 +191,14 @@ public class XMLPropertyListConfiguratio
@Override
protected void addPropertyInternal(String key, Object value)
{
- if (value instanceof byte[])
+ if (value instanceof byte[] || value instanceof List)
{
addPropertyDirect(key, value);
}
+ else if (value instanceof Object[])
+ {
+ addPropertyDirect(key, Arrays.asList((Object[]) value));
+ }
else
{
super.addPropertyInternal(key, value);
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java?rev=1635293&r1=1635292&r2=1635293&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java
Wed Oct 29 20:19:37 2014
@@ -17,13 +17,16 @@
package org.apache.commons.configuration2.plist;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.StringWriter;
+import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
@@ -32,7 +35,6 @@ import java.util.TimeZone;
import junitx.framework.ArrayAssert;
import junitx.framework.ListAssert;
import junitx.framework.ObjectAssert;
-
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ConfigurationAssert;
import org.apache.commons.configuration2.ConfigurationComparator;
@@ -455,4 +457,55 @@ public class TestXMLPropertyListConfigur
"<?xml version=\"1.0\" encoding=\"" + encoding
+ "\"?>") >= 0);
}
+
+ /**
+ * Checks whether the test configuration contains a key with an array
value.
+ *
+ * @param expectedValues the expected values
+ */
+ private void checkArrayProperty(List<?> expectedValues)
+ throws ConfigurationException
+ {
+ StringWriter out = new StringWriter();
+ new FileHandler(config).save(out);
+ StringBuilder values = new StringBuilder();
+ for (Object v : expectedValues)
+ {
+ values.append("<string>").append(v).append("</string>");
+ }
+ String content = out.toString().replaceAll("[ \n\r]", "");
+ assertThat(content, containsString(String.format(
+ "<key>array</key><array>%s</array>", values)));
+ }
+
+ /**
+ * Tests whether a list can be saved correctly. This test is related to
+ * CONFIGURATION-427.
+ */
+ @Test
+ public void testSaveList() throws ConfigurationException
+ {
+ List<String> elems =
+ Arrays.asList("element1", "element2", "anotherElement");
+ config = new XMLPropertyListConfiguration();
+ config.addProperty("array", elems);
+
+ checkArrayProperty(elems);
+ }
+
+ /**
+ * Tests whether an array can be saved correctly. This test is related to
+ * CONFIGURATION-427.
+ */
+ @Test
+ public void testSaveArray() throws ConfigurationException
+ {
+ Object[] elems = {
+ "arrayElem1", "arrayElem2", "arrayElem3"
+ };
+ config = new XMLPropertyListConfiguration();
+ config.addProperty("array", elems);
+
+ checkArrayProperty(Arrays.asList(elems));
+ }
}