1   /*
2    * Copyright 2007 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.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", "", "http://springframework.org/spring-ws/1");
59          Iterator iterator = envelope.getBody().getChildElements(rootName);
60          assertTrue("No child found", iterator.hasNext());
61          SOAPBodyElement rootElement = (SOAPBodyElement) iterator.next();
62          Name childName = envelope.createName("Child", "child", "http://springframework.org/spring-ws/2");
63          iterator = rootElement.getChildElements(childName);
64          assertTrue("No child found", iterator.hasNext());
65          SOAPElement childElement = (SOAPElement) iterator.next();
66          assertEquals("Invalid contents", "Content", childElement.getValue());
67          Name attributeName = envelope.createName("attribute");
68          assertEquals("Invalid attribute value", "value", childElement.getAttributeValue(attributeName));
69      }
70  }