KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > inout > InoutTest


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 inout;
59
60 import java.text.DateFormat JavaDoc;
61 import java.util.Date JavaDoc;
62
63 import javax.xml.namespace.QName JavaDoc;
64
65 import junit.framework.Test;
66 import junit.framework.TestCase;
67 import junit.framework.TestSuite;
68 import junit.textui.TestRunner;
69 import org.apache.wsif.WSIFException;
70 import org.apache.wsif.WSIFService;
71 import org.apache.wsif.WSIFServiceFactory;
72 import org.apache.wsif.base.WSIFServiceImpl;
73 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
74 import org.apache.wsif.providers.soap.apachesoap.WSIFDynamicProvider_ApacheSOAP;
75 import org.apache.wsif.util.WSIFPluggableProviders;
76
77 import addressbook.wsiftypes.Address;
78 import addressbook.wsiftypes.Phone;
79 import inout.wsifservice.Inout;
80 import inout.wsiftypes.Mutablestring;
81 import util.AddressUtility;
82 import util.TestUtilities;
83
84 /**
85  * Junit test to test out the Inout test
86  * @author Mark Whitlock
87  */

88 public class InoutTest extends TestCase {
89     String JavaDoc wsdlLocation =
90         TestUtilities.getWsdlPath("java\\test\\inout\\wsifservice") + "Inout.wsdl";
91     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
92     
93     static String JavaDoc name1 = "Purdue Boilermaker";
94     static Address addr1 =
95         new Address(
96             1,
97             "University Drive",
98             "West Lafayette",
99             "IN",
100             47907,
101             new Phone(765, "494", "4900"));
102
103     static String JavaDoc firstName2 = "Someone";
104     static String JavaDoc lastName2 = "Else";
105     static String JavaDoc name2 = "Someone Else";
106     static Address addr2 =
107         new Address(
108             0,
109             "Somewhere Else",
110             "No Where",
111             "NO",
112             71983,
113             new Phone(600, "391", "5682"));
114
115     static Address nulladdr = new Address(0, "", "", "", 0, new Phone(0, "", ""));
116
117     private final static String JavaDoc ADDRESS = "address";
118     private final static String JavaDoc NULLNAME = "nullname";
119     private final static String JavaDoc ADD = "add";
120     private final static String JavaDoc DATE = "date";
121     private final static String JavaDoc SUPC = "supc";
122     private final static String JavaDoc GENC = "genericc";
123     private final static String JavaDoc WHOAMI = "whoami";
124     private final static String JavaDoc INOUT = "inout";
125
126     private final static String JavaDoc RPC = "rpc";
127     private final static String JavaDoc DOC = "doc";
128
129     private final static String JavaDoc SOAP = "soap";
130     private final static String JavaDoc AXIS = "axis";
131     private final static String JavaDoc JAVA = "java";
132     private final static String JavaDoc NJMS = "nativeJMS";
133
134     public InoutTest(String JavaDoc name) {
135         super(name);
136     }
137
138     public static void main(String JavaDoc[] args) {
139        TestUtilities.startListeners();
140        junit.textui.TestRunner.run (suite());
141        TestUtilities.stopListeners();
142     }
143
144     public static Test suite() {
145         return new TestSuite(InoutTest.class);
146     }
147
148     public void setUp() {
149         TestUtilities.setUpExtensionsAndProviders();
150     }
151
152     public void testAxis() {
153         doit(server+"Port", AXIS, RPC, ADDRESS);
154     }
155     public void testSoap() {
156         doit(server+"Port", SOAP, RPC, ADDRESS);
157     }
158     public void testJava() {
159         doit("JavaPort", JAVA, RPC, ADDRESS);
160     }
161     public void testSoapJms() {
162         doit("SOAPJMSPort", SOAP, RPC, ADDRESS);
163     }
164     public void testAxisJms() {
165         doit("SOAPJMSPort", AXIS, RPC, ADDRESS);
166     }
167     public void testNativeJms() {
168         doit("NativeJMSPort" ,NJMS, RPC, ADDRESS);
169     }
170     //public void testAxisDoc () { doit("SOAPDocPort" ,AXIS,DOC, ADDRESS ); } FAILS
171
//public void testSoapDoc () { doit("SOAPDocPort" ,SOAP,DOC, ADDRESS ); } FAILS
172
//public void testSoapJmsDoc () { doit("SOAPJMSDocPort",SOAP,DOC, ADDRESS ); } FAILS
173
//public void testAxisJmsDoc () { doit("SOAPJMSDocPort",AXIS,DOC, ADDRESS ); } FAILS
174
//public void testNativeJmsDoc () { doit("NativeJMSPort" ,NJMS,DOC, ADDRESS ); } FAILS
175

176     //public void testNNAxis () { doit(server+"Port" ,AXIS,RPC, NULLNAME); } FAILS
177
//public void testNNSoap () { doit(server+"Port" ,SOAP,RPC, NULLNAME); } FAILS
178
public void testNNJava() {
179         doit("JavaPort", JAVA, RPC, NULLNAME);
180     }
181     //public void testNNSoapJms () { doit("SOAPJMSPort" ,SOAP,RPC, NULLNAME); } FAILS
182
//public void testNNAxisJms () { doit("SOAPJMSPort" ,AXIS,RPC, NULLNAME); } FAILS
183
//public void testNNNativeJms () { doit("NativeJMSPort" ,NJMS,RPC, NULLNAME); }
184

185     //public void testAddAxis () { doit(server+"Port" ,AXIS,RPC, ADD ); } FAILS
186
public void testAddSoap() {
187         doit(server+"Port", SOAP, RPC, ADD);
188     }
189     public void testAddJava() {
190         doit("JavaPort", JAVA, RPC, ADD);
191     }
192     public void testAddSoapJms() {
193         doit("SOAPJMSPort", SOAP, RPC, ADD);
194     }
195     //public void testAddAxisJms () { doit("SOAPJMSPort" ,AXIS,RPC, ADD ); } FAILS
196
public void testAddNativeJms() {
197         doit("NativeJMSPort" , NJMS, RPC, ADD );
198     }
199
200     //public void testDateAxis () { doit(server+"Port" ,AXIS,RPC, DATE ); } FAILS
201
public void testDateSoap() {
202         doit(server+"Port", SOAP, RPC, DATE);
203     }
204     public void testDateJava() {
205         doit("JavaPort", JAVA, RPC, DATE);
206     }
207     public void testDateSoapJms() {
208         doit("SOAPJMSPort", SOAP, RPC, DATE);
209     }
210     //public void testDateAxisJms () { doit("SOAPJMSPort" ,AXIS,RPC, DATE ); } FAILS
211
public void testDateNativeJms() {
212         doit("NativeJMSPort" , NJMS, RPC, DATE );
213     }
214
215     public void testSupCAxis() {
216         doit(server+"Port", AXIS, RPC, SUPC);
217     }
218     public void testSupCSoap() {
219         doit(server+"Port", SOAP, RPC, SUPC);
220     }
221     public void testSupCJava() {
222         doit("JavaPort", JAVA, RPC, SUPC);
223     }
224     public void testSupCSoapJms() {
225         doit("SOAPJMSPort", SOAP, RPC, SUPC);
226     }
227     public void testSupCAxisJms() {
228         doit("SOAPJMSPort", AXIS, RPC, SUPC);
229     }
230     public void testSupCNativeJms() {
231         doit("NativeJMSPort" , NJMS, RPC, SUPC );
232     }
233     public void testGenCAxis() {
234         doit(server+"Port", AXIS, RPC, GENC);
235     }
236     public void testGenCSoap() {
237         doit(server+"Port", SOAP, RPC, GENC);
238     }
239     public void testGenCJava() {
240         doit("JavaPort", JAVA, RPC, GENC);
241     }
242     public void testGenCSoapJms() {
243         doit("SOAPJMSPort", SOAP, RPC, GENC);
244     }
245     public void testGenCAxisJms() {
246         doit("SOAPJMSPort", AXIS, RPC, GENC);
247     }
248     public void testGenCNativeJms() {
249         doit("NativeJMSPort" , NJMS, RPC, GENC );
250      }
251
252     //public void testWhoAxis () { doit(server+"Port" ,AXIS,RPC, WHOAMI ); } FAILS
253
public void testWhoSoap() {
254         doit(server+"Port", SOAP, RPC, WHOAMI);
255     }
256     public void testWhoJava() {
257         doit("JavaPort", JAVA, RPC, WHOAMI);
258     }
259     public void testWhoSoapJms() {
260         doit("SOAPJMSPort", SOAP, RPC, WHOAMI);
261     }
262     //public void testWhoAxisJms () { doit("SOAPJMSPort" ,AXIS,RPC, WHOAMI ); } FAILS
263
public void testWhoNativeJms() {
264         doit("NativeJMSPort" , NJMS, RPC, WHOAMI );
265      }
266
267     // The inout test will never work for any form of soap since soap does not
268
// support inout or multiple output parameters.
269
public void testInoJava() {
270         doit("JavaPort", JAVA, RPC, INOUT);
271     }
272
273     private void doit(String JavaDoc portName, String JavaDoc protocol, String JavaDoc style, String JavaDoc cmd) {
274         if (portName.toUpperCase().indexOf("JMS") != -1
275             && !TestUtilities.areWeTesting("jms"))
276             return;
277
278         if (protocol.equals(SOAP)) {
279             WSIFDynamicProvider_ApacheSOAP provider = new WSIFDynamicProvider_ApacheSOAP();
280
281             if (style.equals(DOC))
282                 provider.setPartSerializerName("services.inout.InoutLiteralSerializer");
283
284             WSIFPluggableProviders.overrideDefaultProvider(
285                 "http://schemas.xmlsoap.org/wsdl/soap/",
286                 provider);
287         }
288         if (protocol.equals(AXIS)) {
289             WSIFDynamicProvider_ApacheAxis provider = new WSIFDynamicProvider_ApacheAxis();
290
291             WSIFPluggableProviders.overrideDefaultProvider(
292                 "http://schemas.xmlsoap.org/wsdl/soap/",
293                 provider);
294         }
295
296         try {
297             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
298             WSIFService service =
299                 factory.getService(
300                     wsdlLocation,
301                     null,
302                     null,
303                     "http://wsifservice.inout/",
304                     "Inout");
305
306             service.mapType(
307                 new QName JavaDoc("http://wsiftypes.inout/", "arrayofint"),
308                 Class.forName("[I"));
309
310             Inout stub = (Inout) service.getStub(portName, Inout.class);
311
312             if (cmd.equals(ADDRESS))
313                 address(stub);
314             else if (cmd.equals(NULLNAME))
315                 nullname(stub);
316             else if (cmd.equals(ADD))
317                 add(stub);
318             else if (cmd.equals(DATE))
319                 date(stub);
320             else if (cmd.equals(SUPC))
321                 supc(service, portName);
322             else if (cmd.equals(GENC))
323                 genericc(service, portName);
324             else if (cmd.equals(WHOAMI))
325                 whoami(stub);
326             else if (cmd.equals(INOUT))
327                 inout(stub);
328             else
329                 assertTrue(false);
330         } catch (Exception JavaDoc e) {
331             System.err.println("InoutTest(" + portName + ") caught exception " + e);
332             e.printStackTrace();
333             assertTrue(false);
334         } finally {
335             if (protocol.equals(SOAP) || protocol.equals(AXIS)) {
336                 WSIFPluggableProviders.overrideDefaultProvider(
337                     "http://schemas.xmlsoap.org/wsdl/soap/",
338                     null);
339             }
340         }
341
342     }
343
344     private void address(Inout stub) throws Exception JavaDoc {
345         stub.addEntry(name1.toString(), addr1);
346
347         Mutablestring msName1 = new Mutablestring(name1);
348         Address resp1 = stub.getAddressFromName(msName1);
349         assertTrue(new AddressUtility(resp1).equals(addr1));
350
351         Mutablestring msName = new Mutablestring("Pur*");
352         resp1 = stub.getAddressFromName(msName);
353         assertTrue(new AddressUtility(resp1).equals(addr1));
354     }
355
356     private void nullname(Inout stub) throws Exception JavaDoc {
357         stub.addEntry(null, null);
358
359         Address resp1 = stub.getAddressFromName((String JavaDoc) null);
360         assertTrue(resp1 == null);
361
362         Address resp2 = stub.getAddressFromName((Mutablestring) null);
363         assertTrue(resp2 == null);
364
365         // This next bit of code works with soap2.2 but not Axis. I've
366
// commented it out because getAddressFromName((String)null) doesn't
367
// work with either so I can't run this test with soap or axis.
368
//Address nullAddr = new Address(0,"","","",0,new Phone(0,null,null));
369
//Address resp = stub.getAddressFromName (new Mutablestring(null));
370
//assertTrue(resp==null || TestUtilities.AddressEquals(resp,nullAddr));
371
}
372
373     private void add(Inout stub) throws Exception JavaDoc {
374         int[] nums = new int[] { 1, 2, 3, 4, 5 };
375         int total = stub.addNumbers(nums);
376
377         String JavaDoc warmFuzzy = new String JavaDoc();
378         int expected = 0;
379         for (int i = 0; i < nums.length; i++) {
380             expected += nums[i];
381             if (i != 0)
382                 warmFuzzy += "+ ";
383             warmFuzzy += nums[i] + " ";
384         }
385         warmFuzzy += "= " + total + " (expected " + expected + ")";
386         System.out.println(warmFuzzy);
387         assertTrue(total == expected);
388     }
389
390     private void date(Inout stub) throws Exception JavaDoc {
391         Date JavaDoc first = new Date JavaDoc();
392         Thread.sleep(1000);
393         Date JavaDoc remote = stub.getDate();
394         Thread.sleep(1000);
395         Date JavaDoc later = new Date JavaDoc();
396
397         DateFormat JavaDoc df = DateFormat.getDateTimeInstance();
398         System.out.println(
399             "Remote date is "
400                 + df.format(remote)
401                 + " First date is "
402                 + df.format(first)
403                 + " Later date is "
404                 + df.format(later));
405
406         assertTrue(first.before(remote) || first.equals(remote));
407         assertTrue(later.after(remote) || later.equals(remote));
408     }
409
410     private interface SubInout extends Inout {
411         public void dubiousMethod() throws WSIFException;
412     }
413
414     private void supc(WSIFService service, String JavaDoc portName) throws Exception JavaDoc {
415         SubInout stub = (SubInout) service.getStub(portName, SubInout.class);
416
417         stub.addEntry(name1, addr1);
418         Mutablestring msName1 = new Mutablestring(name1);
419         Address resp1 = stub.getAddressFromName(msName1);
420         assertTrue(new AddressUtility(addr1).equals(resp1));
421
422         boolean caught = false;
423         try {
424             stub.dubiousMethod();
425         } catch (WSIFException e) {
426             caught = true;
427         }
428         assertTrue(caught);
429
430         stub.addEntry(name2, new SubAddress(addr2));
431         Mutablestring msName2 = new Mutablestring(name2);
432         Address resp2 = stub.getAddressFromName(msName2);
433         assertTrue(new AddressUtility(addr2).equals(resp2));
434     }
435
436     private interface GenericInout {
437         public void addEntry(String JavaDoc wholeName, Object JavaDoc address)
438             throws java.rmi.RemoteException JavaDoc;
439         public Address getAddressFromName(Mutablestring name)
440             throws java.rmi.RemoteException JavaDoc;
441     }
442
443     private void genericc(WSIFService service, String JavaDoc portName)
444         throws Exception JavaDoc {
445         GenericInout stub =
446             (GenericInout) service.getStub(portName, GenericInout.class);
447
448         stub.addEntry(name1, addr1);
449         Mutablestring msName1 = new Mutablestring(name1);
450         Address resp1 = stub.getAddressFromName(msName1);
451         assertTrue(new AddressUtility(addr1).equals(resp1));
452     }
453
454     /**
455      * whoami tests out dynamic proxies by overloading is various
456      * ways.
457      */

458     private void whoami(Inout stub) throws Exception JavaDoc {
459         float f = 1;
460         int i = 1;
461         Address a = new Address();
462         SubAddress suba = new SubAddress();
463         String JavaDoc resp;
464
465         resp = stub.whoami("Hello");
466         System.out.println("resp was " + resp + " expected String");
467         assertTrue("String".equals(resp));
468
469         resp = stub.whoami(f);
470         System.out.println("resp was " + resp + " expected float");
471         assertTrue("float".equals(resp));
472
473         resp = stub.whoami(i);
474         System.out.println("resp was " + resp + " expected int");
475         assertTrue("int".equals(resp));
476
477         resp = stub.whoami(a);
478         System.out.println("resp was " + resp + " expected Address");
479         assertTrue("Address".equals(resp));
480
481         resp = stub.whoami(suba);
482         System.out.println("resp was " + resp + " expected Address");
483         assertTrue("Address".equals(resp));
484     }
485
486     private void inout(Inout stub) throws Exception JavaDoc {
487         stub.addEntry(name1.toString(), addr1);
488
489         Mutablestring msName1 = new Mutablestring(name1);
490         Address resp1 = new AddressUtility(nulladdr).copy();
491         boolean success = stub.getAddressFromName(msName1, resp1);
492         assertTrue(success && new AddressUtility(resp1).equals(addr1));
493     }
494
495 }
496
497 class SubAddress extends Address {
498
499    public SubAddress() {
500       super();
501    }
502
503    public SubAddress(Address a) {
504       super(
505          a.getStreetNum(),
506          a.getStreetName(),
507          a.getCity(),
508          a.getState(),
509          a.getZip(),
510          a.getPhoneNumber());
511    }
512
513 }
514
515
Popular Tags