1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activesoap;
19  
20  import junit.framework.TestCase;
21  import org.apache.xmlbeans.XmlObject;
22  import org.apache.xmlbeans.XmlOptions;
23  
24  import javax.xml.stream.XMLInputFactory;
25  import javax.xml.stream.XMLOutputFactory;
26  import javax.xml.stream.XMLStreamException;
27  import javax.xml.stream.XMLStreamReader;
28  import javax.xml.stream.XMLStreamWriter;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.io.Reader;
33  import java.io.StringReader;
34  import java.io.StringWriter;
35  import java.io.Writer;
36  import java.net.URL;
37  
38  /***
39   * @version $Revision: 1.3 $
40   */
41  public abstract class TestSupport extends TestCase {
42      protected SoapService service;
43      private XmlOptions options = new XmlOptions();
44      protected XMLInputFactory inputFactory;
45      protected boolean repairing = false;
46  
47      protected void assertResponse(String requestMessage, String intermediary, String responseMessage) throws Exception {
48  
49          // lets use an intermediary
50          SoapService previousSesrvice = service;
51          service = new SoapService();
52          service.setIntermediary(true);
53          service.getRoles().add("http://example.org/ts-tests/B");
54  
55          String intermediateText = assertResponse(requestMessage, intermediary);
56          Reader in = new StringReader(intermediateText);
57  
58          // restore the endpoint context
59          service = previousSesrvice;
60          if (responseMessage != null) {
61              assertResponse(in, responseMessage);
62          }
63      }
64  
65      protected String assertResponse(String requestMessage, String responseMessage) throws Exception {
66          return assertResponse(createReader(requestMessage), responseMessage);
67      }
68  
69      protected String assertResponse(Reader in, String responseMessage) throws Exception {
70          StringWriter out = new StringWriter();
71          service.invoke(in, out);
72  
73          String responseText = out.toString();
74  
75          XmlObject expected = XmlObject.Factory.parse(getClass().getResource(responseMessage), options);
76          XmlObject actual = prettyPrintXml(responseText);
77  
78          System.out.println();
79          System.out.println("Pretty printed response:-");
80          System.out.println(actual);
81          System.out.println();
82          assertXml(expected, actual);
83          return responseText;
84      }
85  
86      protected XMLStreamWriter createStreamWriter(Writer writer) throws XMLStreamException {
87          XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
88          if (repairing) {
89              outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE);
90          }
91          XMLStreamWriter out = outputFactory.createXMLStreamWriter(writer);
92          return out;
93      }
94  
95      private void assertXml(XmlObject expected, XmlObject actual) {
96          int value = expected.compareValue(actual);
97          System.out.println("Value: " + value);
98  
99          assertEquals("Response does not match expected results", expected.toString(), actual.toString());
100     }
101 
102     protected void setUp() throws Exception {
103         super.setUp();
104         options.setLoadStripWhitespace();
105         options.setLoadStripComments();
106         options.setLoadTrimTextBuffer();
107         options.setSavePrettyPrint();
108         service = new SoapService();
109     }
110 
111     protected XMLStreamReader createXMLStreamReader(URL resource) throws XMLStreamException, IOException {
112         assertTrue("Found resource", resource != null);
113 
114         XMLInputFactory inputFactory = getInputFactory();
115         XMLStreamReader in = inputFactory.createXMLStreamReader(resource.openStream());
116         return in;
117     }
118 
119     protected XMLInputFactory getInputFactory() {
120         if (inputFactory == null) {
121             inputFactory = XMLInputFactory.newInstance();
122             inputFactory.setProperty("javax.xml.stream.supportDTD", Boolean.FALSE);
123         }
124         return inputFactory;
125     }
126 
127     protected XMLStreamReader createXMLStreamReader(String name) throws XMLStreamException, IOException {
128         return createXMLStreamReader(getClass().getResource(name));
129     }
130 
131     /***
132      * Creates a reader for a resource on the classpath in this classes packaget
133      */
134     protected Reader createReader(String uri) {
135         InputStream in = getClass().getResourceAsStream(uri);
136         assertTrue("No resource found on the classpath in the package of class: " + getClass().getName()
137                 + " for uri: " + uri, in != null);
138         return new InputStreamReader(in);
139     }
140 
141     protected XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException {
142         return XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
143     }
144 
145     protected XmlObject prettyPrintXml(String xml) throws Exception {
146         if (xml.trim().length() == 0) {
147             fail("Empty string cannot be parsed!");
148         }
149         try {
150             return XmlObject.Factory.parse(new StringReader(xml), options);
151         }
152         catch (Exception e) {
153             System.out.println("Failed to parse XML due to: " + e);
154             System.out.println("Raw XML:");
155             System.out.println();
156             System.out.println(xml);
157             System.out.println();
158             e.printStackTrace();
159             throw e;
160         }
161     }
162 }