This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git

commit fd9a35131512eb86c7320f16a2803134c559e3c9
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Apr 2 07:09:58 2026 -0400

    Add a test to read from an XML DOM Node.
---
 .../configuration2/TestXMLConfiguration.java       | 34 ++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git 
a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java 
b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
index 5ca800367..e5d179c66 100644
--- a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
@@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -41,8 +42,14 @@ import java.util.List;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
 import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 
 import org.apache.commons.configuration2.SynchronizerTestImpl.Methods;
 import 
org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
@@ -51,6 +58,7 @@ import 
org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
 import org.apache.commons.configuration2.convert.DisabledListDelimiterHandler;
 import org.apache.commons.configuration2.ex.ConfigurationException;
 import org.apache.commons.configuration2.io.FileHandler;
+import org.apache.commons.configuration2.io.FileLocatorUtils;
 import org.apache.commons.configuration2.resolver.CatalogResolver;
 import org.apache.commons.configuration2.tree.ImmutableNode;
 import org.apache.commons.configuration2.tree.NodeStructureHelper;
@@ -60,6 +68,7 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 import org.w3c.dom.Document;
+import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.helpers.DefaultHandler;
@@ -141,6 +150,16 @@ public class TestXMLConfiguration {
         handler.load();
     }
 
+    private static byte[] nodeToByteArray(final Node node) throws 
TransformerException {
+        final Source source = new DOMSource(node);
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        final Result result = new StreamResult(bos);
+        final TransformerFactory factory = TransformerFactory.newInstance();
+        factory.newTransformer().transform(source, result);
+        // 4. Return the resulting byte array
+        return bos.toByteArray();
+    }
+
     /** A folder for temporary files. */
     @TempDir
     public File tempFolder;
@@ -1057,6 +1076,21 @@ public class TestXMLConfiguration {
         assertTrue(e.getMessage().contains("FileHandler"));
     }
 
+    /**
+     * Tests how to read from a DOM Node.
+     */
+    @Test
+    void testReadDomNode() throws Exception {
+         conf = new XMLConfiguration();
+         final String content = "<configuration><test 
attr=\"x\">1</test></configuration>";
+         final Node document = 
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new 
ByteArrayInputStream(content.getBytes()));
+         final Node node = document.getFirstChild().getFirstChild(); // <test>
+         conf.initFileLocator(FileLocatorUtils.fileLocator().create());
+         // Read from a DOM Node
+         conf.read(new ByteArrayInputStream(nodeToByteArray(node)));
+         assertEquals("x", conf.getString("[@attr]"));
+    }
+
     @Test
     void testSave() throws Exception {
         // add an array of strings to the configuration

Reply via email to