01: /*
02: * Copyright 2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.soap.saaj.support;
18:
19: import java.util.Iterator;
20: import javax.xml.soap.MessageFactory;
21: import javax.xml.soap.Name;
22: import javax.xml.soap.SOAPBodyElement;
23: import javax.xml.soap.SOAPElement;
24: import javax.xml.soap.SOAPEnvelope;
25: import javax.xml.soap.SOAPMessage;
26: import javax.xml.transform.Result;
27: import javax.xml.transform.Source;
28: import javax.xml.transform.Transformer;
29: import javax.xml.transform.TransformerFactory;
30: import javax.xml.transform.sax.SAXResult;
31:
32: import junit.framework.TestCase;
33: import org.springframework.xml.transform.StringSource;
34:
35: public class SaajContentHandlerTest extends TestCase {
36:
37: private SaajContentHandler handler;
38:
39: private Transformer transformer;
40:
41: private SOAPEnvelope envelope;
42:
43: protected void setUp() throws Exception {
44: MessageFactory messageFactory = MessageFactory.newInstance();
45: SOAPMessage message = messageFactory.createMessage();
46: envelope = message.getSOAPPart().getEnvelope();
47: handler = new SaajContentHandler(envelope.getBody());
48: transformer = TransformerFactory.newInstance().newTransformer();
49: }
50:
51: public void testHandler() throws Exception {
52: String content = "<Root xmlns='http://springframework.org/spring-ws/1' "
53: + "xmlns:child='http://springframework.org/spring-ws/2'>"
54: + "<child:Child attribute='value'>Content</child:Child></Root>";
55: Source source = new StringSource(content);
56: Result result = new SAXResult(handler);
57: transformer.transform(source, result);
58: Name rootName = envelope.createName("Root", "",
59: "http://springframework.org/spring-ws/1");
60: Iterator iterator = envelope.getBody().getChildElements(
61: rootName);
62: assertTrue("No child found", iterator.hasNext());
63: SOAPBodyElement rootElement = (SOAPBodyElement) iterator.next();
64: Name childName = envelope.createName("Child", "child",
65: "http://springframework.org/spring-ws/2");
66: iterator = rootElement.getChildElements(childName);
67: assertTrue("No child found", iterator.hasNext());
68: SOAPElement childElement = (SOAPElement) iterator.next();
69: assertEquals("Invalid contents", "Content", childElement
70: .getValue());
71: Name attributeName = envelope.createName("attribute");
72: assertEquals("Invalid attribute value", "value", childElement
73: .getAttributeValue(attributeName));
74: }
75: }
|