提交 f72c431e 编写于 作者: A Arjen Poutsma

SPR-7257 - AbstractMarshaller incorrectly expects DOMResult to already have a node

上级 0dc29cb2
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -189,7 +189,21 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
* @see #marshalDomNode(Object, org.w3c.dom.Node)
*/
protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException {
Assert.notNull(domResult.getNode(), "DOMResult does not contain Node");
if (domResult.getNode() == null) {
try {
synchronized (this.documentBuilderFactoryMonitor) {
if (this.documentBuilderFactory == null) {
this.documentBuilderFactory = createDocumentBuilderFactory();
}
}
DocumentBuilder documentBuilder = createDocumentBuilder(this.documentBuilderFactory);
domResult.setNode(documentBuilder.newDocument());
}
catch (ParserConfigurationException ex) {
throw new UnmarshallingFailureException(
"Could not create document placeholder for DOMResult: " + ex.getMessage(), ex);
}
}
marshalDomNode(graph, domResult.getNode());
}
......
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -28,8 +28,9 @@ import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stream.StreamResult;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.custommonkey.xmlunit.XMLAssert.*;
import org.custommonkey.xmlunit.XMLUnit;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Attr;
......@@ -86,6 +87,30 @@ public abstract class AbstractMarshallerTests {
assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
}
@Test
public void marshalEmptyDOMResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
DOMResult domResult = new DOMResult();
marshaller.marshal(flights, domResult);
assertTrue("DOMResult does not contain a Document", domResult.getNode() instanceof Document);
Document result = (Document) domResult.getNode();
Document expected = builder.newDocument();
Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
namespace.setNodeValue("http://samples.springframework.org/flight");
flightsElement.setAttributeNode(namespace);
expected.appendChild(flightsElement);
Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
flightsElement.appendChild(flightElement);
Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
}
@Test
public void marshalStreamResultWriter() throws Exception {
StringWriter writer = new StringWriter();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册