[CONFIGURATION-767] NullPointerException in
XMLConfiguration#createTransformer() when no FileLocator is set.
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 542a735..ad5d6da 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -41,6 +41,9 @@
<action dev="ggregory" type="add" issue="CONFIGURATION-765" due-to="Gary Gregory">
Refactor XMLConfiguration.write(Writer) to add XMLConfiguration.write(Writer, Transformer).
</action>
+ <action dev="ggregory" type="fix" issue="CONFIGURATION-767" due-to="Gary Gregory">
+ NullPointerException in XMLConfiguration#createTransformer() when no FileLocator is set.
+ </action>
</release>
<release version="2.6" date="2019-09-13"
description="Minor release with new features and updated dependencies.">
diff --git a/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java b/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
index d74005a..469b226 100644
--- a/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
@@ -881,19 +881,17 @@
final Transformer transformer = XMLDocumentHelper.createTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- if (locator.getEncoding() != null)
+ if (locator != null && locator.getEncoding() != null)
{
transformer.setOutputProperty(OutputKeys.ENCODING, locator.getEncoding());
}
if (publicID != null)
{
- transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
- publicID);
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicID);
}
if (systemID != null)
{
- transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
- systemID);
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemID);
}
return transformer;
diff --git a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
index dd8a3a7..840bc96 100644
--- a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
@@ -26,21 +26,23 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerFactoryConfigurationError;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+
import org.apache.commons.configuration2.SynchronizerTestImpl.Methods;
import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
@@ -52,10 +54,12 @@
import org.apache.commons.configuration2.tree.ImmutableNode;
import org.apache.commons.configuration2.tree.NodeStructureHelper;
import org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine;
+import org.junit.Assert;
import org.junit.Before;
-import org.junit.Test;
import org.junit.Rule;
+import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
@@ -230,6 +234,11 @@
return builder;
}
+ private Document parseXml(final String xml) throws SAXException, IOException, ParserConfigurationException {
+ return DocumentBuilderFactory.newInstance().newDocumentBuilder()
+ .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
+ }
+
/**
* Removes the test output file if it exists.
*/
@@ -965,13 +974,13 @@
assertEquals("three", list.get(1));
}
+
@Test
public void testGetProperty()
{
assertEquals("value", conf.getProperty("element"));
}
-
@Test
public void testGetPropertyWithXMLEntity()
{
@@ -1764,7 +1773,17 @@
conf.setValidating(true);
load(conf, "testValidateInvalid.xml");
}
-
+
+ @Test
+ public void testWrite() throws Exception
+ {
+ XMLConfiguration xmlConfig = new XMLConfiguration();
+ xmlConfig.setRootElementName("IAmRoot");
+ StringWriter sw = new StringWriter();
+ xmlConfig.write(sw);
+ // Check that we can parse the XML.
+ Assert.assertNotNull(parseXml(sw.toString()));
+ }
/**
* Tests accessing properties when the XPATH expression engine is set.
*/