Author: oheger
Date: Wed Apr 11 15:42:31 2018
New Revision: 1828907

URL: http://svn.apache.org/viewvc?rev=1828907&view=rev
Log:
CONFIGURATION-652: XML namespace attributes are now set correctly.

When loading and saving XML configuration files the attributes of the
root node are now updated correctly. Thanks to claude at xenei dot com
for the patch.

Added:
    commons/proper/configuration/trunk/src/test/resources/testChildNamespace.xml
    commons/proper/configuration/trunk/src/test/resources/testRootNamespace.xml
Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java?rev=1828907&r1=1828906&r2=1828907&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
 Wed Apr 11 15:42:31 2018
@@ -1135,6 +1135,7 @@ public class XMLConfiguration extends Ba
          */
         public void processDocument(ReferenceNodeHandler refHandler)
         {
+            updateAttributes(refHandler.getRootNode(), 
document.getDocumentElement() );
             NodeTreeWalker.INSTANCE.walkDFS(refHandler.getRootNode(), this,
                     refHandler);
         }

Modified: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java?rev=1828907&r1=1828906&r2=1828907&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
 Wed Apr 11 15:42:31 2018
@@ -398,6 +398,20 @@ public class TestXMLConfiguration
     }
 
     @Test
+    public void testSetRootNamespace() throws ConfigurationException
+    {
+        conf.addProperty(  "[@xmlns:foo]",  "http://example.com/"; );
+        conf.addProperty(  "foo:bar", "foobar" );
+        assertEquals("Root attribute not set", "http://example.com/";, conf
+                .getString("[@xmlns:foo]"));
+        saveTestConfig();
+        XMLConfiguration checkConf = checkSavedConfig();
+        assertTrue("Attribute not found after save", checkConf
+                .containsKey("[@xmlns:foo]"));
+        checkConf.setProperty("[@xmlns:foo]", "http://example.net/";);
+    }
+
+    @Test
     public void testAddList()
     {
         conf.addProperty("test.array", "value1");
@@ -918,6 +932,23 @@ public class TestXMLConfiguration
         assertEquals("test3_yoge", conf.getString("yoge"));
     }
 
+
+    @Test
+    public void testLoadWithRootNamespace() throws ConfigurationException
+    {
+        conf = new XMLConfiguration();
+        new 
FileHandler(conf).load(ConfigurationAssert.getTestFile("testRootNamespace.xml"));
+        assertEquals("http://example.com/";, conf.getString("[@xmlns:foo]"));
+    }
+
+    @Test
+    public void testLoadChildNamespace() throws ConfigurationException
+    {
+        conf = new XMLConfiguration();
+        new 
FileHandler(conf).load(ConfigurationAssert.getTestFile("testChildNamespace.xml"));
+        assertEquals("http://example.com/";, 
conf.getString("foo:bar.[@xmlns:foo]"));
+    }
+
     /**
      * Tests whether the encoding is written to the generated XML file.
      */
@@ -935,6 +966,35 @@ public class TestXMLConfiguration
                 containsString("encoding=\"" + ENCODING + "\""));
     }
 
+    @Test
+    public void testSaveWithRootAttributes() throws ConfigurationException
+    {
+        conf.setProperty("[@xmlns:ex]", "http://example.com/";);
+        assertEquals("Root attribute not set", "http://example.com/";, conf
+                .getString("[@xmlns:ex]"));
+        FileHandler handler = new FileHandler(conf);
+
+        StringWriter out = new StringWriter();
+        handler.save(out);
+        assertThat("Encoding was not written to file", out.toString(),
+                containsString("testconfig xmlns:ex=\"http://example.com/\"";));
+    }
+
+    @Test
+    public void testSaveWithRootAttributes_ByHand() throws 
ConfigurationException
+    {
+        conf = new XMLConfiguration();
+        conf.addProperty(  "[@xmlns:foo]",  "http://example.com/"; );
+        assertEquals("Root attribute not set", "http://example.com/";, conf
+                .getString("[@xmlns:foo]"));
+        FileHandler handler = new FileHandler(conf);
+
+        StringWriter out = new StringWriter();
+        handler.save(out);
+        assertThat("Encoding was not written to file", out.toString(),
+                containsString("configuration 
xmlns:foo=\"http://example.com/\"";));
+    }
+
     /**
      * Tests whether a default encoding is used if no specific encoding is set.
      * According to the XSLT specification (http://www.w3.org/TR/xslt#output)

Added: 
commons/proper/configuration/trunk/src/test/resources/testChildNamespace.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testChildNamespace.xml?rev=1828907&view=auto
==============================================================================
Binary files 
commons/proper/configuration/trunk/src/test/resources/testChildNamespace.xml 
(added) and 
commons/proper/configuration/trunk/src/test/resources/testChildNamespace.xml 
Wed Apr 11 15:42:31 2018 differ

Added: 
commons/proper/configuration/trunk/src/test/resources/testRootNamespace.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testRootNamespace.xml?rev=1828907&view=auto
==============================================================================
Binary files 
commons/proper/configuration/trunk/src/test/resources/testRootNamespace.xml 
(added) and 
commons/proper/configuration/trunk/src/test/resources/testRootNamespace.xml Wed 
Apr 11 15:42:31 2018 differ


Reply via email to