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 javax.xml.soap.MessageFactory;
20  import javax.xml.soap.SOAPEnvelope;
21  import javax.xml.soap.SOAPMessage;
22  import javax.xml.transform.Source;
23  import javax.xml.transform.Transformer;
24  import javax.xml.transform.TransformerFactory;
25  import javax.xml.transform.dom.DOMResult;
26  import javax.xml.transform.dom.DOMSource;
27  import javax.xml.transform.sax.SAXSource;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.w3c.dom.Document;
32  import org.xml.sax.InputSource;
33  
34  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
35  
36  public class SaajXmlReaderTest {
37  
38      private SaajXmlReader saajReader;
39  
40      private SOAPMessage message;
41  
42      private Transformer transformer;
43  
44      @Before
45      public void setUp() throws Exception {
46          MessageFactory messageFactory = MessageFactory.newInstance();
47          message = messageFactory.createMessage();
48          SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
49          saajReader = new SaajXmlReader(envelope);
50          transformer = TransformerFactory.newInstance().newTransformer();
51      }
52  
53      @Test
54      public void testNamespacesPrefixes() throws Exception {
55          saajReader.setFeature("http://xml.org/sax/features/namespaces", true);
56          saajReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
57          DOMResult result = new DOMResult();
58          Source source = new SAXSource(saajReader, new InputSource());
59          transformer.transform(source, result);
60          DOMResult expected = new DOMResult();
61          transformer.transform(new DOMSource(message.getSOAPPart().getEnvelope()), expected);
62          assertXMLEqual((Document) expected.getNode(), (Document) result.getNode());
63      }
64  
65      @Test
66      public void testNamespacesNoPrefixes() throws Exception {
67          saajReader.setFeature("http://xml.org/sax/features/namespaces", true);
68          saajReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
69          DOMResult result = new DOMResult();
70          Source source = new SAXSource(saajReader, new InputSource());
71          transformer.transform(source, result);
72          DOMResult expected = new DOMResult();
73          transformer.transform(new DOMSource(message.getSOAPPart().getEnvelope()), expected);
74          assertXMLEqual((Document) expected.getNode(), (Document) result.getNode());
75      }
76  }