Source code: docStyle/NWBankTest.java
1 /*
2 * The Apache Software License, Version 1.1
3 *
4 *
5 * Copyright (c) 2002 The Apache Software Foundation. All rights
6 * reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by the
23 * Apache Software Foundation (http://www.apache.org/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
26 *
27 * 4. The names "WSIF" and "Apache Software Foundation" must
28 * not be used to endorse or promote products derived from this
29 * software without prior written permission. For written
30 * permission, please contact apache@apache.org.
31 *
32 * 5. Products derived from this software may not be called "Apache",
33 * nor may "Apache" appear in their name, without prior written
34 * permission of the Apache Software Foundation.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the Apache Software Foundation and was
52 * originally based on software copyright (c) 2001, 2002, International
53 * Business Machines, Inc., http://www.apache.org. For more
54 * information on the Apache Software Foundation, please see
55 * <http://www.apache.org/>.
56 */
57
58 package docStyle;
59
60 import http.net.pointwsp.www.ws.finance.ArrayOfCurrency;
61 import http.net.pointwsp.www.ws.finance.Currency;
62 import http.net.pointwsp.www.ws.finance.Currencyrates;
63 import java.io.StringReader;
64 import java.io.StringWriter;
65
66 import junit.framework.Test;
67 import junit.framework.TestCase;
68 import junit.framework.TestSuite;
69
70 import org.apache.wsif.WSIFConstants;
71 import org.apache.wsif.WSIFMessage;
72 import org.apache.wsif.WSIFOperation;
73 import org.apache.wsif.WSIFPort;
74 import org.apache.wsif.WSIFService;
75 import org.apache.wsif.WSIFServiceFactory;
76 import org.apache.xerces.parsers.DOMParser;
77 import org.apache.xml.serialize.OutputFormat;
78 import org.apache.xml.serialize.XMLSerializer;
79 import org.w3c.dom.Element;
80 import org.xml.sax.InputSource;
81 import util.TestUtilities;
82
83 import docStyle.wsifservice.PwspNoCentrbankCurRatesSoap;
84
85 /**
86 * Junit test to test out the AXIS provider docstyle support.
87 * Uses the norwegian central bank daily rates service
88 * (this has no input parts and returns complex types)
89 */
90 public class NWBankTest extends TestCase {
91 String wsdlLocation =
92 TestUtilities.getWsdlPath("java\\test\\docStyle\\wsifservice")
93 + "nwBank.wsdl";
94 // + "nwBankLocal.wsdl";
95
96 public NWBankTest(String name) {
97 super(name);
98 }
99
100 public static void main(String[] args) {
101 TestUtilities.startListeners(
102 TestUtilities.ADDRESSBOOK_LISTENER
103 | TestUtilities.ASYNC_LISTENER
104 | TestUtilities.NATIVEJMS_LISTENER);
105 junit.textui.TestRunner.run(suite());
106 TestUtilities.stopListeners();
107 }
108
109 public static Test suite() {
110 return new TestSuite(NWBankTest.class);
111 }
112
113 public void setUp() {
114 TestUtilities.setUpExtensionsAndProviders();
115 }
116
117 public void testDynamicAxis() {
118 doitDyn("pwspNoCentrbankCurRatesSoap", "axis");
119 }
120
121 public void testStubsAxis() {
122 doitStub("pwspNoCentrbankCurRatesSoap", "axis");
123 }
124
125 public void testMessagingAxis() {
126 doitMessaging("pwspNoCentrbankCurRatesSoap", "axis");
127 }
128
129 public void testDynJMS() {
130 doitDyn("pwspNoCentrbankCurRatesSoapJMS", "axis");
131 }
132
133 public void testStubsAxisJMS() {
134 doitStub("pwspNoCentrbankCurRatesSoapJMS", "axis");
135 }
136
137 private void doitDyn(String portName, String protocol) {
138 if (portName.toUpperCase().indexOf("JMS") != -1
139 && !TestUtilities.areWeTesting("jms"))
140 return;
141
142 TestUtilities.setProviderForProtocol(protocol);
143
144 try {
145 WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
146 WSIFService service =
147 factory.getService(
148 wsdlLocation,
149 null,
150 null,
151 "http/www.pointwsp.net/ws/finance",
152 "pwspNoCentrbankCurRatesSoap");
153
154 service.mapType(
155 new javax.xml.namespace.QName("http/www.pointwsp.net/ws/finance", "currencyrates"),
156 Currencyrates.class );
157
158 service.mapType(
159 new javax.xml.namespace.QName("http/www.pointwsp.net/ws/finance", "ArrayOfCurrency"),
160 ArrayOfCurrency.class);
161
162 service.mapType(
163 new javax.xml.namespace.QName("http/www.pointwsp.net/ws/finance", "currency"),
164 Currency.class);
165
166 // force to use a 'wrapped' type operation
167 WSIFMessage context = service.getContext();
168 context.setObjectPart(WSIFConstants.CONTEXT_OPERATION_STYLE, WSIFConstants.CONTEXT_OPERATION_STYLE_WRAPPED);
169 service.setContext(context);
170
171 WSIFPort port = service.getPort(portName);
172
173 WSIFOperation operation = port.createOperation("GetRatesXML");
174
175 WSIFMessage inMsg = operation.createInputMessage();
176 WSIFMessage outMsg = operation.createOutputMessage();
177 WSIFMessage faultMsg = operation.createFaultMessage();
178
179 boolean ok =
180 operation.executeRequestResponseOperation(
181 inMsg,
182 outMsg,
183 faultMsg);
184
185 assertTrue("operation returned false!!", ok);
186 Object o = outMsg.getObjectPart("GetRatesXMLResult");
187 assertTrue("unexpected result: " + o, o instanceof Currencyrates);
188
189 // Currencyrates cr = (Currencyrates) o;
190 // System.out.println("source=" + cr.getSource());
191
192 } catch (Exception ex) {
193 ex.printStackTrace();
194 assertTrue(
195 "AddressBookTest("
196 + portName
197 + ") caught exception "
198 + ex.getLocalizedMessage(),
199 false);
200 }
201 }
202
203 private void doitStub(String portName, String protocol) {
204 if (portName.toUpperCase().indexOf("JMS") != -1
205 && !TestUtilities.areWeTesting("jms"))
206 return;
207
208 TestUtilities.setProviderForProtocol(protocol);
209
210 try {
211 WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
212 WSIFService service =
213 factory.getService(
214 wsdlLocation,
215 null,
216 null,
217 "http/www.pointwsp.net/ws/finance",
218 "pwspNoCentrbankCurRatesSoap");
219
220 // force to use a 'wrapped' type operation
221 WSIFMessage context = service.getContext();
222 context.setObjectPart(WSIFConstants.CONTEXT_OPERATION_STYLE, WSIFConstants.CONTEXT_OPERATION_STYLE_WRAPPED);
223 service.setContext(context);
224
225 PwspNoCentrbankCurRatesSoap stub = (PwspNoCentrbankCurRatesSoap) service.getStub(portName, PwspNoCentrbankCurRatesSoap.class);
226
227 Object o = stub.GetRatesXML();
228 assertTrue("unexpected result: " + o, o instanceof Currencyrates);
229 // Currencyrates cr = (Currencyrates) o;
230 // System.out.println("source=" + cr.getSource());
231
232 } catch (Exception ex) {
233 ex.printStackTrace();
234 assertTrue(
235 "AddressBookTest("
236 + portName
237 + ") caught exception "
238 + ex.getLocalizedMessage(),
239 false);
240 }
241 }
242
243 private void doitMessaging(String portName, String protocol) {
244 if (portName.toUpperCase().indexOf("JMS") != -1
245 && !TestUtilities.areWeTesting("jms"))
246 return;
247
248 TestUtilities.setProviderForProtocol(protocol);
249
250 try {
251 WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
252 WSIFService service =
253 factory.getService(
254 wsdlLocation,
255 null,
256 null,
257 "http/www.pointwsp.net/ws/finance",
258 "pwspNoCentrbankCurRatesSoap");
259
260 service.mapType(
261 new javax.xml.namespace.QName("http/www.pointwsp.net/ws/finance", "currencyrates"),
262 Currencyrates.class );
263
264 service.mapType(
265 new javax.xml.namespace.QName("http/www.pointwsp.net/ws/finance", "ArrayOfCurrency"),
266 ArrayOfCurrency.class);
267
268 service.mapType(
269 new javax.xml.namespace.QName("http/www.pointwsp.net/ws/finance", "currency"),
270 Currency.class);
271
272 WSIFPort port = service.getPort(portName);
273
274 WSIFOperation operation = port.createOperation("GetRatesXML");
275
276 WSIFMessage inMsg = operation.createInputMessage();
277 WSIFMessage outMsg = operation.createOutputMessage();
278 WSIFMessage faultMsg = operation.createFaultMessage();
279
280 String inputDocument =
281 "<GetRatesXML xmlns=\"http/www.pointwsp.net/ws/finance\"/>";
282
283 DOMParser parser = new DOMParser();
284 String xmlString = "<?xml version=\"1.0\"?>\n" + inputDocument;
285 parser.parse(new InputSource(new StringReader(xmlString)));
286 Element element = parser.getDocument().getDocumentElement();
287 //printElement(element);
288
289 inMsg.setObjectPart("parameters", element);
290
291 boolean ok =
292 operation.executeRequestResponseOperation(
293 inMsg,
294 outMsg,
295 faultMsg);
296
297 assertTrue("operation returned false!!", ok);
298
299 Element responseElement =
300 (Element) outMsg.getObjectPart("parameters");
301 //printElement(responseElement);
302 assertTrue("return element is null!!", responseElement != null);
303
304
305 } catch (Exception ex) {
306 ex.printStackTrace();
307 assertTrue(
308 "AddressBookTest("
309 + portName
310 + ") caught exception "
311 + ex.getLocalizedMessage(),
312 false);
313 }
314 }
315
316 private void printElement(Element e) throws Exception {
317 OutputFormat of = new OutputFormat(e.getOwnerDocument(), "UTF-8", true);
318 XMLSerializer xmls = new XMLSerializer(of);
319 StringWriter sw = new StringWriter();
320 xmls.setOutputCharStream(sw);
321 xmls.serialize(e);
322 System.err.println("element=" + sw.toString());
323 }
324 }