1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.server.endpoint.support;
18
19 import java.io.StringReader;
20 import javax.xml.namespace.QName;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.stream.XMLInputFactory;
24 import javax.xml.stream.XMLStreamReader;
25 import javax.xml.transform.Source;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.sax.SAXSource;
29 import javax.xml.transform.stream.StreamSource;
30
31 import junit.framework.TestCase;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.xml.sax.InputSource;
35
36 import org.springframework.xml.transform.StaxSource;
37
38 public class PayloadRootUtilsTest extends TestCase {
39
40 public void testGetQNameForDomSource() throws Exception {
41 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
42 DocumentBuilder builder = factory.newDocumentBuilder();
43 Document document = builder.newDocument();
44 Element element = document.createElementNS("namespace", "prefix:localname");
45 document.appendChild(element);
46 Source source = new DOMSource(document);
47 QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
48 assertNotNull("getQNameForNode returns null", qName);
49 assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
50 assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
51 assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
52 }
53
54 public void testGetQNameForStaxSource() throws Exception {
55 String contents = "<prefix:localname xmlns:prefix='namespace'/>";
56 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
57 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(contents));
58 Source source = new StaxSource(streamReader);
59 QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
60 assertNotNull("getQNameForNode returns null", qName);
61 assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
62 assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
63 assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
64 }
65
66 public void testGetQNameForStreamSource() throws Exception {
67 String contents = "<prefix:localname xmlns:prefix='namespace'/>";
68 Source source = new StreamSource(new StringReader(contents));
69 QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
70 assertNotNull("getQNameForNode returns null", qName);
71 assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
72 assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
73 assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
74 }
75
76 public void testGetQNameForSaxSource() throws Exception {
77 String contents = "<prefix:localname xmlns:prefix='namespace'/>";
78 Source source = new SAXSource(new InputSource(new StringReader(contents)));
79 QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
80 assertNotNull("getQNameForNode returns null", qName);
81 assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
82 assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
83 assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
84 }
85
86 public void testGetQNameForNullSource() throws Exception {
87 QName qName = PayloadRootUtils.getPayloadRootQName(null, TransformerFactory.newInstance());
88 assertNull("Qname returned", qName);
89 }
90 }