Author: oheger Date: Sun Jan 24 16:28:55 2010 New Revision: 902596 URL: http://svn.apache.org/viewvc?rev=902596&view=rev Log: [CONFIGURATION-405] Make sure that the root node of XMLPropertyListConfiguration is a PListNode. Otherwise a ConfigurationException is thrown when loading configuration files that do not have an outer dict element.
Added: commons/proper/configuration/trunk/conf/test2.plist.xml (with props) Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java commons/proper/configuration/trunk/xdocs/changes.xml Added: commons/proper/configuration/trunk/conf/test2.plist.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/conf/test2.plist.xml?rev=902596&view=auto ============================================================================== --- commons/proper/configuration/trunk/conf/test2.plist.xml (added) +++ commons/proper/configuration/trunk/conf/test2.plist.xml Sun Jan 24 16:28:55 2010 @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd"> +<plist version="1.0"> + <array> + <array> + <string>http://www.google.com/search?hl=en&client=safari&rls=en&q=RFC822MessageDatasPboardType&btnG=Search&aq=f&oq=&aqi=</string> + </array> + <array> + <string>RFC822MessageDatasPboardType - Google Search</string> + </array> + </array> +</plist> + Propchange: commons/proper/configuration/trunk/conf/test2.plist.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/configuration/trunk/conf/test2.plist.xml ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/configuration/trunk/conf/test2.plist.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java?rev=902596&r1=902595&r2=902596&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java (original) +++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java Sun Jan 24 16:28:55 2010 @@ -136,6 +136,7 @@ */ public XMLPropertyListConfiguration() { + initRoot(); } /** @@ -224,6 +225,14 @@ public void load(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 + // root node is a plain Node. + if (!(getRootNode() instanceof PListNode)) + { + initRoot(); + } + // set up the DTD validation EntityResolver resolver = new EntityResolver() { @@ -415,6 +424,14 @@ } /** + * Helper method for initializing the configuration's root node. + */ + private void initRoot() + { + setRootNode(new PListNode()); + } + + /** * SAX Handler to build the configuration nodes while the document is being parsed. */ private static class XMLPropertyListHandler extends DefaultHandler Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java?rev=902596&r1=902595&r2=902596&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java (original) +++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java Sun Jan 24 16:28:55 2010 @@ -18,17 +18,22 @@ package org.apache.commons.configuration.plist; import java.io.File; -import java.util.*; +import java.util.Calendar; +import java.util.Iterator; +import java.util.List; +import java.util.TimeZone; import junit.framework.TestCase; -import junitx.framework.ObjectAssert; import junitx.framework.ArrayAssert; import junitx.framework.ListAssert; -import org.apache.commons.configuration.FileConfiguration; +import junitx.framework.ObjectAssert; + import org.apache.commons.configuration.Configuration; +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.ConfigurationComparator; /** * @author Emmanuel Bourg @@ -279,7 +284,7 @@ { assertTrue(savedFile.delete()); } - + // save the configuration String filename = savedFile.getAbsolutePath(); config.save(filename); @@ -288,7 +293,7 @@ // read the configuration and compare the properties Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename)); - + assertEquals(null, config.getProperty("empty-dictionary")); assertEquals(null, checkConfig.getProperty("empty-dictionary")); } @@ -336,4 +341,29 @@ StrictConfigurationComparator comp = new StrictConfigurationComparator(); assertTrue("Configurations are not equal", comp.compare(config, copy)); } + + /** + * Tests whether a configuration can be loaded that does not start with a + * <code>dict</code> element. This test case is related to + * CONFIGURATION-405. + */ + public void testLoadNoDict() throws ConfigurationException + { + XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration(); + plist.setFileName("conf/test2.plist.xml"); + plist.load(); + assertFalse("Configuration is empty", plist.isEmpty()); + } + + /** + * Tests whether a configuration that does not start with a + * <code>dict</code> element can be loaded from a constructor. This test + * case is related to CONFIGURATION-405. + */ + public void testLoadNoDictConstr() throws ConfigurationException + { + XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration( + "conf/test2.plist.xml"); + assertFalse("Configuration is empty", plist.isEmpty()); + } } Modified: commons/proper/configuration/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=902596&r1=902595&r2=902596&view=diff ============================================================================== --- commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ commons/proper/configuration/trunk/xdocs/changes.xml Sun Jan 24 16:28:55 2010 @@ -23,6 +23,10 @@ <body> <release version="1.7" date="in SVN" description=""> + <action dev="oheger" type="fix" issue="CONFIGURATION-405"> + XMLPropertyListConfiguration no longer throws a ConfigurationException + if the file to be loaded does not have an outer dict element. + </action> <action dev="oheger" type="fix" issue="CONFIGURATION-403"> When an empty XMLConfiguration was saved and reloaded the root element was assigned an empty text value. Because of this isEmpty() returned