001: /*
002: * Copyright 2006 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.soap.saaj.support;
018:
019: import javax.xml.namespace.QName;
020: import javax.xml.parsers.DocumentBuilder;
021: import javax.xml.parsers.DocumentBuilderFactory;
022: import javax.xml.soap.MessageFactory;
023: import javax.xml.soap.Name;
024: import javax.xml.soap.SOAPEnvelope;
025: import javax.xml.soap.SOAPMessage;
026:
027: import org.custommonkey.xmlunit.XMLTestCase;
028: import org.springframework.core.io.ClassPathResource;
029: import org.springframework.util.StringUtils;
030: import org.w3c.dom.Document;
031:
032: public class SaajUtilsTest extends XMLTestCase {
033:
034: private MessageFactory messageFactory;
035:
036: protected void setUp() throws Exception {
037: messageFactory = MessageFactory.newInstance();
038: }
039:
040: public void testToName() throws Exception {
041: SOAPMessage message = messageFactory.createMessage();
042: QName qName = new QName("localPart");
043: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
044: Name name = SaajUtils.toName(qName, envelope);
045: assertNotNull("Invalid name", name);
046: assertEquals("Invalid local part", qName.getLocalPart(), name
047: .getLocalName());
048: assertFalse("Invalid prefix", StringUtils.hasLength(name
049: .getPrefix()));
050: assertFalse("Invalid namespace", StringUtils.hasLength(name
051: .getURI()));
052: }
053:
054: public void testToNameNamespace() throws Exception {
055: SOAPMessage message = messageFactory.createMessage();
056: QName qName = new QName("namespace", "localPart");
057: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
058: envelope.addNamespaceDeclaration("prefix", "namespace");
059: Name name = SaajUtils.toName(qName, envelope);
060: assertNotNull("Invalid name", name);
061: assertEquals("Invalid namespace", qName.getNamespaceURI(), name
062: .getURI());
063: assertEquals("Invalid local part", qName.getLocalPart(), name
064: .getLocalName());
065: assertEquals("Invalid prefix", "prefix", name.getPrefix());
066: }
067:
068: public void testToNameNamespacePrefix() throws Exception {
069: SOAPMessage message = messageFactory.createMessage();
070: QName qName = new QName("namespace", "localPart", "prefix");
071: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
072: Name name = SaajUtils.toName(qName, envelope);
073: assertNotNull("Invalid name", name);
074: assertEquals("Invalid namespace", qName.getNamespaceURI(), name
075: .getURI());
076: assertEquals("Invalid local part", qName.getLocalPart(), name
077: .getLocalName());
078: assertEquals("Invalid prefix", qName.getPrefix(), name
079: .getPrefix());
080: }
081:
082: public void testToQName() throws Exception {
083: SOAPMessage message = messageFactory.createMessage();
084: Name name = message.getSOAPPart().getEnvelope().createName(
085: "localPart", null, null);
086: QName qName = SaajUtils.toQName(name);
087: assertNotNull("Invalid qName", qName);
088: assertEquals("Invalid namespace", name.getURI(), qName
089: .getNamespaceURI());
090: assertFalse("Invalid prefix", StringUtils.hasLength(qName
091: .getPrefix()));
092: assertFalse("Invalid namespace", StringUtils.hasLength(qName
093: .getNamespaceURI()));
094: }
095:
096: public void testToQNameNamespace() throws Exception {
097: SOAPMessage message = messageFactory.createMessage();
098: Name name = message.getSOAPPart().getEnvelope().createName(
099: "localPart", null, "namespace");
100: QName qName = SaajUtils.toQName(name);
101: assertNotNull("Invalid qName", qName);
102: assertEquals("Invalid namespace", name.getURI(), qName
103: .getNamespaceURI());
104: assertEquals("Invalid local part", name.getLocalName(), qName
105: .getLocalPart());
106: assertFalse("Invalid prefix", StringUtils.hasLength(qName
107: .getPrefix()));
108: }
109:
110: public void testToQNamePrefixNamespace() throws Exception {
111: SOAPMessage message = messageFactory.createMessage();
112: Name name = message.getSOAPPart().getEnvelope().createName(
113: "localPart", "prefix", "namespace");
114: QName qName = SaajUtils.toQName(name);
115: assertNotNull("Invalid qName", qName);
116: assertEquals("Invalid namespace", name.getURI(), qName
117: .getNamespaceURI());
118: assertEquals("Invalid local part", name.getLocalName(), qName
119: .getLocalPart());
120: assertEquals("Invalid prefix", name.getPrefix(), qName
121: .getPrefix());
122: }
123:
124: public void testLoadMessage() throws Exception {
125: DocumentBuilderFactory factory = DocumentBuilderFactory
126: .newInstance();
127: factory.setNamespaceAware(true);
128: DocumentBuilder builder = factory.newDocumentBuilder();
129: Document document = builder.parse(getClass()
130: .getResourceAsStream("soapMessage.xml"));
131: SOAPMessage soapMessage = SaajUtils.loadMessage(
132: new ClassPathResource("soapMessage.xml", getClass()),
133: messageFactory);
134: assertXMLEqual(soapMessage.getSOAPPart(), document);
135: }
136:
137: public void testGetSaajVersion() throws Exception {
138: assertEquals("Invalid SAAJ version", SaajUtils.SAAJ_13,
139: SaajUtils.getSaajVersion());
140: }
141: }
|