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