KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > serialization > SerializationTest


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 serialization;
59
60 import java.io.ByteArrayInputStream JavaDoc;
61 import java.io.ByteArrayOutputStream JavaDoc;
62 import java.io.ObjectInputStream JavaDoc;
63 import java.io.ObjectOutputStream JavaDoc;
64 import java.util.ArrayList JavaDoc;
65 import java.util.List JavaDoc;
66 import java.util.StringTokenizer JavaDoc;
67
68 import javax.wsdl.Definition;
69 import javax.xml.namespace.QName JavaDoc;
70 import junit.framework.Test;
71 import junit.framework.TestCase;
72 import junit.framework.TestSuite;
73
74 import org.apache.wsif.WSIFMessage;
75 import org.apache.wsif.WSIFOperation;
76 import org.apache.wsif.WSIFPort;
77 import org.apache.wsif.WSIFService;
78 import org.apache.wsif.WSIFServiceFactory;
79 import org.apache.wsif.providers.WSIFDynamicTypeMapping;
80 import org.apache.wsif.schema.Parser;
81 import org.apache.wsif.util.WSIFUtils;
82
83 import addressbook.wsiftypes.Address;
84 import addressbook.wsiftypes.Phone;
85
86 import util.TestUtilities;
87
88 /**
89  * Test the serializing and deserializing of WSIF objects
90  * @author Owen Burroughs <owenb@apache.org>
91  */

92 public class SerializationTest extends TestCase {
93     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
94     
95     public SerializationTest(String JavaDoc name) {
96         super(name);
97     }
98
99     public static void main(String JavaDoc[] args) {
100         junit.textui.TestRunner.run(suite());
101     }
102
103     public static Test suite() {
104         return new TestSuite(SerializationTest.class);
105     }
106
107     public void setUp() {
108         TestUtilities.setUpExtensionsAndProviders();
109     }
110
111     public void testWSIFDynamicTypeMapping() {
112         try {
113             String JavaDoc dummyNS = "http://this.is.a.test/";
114
115             // java.lang.String
116
QName JavaDoc qn1 = new QName JavaDoc(dummyNS, "string");
117             doItTM(qn1, java.lang.String JavaDoc.class);
118
119             // int
120
QName JavaDoc qn2 = new QName JavaDoc(dummyNS, "int");
121             doItTM(qn2, int.class);
122
123             // float
124
QName JavaDoc qn3 = new QName JavaDoc(dummyNS, "float");
125             doItTM(qn3, float.class);
126
127             // double
128
QName JavaDoc qn4 = new QName JavaDoc(dummyNS, "double");
129             doItTM(qn4, double.class);
130
131             // long
132
QName JavaDoc qn5 = new QName JavaDoc(dummyNS, "long");
133             doItTM(qn5, long.class);
134
135             // short
136
QName JavaDoc qn6 = new QName JavaDoc(dummyNS, "short");
137             doItTM(qn6, short.class);
138
139             // byte
140
QName JavaDoc qn7 = new QName JavaDoc(dummyNS, "byte");
141             doItTM(qn7, byte.class);
142
143             // void
144
QName JavaDoc qn8 = new QName JavaDoc(dummyNS, "void");
145             doItTM(qn8, void.class);
146
147         } catch (Exception JavaDoc e) {
148             System.out.println(
149                 "An error occured when running testWSIFDynamicTypeMapping "
150                     + e);
151             assertTrue(false);
152         }
153     }
154
155     public void testWSIFPort_Java() {
156         try {
157             // TEST JAVA
158
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
159             WSIFService service =
160                 factory.getService(
161                     TestUtilities.getWsdlPath("java\\test\\shop")
162                         + "ShoppingCartAll.wsdl",
163                     "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
164                     "ShoppingCart_JavaService",
165                     "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
166                     "ShoppingCart_JavaPortType");
167             WSIFPort port = service.getPort();
168             //WSIFOperation op = port.createOperation("addItemOperation");
169
doItPort(port, "WSIFPort_Java");
170         } catch (Exception JavaDoc e) {
171             System.out.println(
172                 "\nAn error occured when running testWSIFPort_Java " + e);
173             assertTrue(false);
174         }
175     }
176
177     public void testWSIFPort_EJB() {
178         if (!TestUtilities.areWeTesting("ejb")) return;
179         try {
180             // TEST EJB
181
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
182             WSIFService service =
183                 factory.getService(
184                     TestUtilities.getWsdlPath("java\\test\\shop")
185                         + "ShoppingCartAll.wsdl",
186                     "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
187                     "ShoppingCart_EJBService",
188                     "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
189                     "ShoppingCart_EJBPortType");
190             WSIFPort port = service.getPort();
191             //WSIFOperation op = port.createOperation("addItemOperation");
192
doItPort(port, "WSIFPort_EJB");
193         } catch (Exception JavaDoc e) {
194             System.out.println(
195                 "\nAn error occured when running testWSIFPort_EJB " + e);
196             assertTrue(false);
197         }
198     }
199
200     public void testWSIFPort_ApacheSOAP() {
201         try {
202             // TEST APACHE SOAP
203
TestUtilities.setProviderForProtocol("soap");
204             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
205             WSIFService service =
206                 factory.getService(
207                     TestUtilities.getWsdlPath(
208                         "java\\test\\addressbook\\wsifservice")
209                         + "AddressBook.wsdl",
210                     null,
211                     null,
212                     "http://wsifservice.addressbook/",
213                     "AddressBook");
214             service.mapType(
215                 new javax.xml.namespace.QName JavaDoc(
216                     "http://wsiftypes.addressbook/",
217                     "address"),
218                 Class.forName("addressbook.wsiftypes.Address"));
219
220             service.mapType(
221                 new javax.xml.namespace.QName JavaDoc(
222                     "http://wsiftypes.addressbook/",
223                     "phone"),
224                 Class.forName("addressbook.wsiftypes.Phone"));
225             WSIFPort port = service.getPort(server+"Port");
226             WSIFOperation op = port.createOperation("addEntry", "AddEntryWholeNameRequest", null);
227             doItPort(port, "WSIFPort_ApacheSOAP");
228             WSIFOperation op2 = port.createOperation("addEntry", "AddEntryWholeNameRequest", null);
229             WSIFMessage inputMessage = op2.createInputMessage();
230             WSIFMessage outputMessage = op2.createOutputMessage();
231             WSIFMessage faultMessage = op2.createFaultMessage();
232
233             // Create a name and address to add to the addressbook
234
String JavaDoc nameToAdd = "Chris P. Bacon";
235             Address addressToAdd =
236                 new Address(
237                     1,
238                     "The Waterfront",
239                     "Some City",
240                     "NY",
241                     47907,
242                     new Phone(765, "494", "4900"));
243
244             // Add the name and address to the input message
245
inputMessage.setObjectPart("name", nameToAdd);
246             inputMessage.setObjectPart("address", addressToAdd);
247
248             // Execute the operation, obtaining a flag to indicate its success
249
op2.executeInputOnlyOperation(inputMessage);
250         } catch (Exception JavaDoc e) {
251             System.out.println(
252                 "\nAn error occured when running testWSIFPort_ApacheSOAP " + e);
253             assertTrue(false);
254         }
255     }
256
257     public void testWSIFPort_ApacheAxis() {
258         try {
259             // TEST APACHE AXIS
260
TestUtilities.setProviderForProtocol("axis");
261             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
262             WSIFService service =
263                 factory.getService(
264                     TestUtilities.getWsdlPath(
265                         "java\\test\\addressbook\\wsifservice")
266                         + "AddressBook.wsdl",
267                     null,
268                     null,
269                     "http://wsifservice.addressbook/",
270                     "AddressBook");
271             WSIFPort port = service.getPort(server+"Port");
272             WSIFOperation op = port.createOperation("addEntry", "AddEntryWholeNameRequest", null);
273             doItPort(port, "WSIFPort_ApacheAxis");
274         } catch (Exception JavaDoc e) {
275             System.out.println(
276                 "\nAn error occured when running testWSIFPort_ApacheAxis " + e);
277             assertTrue(false);
278         }
279     }
280
281     public void testWSIFPort_JMS() {
282         if (!TestUtilities.areWeTesting("jms")) return;
283         try {
284             // TEST APACHE AXIS
285
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
286             WSIFService service =
287                 factory.getService(
288                     TestUtilities.getWsdlPath(
289                         "java\\test\\addressbook\\wsifservice")
290                         + "AddressBook.wsdl",
291                     null,
292                     null,
293                     "http://wsifservice.addressbook/",
294                     "AddressBook");
295             WSIFPort port = service.getPort("NativeJmsPort");
296             WSIFOperation op = port.createOperation("addEntry", "AddEntryWholeNameRequest", null);
297             doItPort(port, "WSIFPort_JMS");
298         } catch (Exception JavaDoc e) {
299             System.out.println(
300                 "\nAn error occured when running testWSIFPort_JMS " + e);
301             assertTrue(false);
302         }
303     }
304
305     public void testWSIFMessage() {
306         try {
307             // TEST JAVA
308
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
309             WSIFService service =
310                 factory.getService(
311                     TestUtilities.getWsdlPath("java\\test\\shop")
312                         + "ShoppingCartAll.wsdl",
313                     "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
314                     "ShoppingCart_JavaService",
315                     "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
316                     "ShoppingCart_JavaPortType");
317             WSIFPort port = service.getPort();
318             WSIFOperation op = port.createOperation("addItemOperation");
319             WSIFMessage message = op.createInputMessage();
320             message.setObjectPart("testPart1", "test1");
321 // message.setObjectPart("testPart2", new StringTokenizer("test2"));
322
doItMessage(message);
323         } catch (Exception JavaDoc e) {
324             System.out.println(
325                 "\nAn error occured when running testWSIFPort_Java " + e);
326             assertTrue(false);
327         }
328     }
329
330     public void testSchemaList() {
331         try {
332             List JavaDoc list = new ArrayList JavaDoc();
333             String JavaDoc wsdl = TestUtilities.getWsdlPath("java\\test\\schema") + "SchemaTest1.wsdl";
334             Definition def = WSIFUtils.readWSDL(null, wsdl);
335             Parser.getAllSchemaTypes(def, list, null);
336             doItSchemaList(list);
337         } catch (Exception JavaDoc e) {
338             System.out.println(
339                 "\nAn error occured when running testWSIFPort_Java " + e);
340             assertTrue(false);
341         }
342     }
343
344     private WSIFPort doItPort(WSIFPort port, String JavaDoc portType) {
345
346         try {
347             WSIFPort p2 = (WSIFPort) writeItReadIt(port);
348             assertNotNull(p2);
349             System.out.println("\n" + portType + " was serialized and deserialized without problem");
350             return p2;
351         } catch (Exception JavaDoc e) {
352             System.out.println("\nError testing port: " + portType + " :- " + e);
353             System.out.println("The full stack trace is:");
354             e.printStackTrace();
355             assertTrue(false);
356             return null;
357         }
358     }
359
360     private void doItTM(QName JavaDoc qn, Class JavaDoc cls) {
361         WSIFDynamicTypeMapping tm = new WSIFDynamicTypeMapping(qn, cls);
362
363         try {
364             WSIFDynamicTypeMapping tma =
365                 (WSIFDynamicTypeMapping) writeItReadIt(tm);
366             assertNotNull(tma);
367             assertEquals(
368                 tm.getJavaType().getName(),
369                 tma.getJavaType().getName());
370             assertEquals(tm.getXmlType(), tma.getXmlType());
371         } catch (Exception JavaDoc e) {
372             System.out.println(
373                 "Error testing type mapping with for QName: "
374                     + qn
375                     + "and Java type: "
376                     + cls
377                     + " :- "
378                     + e);
379             assertTrue(false);
380         }
381     }
382
383     private void doItMessage(WSIFMessage message) {
384         try {
385             WSIFMessage m2 = (WSIFMessage) writeItReadIt(message);
386             assertNotNull(m2);
387             System.out.println("\n WSIFMessage was serialized and deserialized without problem");
388         } catch (Exception JavaDoc e) {
389             System.out.println("\nError testing message:- " + e);
390             System.out.println("The full stack trace is:");
391             e.printStackTrace();
392             assertTrue(false);
393         }
394     }
395
396     private void doItSchemaList(List JavaDoc list) {
397         try {
398             List JavaDoc l2 = (List JavaDoc) writeItReadIt(list);
399             assertNotNull(l2);
400             System.out.println("\n Schema List was serialized and deserialized without problem");
401         } catch (Exception JavaDoc e) {
402             System.out.println("\nError testing schema list:- " + e);
403             System.out.println("The full stack trace is:");
404             e.printStackTrace();
405             assertTrue(false);
406         }
407     }
408
409     private Object JavaDoc writeItReadIt(Object JavaDoc o) throws Exception JavaDoc {
410         ByteArrayOutputStream JavaDoc b = new ByteArrayOutputStream JavaDoc();
411         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(b);
412         out.writeObject(o);
413         out.flush();
414         out.close();
415         byte[] data = b.toByteArray();
416         ObjectInputStream JavaDoc in =
417             new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(data));
418         Object JavaDoc o2 = in.readObject();
419         in.close();
420         return o2;
421     }
422 }
423
Popular Tags