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/04/03 20:53:50 UTC
svn commit: r1464157 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/plist/
test/java/org/apache/commons/configuration/plist/
Author: oheger
Date: Wed Apr 3 18:53:50 2013
New Revision: 1464157
URL: http://svn.apache.org/r1464157
Log:
XMLPropertyListConfiguration no longer extends AbstractFileConfiguration.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfigurationEvents.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java?rev=1464157&r1=1464156&r2=1464157&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java Wed Apr 3 18:53:50 2013
@@ -17,13 +17,11 @@
package org.apache.commons.configuration.plist;
-import java.io.File;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -41,11 +39,14 @@ import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.configuration.AbstractHierarchicalFileConfiguration;
+import org.apache.commons.configuration.BaseHierarchicalConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.FileBasedConfiguration;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.MapConfiguration;
+import org.apache.commons.configuration.io.FileLocator;
+import org.apache.commons.configuration.io.FileLocatorAware;
import org.apache.commons.configuration.tree.ConfigurationNode;
import org.apache.commons.configuration.tree.DefaultConfigurationNode;
import org.apache.commons.lang.StringEscapeUtils;
@@ -122,7 +123,8 @@ import org.xml.sax.helpers.DefaultHandle
* @author Emmanuel Bourg
* @version $Id$
*/
-public class XMLPropertyListConfiguration extends AbstractHierarchicalFileConfiguration
+public class XMLPropertyListConfiguration extends BaseHierarchicalConfiguration
+ implements FileBasedConfiguration, FileLocatorAware
{
/**
* The serial version UID.
@@ -132,6 +134,9 @@ public class XMLPropertyListConfiguratio
/** Size of the indentation for the generated file. */
private static final int INDENT_SIZE = 4;
+ /** Temporarily stores the current file location. */
+ private FileLocator locator;
+
/**
* Creates an empty XMLPropertyListConfiguration object which can be
* used to synthesize a new plist file by adding values and
@@ -154,40 +159,6 @@ public class XMLPropertyListConfiguratio
super(configuration);
}
- /**
- * Creates and loads the property list from the specified file.
- *
- * @param fileName The name of the plist file to load.
- * @throws org.apache.commons.configuration.ConfigurationException Error
- * while loading the plist file
- */
- public XMLPropertyListConfiguration(String fileName) throws ConfigurationException
- {
- super(fileName);
- }
-
- /**
- * Creates and loads the property list from the specified file.
- *
- * @param file The plist file to load.
- * @throws ConfigurationException Error while loading the plist file
- */
- public XMLPropertyListConfiguration(File file) throws ConfigurationException
- {
- super(file);
- }
-
- /**
- * Creates and loads the property list from the specified URL.
- *
- * @param url The location of the plist file to load.
- * @throws ConfigurationException Error while loading the plist file
- */
- public XMLPropertyListConfiguration(URL url) throws ConfigurationException
- {
- super(url);
- }
-
@Override
public void setProperty(String key, Object value)
{
@@ -228,7 +199,18 @@ public class XMLPropertyListConfiguratio
}
}
- public void load(Reader in) throws ConfigurationException
+ /**
+ * Stores the current file locator. This method is called before I/O
+ * operations.
+ *
+ * @param locator the current {@code FileLocator}
+ */
+ public void initFileLocator(FileLocator locator)
+ {
+ this.locator = locator;
+ }
+
+ public void read(Reader in) throws ConfigurationException
{
// We have to make sure that the root node is actually a PListNode.
// If this object was not created using the standard constructor, the
@@ -265,13 +247,13 @@ public class XMLPropertyListConfiguratio
}
}
- public void save(Writer out) throws ConfigurationException
+ public void write(Writer out) throws ConfigurationException
{
PrintWriter writer = new PrintWriter(out);
- if (getEncoding() != null)
+ if (locator.getEncoding() != null)
{
- writer.println("<?xml version=\"1.0\" encoding=\"" + getEncoding() + "\"?>");
+ writer.println("<?xml version=\"1.0\" encoding=\"" + locator.getEncoding() + "\"?>");
}
else
{
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java?rev=1464157&r1=1464156&r2=1464157&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java Wed Apr 3 18:53:50 2013
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNot
import static org.junit.Assert.assertTrue;
import java.io.File;
+import java.io.StringWriter;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
@@ -36,11 +37,12 @@ import org.apache.commons.configuration.
import org.apache.commons.configuration.ConfigurationAssert;
import org.apache.commons.configuration.ConfigurationComparator;
import org.apache.commons.configuration.ConfigurationException;
-import org.apache.commons.configuration.FileConfiguration;
-import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.StrictConfigurationComparator;
+import org.apache.commons.configuration.io.FileHandler;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
/**
* @author Emmanuel Bourg
@@ -48,14 +50,42 @@ import org.junit.Test;
*/
public class TestXMLPropertyListConfiguration
{
- private FileConfiguration config;
+ /** A helper object for dealing with temporary files. */
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
+
+ /** The test configuration. */
+ private XMLPropertyListConfiguration config;
@Before
public void setUp() throws Exception
{
config = new XMLPropertyListConfiguration();
- config.setFile(ConfigurationAssert.getTestFile("test.plist.xml"));
- config.load();
+ load(config, ConfigurationAssert.getTestFile("test.plist.xml"));
+ }
+
+ /**
+ * Loads a test configuration.
+ *
+ * @param c the configuration object to be loaded
+ * @param file the test file to be loaded
+ * @throws ConfigurationException if an error occurs
+ */
+ private static void load(XMLPropertyListConfiguration c, File file)
+ throws ConfigurationException
+ {
+ new FileHandler(c).load(file);
+ }
+
+ /**
+ * Saves the test configuration to the specified file.
+ *
+ * @param file the target file
+ * @throws ConfigurationException if an error occurs
+ */
+ private void save(File file) throws ConfigurationException
+ {
+ new FileHandler(config).save(file);
}
@Test
@@ -213,13 +243,7 @@ public class TestXMLPropertyListConfigur
@Test
public void testSave() throws Exception
{
- File savedFile = new File("target/testsave.plist.xml");
-
- // remove the file previously saved if necessary
- if (savedFile.exists())
- {
- assertTrue(savedFile.delete());
- }
+ File savedFile = folder.newFile();
// add an array of strings to the configuration
/*
@@ -242,13 +266,12 @@ public class TestXMLPropertyListConfigur
// todo : a Map added to a HierarchicalConfiguration should be decomposed as list of nodes
// save the configuration
- String filename = savedFile.getAbsolutePath();
- config.save(filename);
-
+ save(savedFile);
assertTrue("The saved file doesn't exist", savedFile.exists());
// read the configuration and compare the properties
- Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
+ XMLPropertyListConfiguration checkConfig = new XMLPropertyListConfiguration();
+ load(checkConfig, savedFile);
Iterator<String> it = config.getKeys();
while (it.hasNext())
@@ -298,22 +321,15 @@ public class TestXMLPropertyListConfigur
@Test
public void testSaveEmptyDictionary() throws Exception
{
- File savedFile = new File("target/testsave.plist.xml");
-
- // remove the file previously saved if necessary
- if (savedFile.exists())
- {
- assertTrue(savedFile.delete());
- }
+ File savedFile = folder.newFile();
// save the configuration
- String filename = savedFile.getAbsolutePath();
- config.save(filename);
-
+ save(savedFile);
assertTrue("The saved file doesn't exist", savedFile.exists());
// read the configuration and compare the properties
- Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
+ XMLPropertyListConfiguration checkConfig = new XMLPropertyListConfiguration();
+ load(checkConfig, savedFile);
assertEquals(null, config.getProperty("empty-dictionary"));
assertEquals(null, checkConfig.getProperty("empty-dictionary"));
@@ -326,12 +342,14 @@ public class TestXMLPropertyListConfigur
@Test
public void testSetDataProperty() throws Exception
{
+ File savedFile = folder.newFile();
byte[] expected = new byte[]{1, 2, 3, 4};
- XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
+ config = new XMLPropertyListConfiguration();
config.setProperty("foo", expected);
- config.save("target/testdata.plist.xml");
+ save(savedFile);
- XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
+ XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration();
+ load(config2, savedFile);
Object array = config2.getProperty("foo");
assertNotNull("data not found", array);
@@ -345,12 +363,14 @@ public class TestXMLPropertyListConfigur
@Test
public void testAddDataProperty() throws Exception
{
+ File savedFile = folder.newFile();
byte[] expected = new byte[]{1, 2, 3, 4};
- XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
+ config = new XMLPropertyListConfiguration();
config.addProperty("foo", expected);
- config.save("target/testdata.plist.xml");
+ save(savedFile);
- XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
+ XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration();
+ load(config2, savedFile);
Object array = config2.getProperty("foo");
assertNotNull("data not found", array);
@@ -361,7 +381,7 @@ public class TestXMLPropertyListConfigur
@Test
public void testInitCopy()
{
- XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration((HierarchicalConfiguration) config);
+ XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration(config);
StrictConfigurationComparator comp = new StrictConfigurationComparator();
assertTrue("Configurations are not equal", comp.compare(config, copy));
}
@@ -375,8 +395,7 @@ public class TestXMLPropertyListConfigur
public void testLoadNoDict() throws ConfigurationException
{
XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration();
- plist.setFile(ConfigurationAssert.getTestFile("test2.plist.xml"));
- plist.load();
+ load(plist, ConfigurationAssert.getTestFile("test2.plist.xml"));
assertFalse("Configuration is empty", plist.isEmpty());
}
@@ -388,8 +407,8 @@ public class TestXMLPropertyListConfigur
@Test
public void testLoadNoDictConstr() throws ConfigurationException
{
- XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration(
- ConfigurationAssert.getTestFile("test2.plist.xml"));
+ XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration();
+ load(plist, ConfigurationAssert.getTestFile("test2.plist.xml"));
assertFalse("Configuration is empty", plist.isEmpty());
}
@@ -401,9 +420,39 @@ public class TestXMLPropertyListConfigur
public void testSetDatePropertyInvalid() throws ConfigurationException
{
config.clear();
- config.setFile(ConfigurationAssert.getTestFile("test_invalid_date.plist.xml"));
- config.load();
+ load(config, ConfigurationAssert.getTestFile("test_invalid_date.plist.xml"));
assertEquals("'string' property", "value1", config.getString("string"));
assertFalse("Date property was loaded", config.containsKey("date"));
}
+
+ /**
+ * Tests the header of a saved file if no encoding is specified.
+ */
+ @Test
+ public void testSaveNoEncoding() throws ConfigurationException
+ {
+ StringWriter writer = new StringWriter();
+ new FileHandler(config).save(writer);
+ assertTrue("Wrong document header",
+ writer.toString().indexOf("<?xml version=\"1.0\"?>") >= 0);
+ }
+
+ /**
+ * Tests whether the encoding is written when saving a configuration.
+ */
+ @Test
+ public void testSaveWithEncoding() throws ConfigurationException
+ {
+ String encoding = "UTF-8";
+ FileHandler handler = new FileHandler(config);
+ handler.setEncoding(encoding);
+ StringWriter writer = new StringWriter();
+ handler.save(writer);
+ assertTrue(
+ "Encoding not found",
+ writer.toString()
+ .indexOf(
+ "<?xml version=\"1.0\" encoding=\"" + encoding
+ + "\"?>") >= 0);
+ }
}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfigurationEvents.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfigurationEvents.java?rev=1464157&r1=1464156&r2=1464157&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfigurationEvents.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/plist/TestXMLPropertyListConfigurationEvents.java Wed Apr 3 18:53:50 2013
@@ -19,8 +19,10 @@ package org.apache.commons.configuration
import java.io.File;
import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.ConfigurationAssert;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationRuntimeException;
+import org.apache.commons.configuration.io.FileHandler;
/**
* Test class for the events generated by XMLPropertyListConfiguration.
@@ -31,14 +33,16 @@ public class TestXMLPropertyListConfigur
AbstractTestPListEvents
{
/** Constant for the test file that will be loaded. */
- private static final File TEST_FILE = new File("conf/test.plist.xml");
+ private static final File TEST_FILE = ConfigurationAssert.getTestFile("test.plist.xml");
@Override
protected AbstractConfiguration createConfiguration()
{
try
{
- return new XMLPropertyListConfiguration(TEST_FILE);
+ XMLPropertyListConfiguration c = new XMLPropertyListConfiguration();
+ new FileHandler(c).load(TEST_FILE);
+ return c;
}
catch (ConfigurationException cex)
{