1   /*
2    * Copyright 2005-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.XMLEventReader;
24  import javax.xml.stream.XMLInputFactory;
25  import javax.xml.stream.XMLStreamReader;
26  import javax.xml.transform.Source;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.dom.DOMSource;
29  import javax.xml.transform.sax.SAXSource;
30  import javax.xml.transform.stream.StreamSource;
31  
32  import org.springframework.util.xml.StaxUtils;
33  
34  import org.junit.Assert;
35  import org.junit.Test;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Element;
38  import org.xml.sax.InputSource;
39  
40  public class PayloadRootUtilsTest {
41  
42      @Test
43      public void testGetQNameForDomSource() throws Exception {
44          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45          DocumentBuilder builder = factory.newDocumentBuilder();
46          Document document = builder.newDocument();
47          Element element = document.createElementNS("namespace", "prefix:localname");
48          document.appendChild(element);
49          Source source = new DOMSource(document);
50          QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
51          Assert.assertNotNull("getQNameForNode returns null", qName);
52          Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
53          Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
54          Assert.assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
55      }
56  
57      @Test
58      public void testGetQNameForStaxSourceStreamReader() throws Exception {
59          String contents = "<prefix:localname xmlns:prefix='namespace'/>";
60          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
61          XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(contents));
62          Source source = StaxUtils.createStaxSource(streamReader);
63          QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
64          Assert.assertNotNull("getQNameForNode returns null", qName);
65          Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
66          Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
67          Assert.assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
68      }
69  
70      @Test
71      public void testGetQNameForStaxSourceEventReader() throws Exception {
72          String contents = "<prefix:localname xmlns:prefix='namespace'/>";
73          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
74          XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(contents));
75          Source source = StaxUtils.createStaxSource(eventReader);
76          QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
77          Assert.assertNotNull("getQNameForNode returns null", qName);
78          Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
79          Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
80          Assert.assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
81      }
82  
83      @Test
84      public void testGetQNameForStreamSource() throws Exception {
85          String contents = "<prefix:localname xmlns:prefix='namespace'/>";
86          Source source = new StreamSource(new StringReader(contents));
87          QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
88          Assert.assertNotNull("getQNameForNode returns null", qName);
89          Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
90          Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
91          Assert.assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
92      }
93  
94      @Test
95      public void testGetQNameForSaxSource() throws Exception {
96          String contents = "<prefix:localname xmlns:prefix='namespace'/>";
97          Source source = new SAXSource(new InputSource(new StringReader(contents)));
98          QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
99          Assert.assertNotNull("getQNameForNode returns null", qName);
100         Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
101         Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
102         Assert.assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
103     }
104 
105     @Test
106     public void testGetQNameForNullSource() throws Exception {
107         QName qName = PayloadRootUtils.getPayloadRootQName(null, TransformerFactory.newInstance());
108         Assert.assertNull("Qname returned", qName);
109     }
110 }