Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: docStyle/ZipCodeAxisTest.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 java.io.StringReader;
61  import java.io.StringWriter;
62  
63  import junit.framework.Test;
64  import junit.framework.TestCase;
65  import junit.framework.TestSuite;
66  
67  import org.apache.wsif.WSIFMessage;
68  import org.apache.wsif.WSIFOperation;
69  import org.apache.wsif.WSIFPort;
70  import org.apache.wsif.WSIFService;
71  import org.apache.wsif.WSIFServiceFactory;
72  import org.apache.xerces.parsers.DOMParser;
73  import org.apache.xml.serialize.OutputFormat;
74  import org.apache.xml.serialize.XMLSerializer;
75  import org.w3c.dom.Element;
76  import org.w3c.dom.Node;
77  import org.w3c.dom.NodeList;
78  import org.xml.sax.InputSource;
79  
80  import docStyle.wsifservice.ZipCodeResolverSoap;
81  import docStyle.zipCodeNW.ShortZipCode;
82  import docStyle.zipCodeNW.ShortZipCodeResponse;
83  
84  import util.TestUtilities;
85  
86  /**
87   * Junit test to test out the AXIS provider docstyle support
88   */
89  public class ZipCodeAxisTest extends TestCase {
90    String wsdlLocation =
91      TestUtilities.getWsdlPath("java\\test\\docStyle\\wsifservice")
92              + "zipCodeResolver.wsdl";
93  //        + "zipCode.wsdl";
94  //            + "zipCodeLocal.wsdl";
95  //            "http://webservices.eraserver.net/zipcoderesolver/zipcoderesolver.asmx?WSDL";
96  
97    public ZipCodeAxisTest(String name) {
98      super(name);
99    }
100 
101     public static void main(String[] args) {
102         TestUtilities.startListeners(
103             TestUtilities.ADDRESSBOOK_LISTENER
104                 | TestUtilities.ASYNC_LISTENER
105                 | TestUtilities.NATIVEJMS_LISTENER);
106         junit.textui.TestRunner.run(suite());
107         TestUtilities.stopListeners();
108     }
109 
110   public static Test suite() {
111     return new TestSuite(ZipCodeAxisTest.class);
112   }
113 
114   public void setUp() {
115     TestUtilities.setUpExtensionsAndProviders();
116   }
117 
118   public void testDynamicAxis() {
119     doitDyn("ZipCodeResolverSoap", "axis");
120   }
121   public void testDynamicAxisWrapped() {
122     doitDynWrapped("ZipCodeResolverSoap", "axis");
123   }
124   public void testStubsAxis() {
125     doitStub("ZipCodeResolverSoap", "axis");
126   }
127   public void testStubsAxisWrapped() {
128     doitStubWrapped("ZipCodeResolverSoap", "axis");
129   }
130   public void testMessagingAxis() {
131     doitMessaging("ZipCodeResolverSoap", "axis");
132   }
133   public void testDyn() {
134     doitStub("ZipCodeResolverSoapJMS", "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://webservices.eraserver.net/",
152           "ZipCodeResolverSoap");
153 
154       WSIFPort port = service.getPort(portName);
155 
156       WSIFOperation operation = port.createOperation("ShortZipCode");
157 
158       WSIFMessage inMsg = operation.createInputMessage();
159       WSIFMessage outMsg = operation.createOutputMessage();
160       WSIFMessage faultMsg = operation.createFaultMessage();
161       inMsg.setObjectPart("accessCode", "9999");
162       inMsg.setObjectPart("address", "607 Trinity");
163       inMsg.setObjectPart("city", "Austin");
164       inMsg.setObjectPart("state", "TX");
165 
166       boolean ok =
167         operation.executeRequestResponseOperation(
168           inMsg,
169           outMsg,
170           faultMsg);
171 
172       assertTrue("operation returned false!!", ok);
173       String s = (String) outMsg.getObjectPart("ShortZipCodeResult");
174       assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
175 
176     } catch (Exception ex) {
177       ex.printStackTrace();
178       assertTrue(
179         "AddressBookTest("
180           + portName
181           + ") caught exception "
182           + ex.getLocalizedMessage(),
183         false);
184     }
185   }
186 
187   private void doitDynWrapped(String portName, String protocol) {
188     if (portName.toUpperCase().indexOf("JMS") != -1
189       && !TestUtilities.areWeTesting("jms"))
190       return;
191 
192     TestUtilities.setProviderForProtocol(protocol);
193 
194     try {
195       WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
196       WSIFService service =
197         factory.getService(
198           wsdlLocation,
199           null,
200           null,
201           "http://webservices.eraserver.net/",
202           "ZipCodeResolverSoap");
203 
204             service.mapType(
205                new javax.xml.namespace.QName(
206                    "http://webservices.eraserver.net/", 
207                    "ShortZipCode"),
208                ShortZipCode.class );
209             service.mapType(
210                new javax.xml.namespace.QName(
211                    "http://webservices.eraserver.net/", 
212                    "ShortZipCodeResponse"),
213                ShortZipCodeResponse.class );
214 
215       WSIFPort port = service.getPort(portName);
216 
217       WSIFOperation operation = port.createOperation("ShortZipCode");
218 
219       WSIFMessage inMsg = operation.createInputMessage();
220       WSIFMessage outMsg = operation.createOutputMessage();
221       WSIFMessage faultMsg = operation.createFaultMessage();
222 
223             ShortZipCode zc = new ShortZipCode();
224             zc.setAccessCode("9999");
225             zc.setAddress("607 Trinity");
226             zc.setCity("Austin");
227             zc.setState("TX");
228 
229       inMsg.setObjectPart("parameters", zc);
230 
231       boolean ok =
232         operation.executeRequestResponseOperation(
233           inMsg,
234           outMsg,
235           faultMsg);
236 
237       assertTrue("operation returned false!!", ok);
238 
239             ShortZipCodeResponse zcResp =
240          (ShortZipCodeResponse) outMsg.getObjectPart("parameters");
241 
242             String s = zcResp.getShortZipCodeResult();
243       assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
244 
245     } catch (Exception ex) {
246       ex.printStackTrace();
247       assertTrue(
248         "AddressBookTest("
249           + portName
250           + ") caught exception "
251           + ex.getLocalizedMessage(),
252         false);
253     }
254   }
255 
256   private void doitStub(String portName, String protocol) {
257     if (portName.toUpperCase().indexOf("JMS") != -1
258       && !TestUtilities.areWeTesting("jms"))
259       return;
260 
261     TestUtilities.setProviderForProtocol(protocol);
262 
263     try {
264       WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
265       WSIFService service =
266         factory.getService(
267           wsdlLocation,
268           null,
269           null,
270           "http://webservices.eraserver.net/",
271           "ZipCodeResolverSoap");
272 
273             ZipCodeResolverSoap stub = (ZipCodeResolverSoap) service.getStub(portName, ZipCodeResolverSoap.class);
274 
275             String zipcode = stub.ShortZipCode( "9999", "607 Trinity", "Austin", "TX" );
276       assertTrue("wrong zipcode: " + zipcode + "!!", "78701".equals(zipcode));
277 
278     } catch (Exception ex) {
279       ex.printStackTrace();
280       assertTrue(
281         "AddressBookTest("
282           + portName
283           + ") caught exception "
284           + ex.getLocalizedMessage(),
285         false);
286     }
287   }
288 
289   private void doitStubWrapped(String portName, String protocol) {
290     if (portName.toUpperCase().indexOf("JMS") != -1
291       && !TestUtilities.areWeTesting("jms"))
292       return;
293 
294     TestUtilities.setProviderForProtocol(protocol);
295 
296     try {
297       WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
298       WSIFService service =
299         factory.getService(
300           wsdlLocation,
301           null,
302           null,
303           "http://webservices.eraserver.net/",
304           "ZipCodeResolverSoap");
305 
306             //TODO: its a bug that these mapTypes are needed with wrapped operation stubs 
307             service.mapType(
308                new javax.xml.namespace.QName(
309                    "http://webservices.eraserver.net/", 
310                    "ShortZipCode"),
311                ShortZipCode.class );
312             service.mapType(
313                new javax.xml.namespace.QName(
314                    "http://webservices.eraserver.net/", 
315                    "ShortZipCodeResponse"),
316                ShortZipCodeResponse.class );
317 
318             ShortZipCode zc = new ShortZipCode();
319             zc.setAccessCode("9999");
320             zc.setAddress("607 Trinity");
321             zc.setCity("Austin");
322             zc.setState("TX");
323 
324             docStyle.zipCodeNW.ZipCodeResolverSoap stub = (docStyle.zipCodeNW.ZipCodeResolverSoap) service.getStub(portName, docStyle.zipCodeNW.ZipCodeResolverSoap.class);
325 
326             ShortZipCodeResponse zcResp = stub.shortZipCode(zc);
327 
328             String s = zcResp.getShortZipCodeResult();
329       assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
330 
331     } catch (Exception ex) {
332       ex.printStackTrace();
333       assertTrue(
334         "AddressBookTest("
335           + portName
336           + ") caught exception "
337           + ex.getLocalizedMessage(),
338         false);
339     }
340   }
341 
342   private void doitMessaging(String portName, String protocol) {
343     if (portName.toUpperCase().indexOf("JMS") != -1
344       && !TestUtilities.areWeTesting("jms"))
345       return;
346 
347     TestUtilities.setProviderForProtocol(protocol);
348 
349     try {
350       WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
351       WSIFService service =
352         factory.getService(
353           wsdlLocation,
354           null,
355           null,
356           "http://webservices.eraserver.net/",
357           "ZipCodeResolverSoap");
358 
359       WSIFPort port = service.getPort(portName);
360       WSIFOperation operation = port.createOperation("ShortZipCode");
361       WSIFMessage inMsg = operation.createInputMessage();
362       WSIFMessage outMsg = operation.createOutputMessage();
363       WSIFMessage faultMsg = operation.createFaultMessage();
364 
365       String inputDocument =
366         "<ShortZipCode xmlns=\"http://webservices.eraserver.net/\">"
367           + "<accessCode>9999</accessCode>"
368           + "<address>607 Trinity</address>"
369           + "<city>Austin</city>"
370           + "<state>TX</state>"
371           + "</ShortZipCode>";
372 
373       DOMParser parser = new DOMParser();
374       String xmlString = "<?xml version=\"1.0\"?>\n" + inputDocument;
375       parser.parse(new InputSource(new StringReader(xmlString)));
376       Element element = parser.getDocument().getDocumentElement();
377       //printElement(element);
378 
379       inMsg.setObjectPart("parameters", element);
380 
381       boolean ok =
382         operation.executeRequestResponseOperation(
383           inMsg,
384           outMsg,
385           faultMsg);
386       assertTrue("operation returned false!!", ok);
387 
388       Element responseElement =
389         (Element) outMsg.getObjectPart("parameters");
390       assertTrue("return element is null!!", responseElement != null);
391       //      printElement(responseElement);
392 
393       NodeList nl = responseElement.getChildNodes();
394       Node n = nl.item(0); // "ShortZipCodeResponse"
395       nl = n.getChildNodes();
396       n = nl.item(0); // "ShortZipCodeResult"
397       String s = n.getNodeValue();
398       if (!"78701".equals(s)) {
399         printElement(responseElement);
400       }
401       assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
402 
403     } catch (Exception ex) {
404       ex.printStackTrace();
405       assertTrue(
406         "AddressBookTest("
407           + portName
408           + ") caught exception "
409           + ex.getLocalizedMessage(),
410         false);
411     }
412   }
413 
414   private void printElement(Element e) throws Exception {
415     OutputFormat of = new OutputFormat(e.getOwnerDocument(), "UTF-8", true);
416     XMLSerializer xmls = new XMLSerializer(of);
417     StringWriter sw = new StringWriter();
418     xmls.setOutputCharStream(sw);
419     xmls.serialize(e);
420     System.err.println("element=" + sw.toString());
421   }
422 }