001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.server.endpoint.support;
018:
019: import java.io.StringReader;
020: import javax.xml.namespace.QName;
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.stream.XMLInputFactory;
024: import javax.xml.stream.XMLStreamConstants;
025: import javax.xml.stream.XMLStreamReader;
026: import javax.xml.transform.Source;
027: import javax.xml.transform.TransformerFactory;
028: import javax.xml.transform.dom.DOMSource;
029: import javax.xml.transform.sax.SAXSource;
030: import javax.xml.transform.stream.StreamSource;
031:
032: import junit.framework.TestCase;
033: import org.springframework.xml.transform.StaxSource;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.xml.sax.InputSource;
037:
038: public class PayloadRootUtilsTest extends TestCase {
039:
040: public void testGetQNameForDomSource() throws Exception {
041: DocumentBuilderFactory factory = DocumentBuilderFactory
042: .newInstance();
043: DocumentBuilder builder = factory.newDocumentBuilder();
044: Document document = builder.newDocument();
045: Element element = document.createElementNS("namespace",
046: "prefix:localname");
047: document.appendChild(element);
048: Source source = new DOMSource(document);
049: QName qName = PayloadRootUtils.getPayloadRootQName(source,
050: TransformerFactory.newInstance());
051: assertNotNull("getQNameForNode returns null", qName);
052: assertEquals("QName has invalid localname", "localname", qName
053: .getLocalPart());
054: assertEquals("Qname has invalid namespace", "namespace", qName
055: .getNamespaceURI());
056: assertEquals("Qname has invalid prefix", "prefix", qName
057: .getPrefix());
058: }
059:
060: public void testGetQNameForStaxSource() throws Exception {
061: String contents = "<prefix:localname xmlns:prefix='namespace'/>";
062: XMLInputFactory inputFactory = XMLInputFactory.newInstance();
063: XMLStreamReader streamReader = inputFactory
064: .createXMLStreamReader(new StringReader(contents));
065: while (streamReader.getEventType() != XMLStreamConstants.START_ELEMENT) {
066: streamReader.next();
067: }
068: Source source = new StaxSource(streamReader);
069: QName qName = PayloadRootUtils.getPayloadRootQName(source,
070: TransformerFactory.newInstance());
071: assertNotNull("getQNameForNode returns null", qName);
072: assertEquals("QName has invalid localname", "localname", qName
073: .getLocalPart());
074: assertEquals("Qname has invalid namespace", "namespace", qName
075: .getNamespaceURI());
076: assertEquals("Qname has invalid prefix", "prefix", qName
077: .getPrefix());
078: }
079:
080: public void testGetQNameForStreamSource() throws Exception {
081: String contents = "<prefix:localname xmlns:prefix='namespace'/>";
082: Source source = new StreamSource(new StringReader(contents));
083: QName qName = PayloadRootUtils.getPayloadRootQName(source,
084: TransformerFactory.newInstance());
085: assertNotNull("getQNameForNode returns null", qName);
086: assertEquals("QName has invalid localname", "localname", qName
087: .getLocalPart());
088: assertEquals("Qname has invalid namespace", "namespace", qName
089: .getNamespaceURI());
090: assertEquals("Qname has invalid prefix", "prefix", qName
091: .getPrefix());
092: }
093:
094: public void testGetQNameForSaxSource() throws Exception {
095: String contents = "<prefix:localname xmlns:prefix='namespace'/>";
096: Source source = new SAXSource(new InputSource(new StringReader(
097: contents)));
098: QName qName = PayloadRootUtils.getPayloadRootQName(source,
099: TransformerFactory.newInstance());
100: assertNotNull("getQNameForNode returns null", qName);
101: assertEquals("QName has invalid localname", "localname", qName
102: .getLocalPart());
103: assertEquals("Qname has invalid namespace", "namespace", qName
104: .getNamespaceURI());
105: assertEquals("Qname has invalid prefix", "prefix", qName
106: .getPrefix());
107: }
108: }
|