1   /*
2    * Copyright 2005-2010 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 org.springframework.xml.transform.StringSource;
33  
34  import org.junit.Assert;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  public class SaajContentHandlerTest {
39  
40      private SaajContentHandler handler;
41  
42      private Transformer transformer;
43  
44      private SOAPEnvelope envelope;
45  
46      @Before
47      public void setUp() throws Exception {
48          MessageFactory messageFactory = MessageFactory.newInstance();
49          SOAPMessage message = messageFactory.createMessage();
50          envelope = message.getSOAPPart().getEnvelope();
51          handler = new SaajContentHandler(envelope.getBody());
52          transformer = TransformerFactory.newInstance().newTransformer();
53      }
54  
55      @Test
56      public void testHandler() throws Exception {
57          String content = "<Root xmlns='http://springframework.org/spring-ws/1' " +
58                  "xmlns:child='http://springframework.org/spring-ws/2'>" +
59                  "<child:Child attribute='value'>Content</child:Child></Root>";
60          Source source = new StringSource(content);
61          Result result = new SAXResult(handler);
62          transformer.transform(source, result);
63          Name rootName = envelope.createName("Root", "", "http://springframework.org/spring-ws/1");
64          Iterator<?> iterator = envelope.getBody().getChildElements(rootName);
65          Assert.assertTrue("No child found", iterator.hasNext());
66          SOAPBodyElement rootElement = (SOAPBodyElement) iterator.next();
67          Name childName = envelope.createName("Child", "child", "http://springframework.org/spring-ws/2");
68          iterator = rootElement.getChildElements(childName);
69          Assert.assertTrue("No child found", iterator.hasNext());
70          SOAPElement childElement = (SOAPElement) iterator.next();
71          Assert.assertEquals("Invalid contents", "Content", childElement.getValue());
72          Name attributeName = envelope.createName("attribute");
73          Assert.assertEquals("Invalid attribute value", "value", childElement.getAttributeValue(attributeName));
74      }
75  }