001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package serialization;
059:
060: import java.io.ByteArrayInputStream;
061: import java.io.ByteArrayOutputStream;
062: import java.io.ObjectInputStream;
063: import java.io.ObjectOutputStream;
064: import java.util.ArrayList;
065: import java.util.List;
066: import java.util.StringTokenizer;
067:
068: import javax.wsdl.Definition;
069: import javax.xml.namespace.QName;
070: import junit.framework.Test;
071: import junit.framework.TestCase;
072: import junit.framework.TestSuite;
073:
074: import org.apache.wsif.WSIFMessage;
075: import org.apache.wsif.WSIFOperation;
076: import org.apache.wsif.WSIFPort;
077: import org.apache.wsif.WSIFService;
078: import org.apache.wsif.WSIFServiceFactory;
079: import org.apache.wsif.providers.WSIFDynamicTypeMapping;
080: import org.apache.wsif.schema.Parser;
081: import org.apache.wsif.util.WSIFUtils;
082:
083: import addressbook.wsiftypes.Address;
084: import addressbook.wsiftypes.Phone;
085:
086: import util.TestUtilities;
087:
088: /**
089: * Test the serializing and deserializing of WSIF objects
090: * @author Owen Burroughs <owenb@apache.org>
091: */
092: public class SerializationTest extends TestCase {
093: static String server = TestUtilities.getSoapServer().toUpperCase();
094:
095: public SerializationTest(String name) {
096: super (name);
097: }
098:
099: public static void main(String[] 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 dummyNS = "http://this.is.a.test/";
114:
115: // java.lang.String
116: QName qn1 = new QName(dummyNS, "string");
117: doItTM(qn1, java.lang.String.class);
118:
119: // int
120: QName qn2 = new QName(dummyNS, "int");
121: doItTM(qn2, int.class);
122:
123: // float
124: QName qn3 = new QName(dummyNS, "float");
125: doItTM(qn3, float.class);
126:
127: // double
128: QName qn4 = new QName(dummyNS, "double");
129: doItTM(qn4, double.class);
130:
131: // long
132: QName qn5 = new QName(dummyNS, "long");
133: doItTM(qn5, long.class);
134:
135: // short
136: QName qn6 = new QName(dummyNS, "short");
137: doItTM(qn6, short.class);
138:
139: // byte
140: QName qn7 = new QName(dummyNS, "byte");
141: doItTM(qn7, byte.class);
142:
143: // void
144: QName qn8 = new QName(dummyNS, "void");
145: doItTM(qn8, void.class);
146:
147: } catch (Exception e) {
148: System.out
149: .println("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
159: .newInstance();
160: WSIFService service = factory
161: .getService(
162: TestUtilities
163: .getWsdlPath("java\\test\\shop")
164: + "ShoppingCartAll.wsdl",
165: "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
166: "ShoppingCart_JavaService",
167: "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
168: "ShoppingCart_JavaPortType");
169: WSIFPort port = service.getPort();
170: //WSIFOperation op = port.createOperation("addItemOperation");
171: doItPort(port, "WSIFPort_Java");
172: } catch (Exception e) {
173: System.out
174: .println("\nAn error occured when running testWSIFPort_Java "
175: + e);
176: assertTrue(false);
177: }
178: }
179:
180: public void testWSIFPort_EJB() {
181: if (!TestUtilities.areWeTesting("ejb"))
182: return;
183: try {
184: // TEST EJB
185: WSIFServiceFactory factory = WSIFServiceFactory
186: .newInstance();
187: WSIFService service = factory
188: .getService(
189: TestUtilities
190: .getWsdlPath("java\\test\\shop")
191: + "ShoppingCartAll.wsdl",
192: "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
193: "ShoppingCart_EJBService",
194: "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
195: "ShoppingCart_EJBPortType");
196: WSIFPort port = service.getPort();
197: //WSIFOperation op = port.createOperation("addItemOperation");
198: doItPort(port, "WSIFPort_EJB");
199: } catch (Exception e) {
200: System.out
201: .println("\nAn error occured when running testWSIFPort_EJB "
202: + e);
203: assertTrue(false);
204: }
205: }
206:
207: public void testWSIFPort_ApacheSOAP() {
208: try {
209: // TEST APACHE SOAP
210: TestUtilities.setProviderForProtocol("soap");
211: WSIFServiceFactory factory = WSIFServiceFactory
212: .newInstance();
213: WSIFService service = factory
214: .getService(
215: TestUtilities
216: .getWsdlPath("java\\test\\addressbook\\wsifservice")
217: + "AddressBook.wsdl", null, null,
218: "http://wsifservice.addressbook/",
219: "AddressBook");
220: service.mapType(new javax.xml.namespace.QName(
221: "http://wsiftypes.addressbook/", "address"), Class
222: .forName("addressbook.wsiftypes.Address"));
223:
224: service.mapType(new javax.xml.namespace.QName(
225: "http://wsiftypes.addressbook/", "phone"), Class
226: .forName("addressbook.wsiftypes.Phone"));
227: WSIFPort port = service.getPort(server + "Port");
228: WSIFOperation op = port.createOperation("addEntry",
229: "AddEntryWholeNameRequest", null);
230: doItPort(port, "WSIFPort_ApacheSOAP");
231: WSIFOperation op2 = port.createOperation("addEntry",
232: "AddEntryWholeNameRequest", null);
233: WSIFMessage inputMessage = op2.createInputMessage();
234: WSIFMessage outputMessage = op2.createOutputMessage();
235: WSIFMessage faultMessage = op2.createFaultMessage();
236:
237: // Create a name and address to add to the addressbook
238: String nameToAdd = "Chris P. Bacon";
239: Address addressToAdd = new Address(1, "The Waterfront",
240: "Some City", "NY", 47907, new Phone(765, "494",
241: "4900"));
242:
243: // Add the name and address to the input message
244: inputMessage.setObjectPart("name", nameToAdd);
245: inputMessage.setObjectPart("address", addressToAdd);
246:
247: // Execute the operation, obtaining a flag to indicate its success
248: op2.executeInputOnlyOperation(inputMessage);
249: } catch (Exception e) {
250: System.out
251: .println("\nAn error occured when running testWSIFPort_ApacheSOAP "
252: + 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
262: .newInstance();
263: WSIFService service = factory
264: .getService(
265: TestUtilities
266: .getWsdlPath("java\\test\\addressbook\\wsifservice")
267: + "AddressBook.wsdl", null, null,
268: "http://wsifservice.addressbook/",
269: "AddressBook");
270: WSIFPort port = service.getPort(server + "Port");
271: WSIFOperation op = port.createOperation("addEntry",
272: "AddEntryWholeNameRequest", null);
273: doItPort(port, "WSIFPort_ApacheAxis");
274: } catch (Exception e) {
275: System.out
276: .println("\nAn error occured when running testWSIFPort_ApacheAxis "
277: + e);
278: assertTrue(false);
279: }
280: }
281:
282: public void testWSIFPort_JMS() {
283: if (!TestUtilities.areWeTesting("jms"))
284: return;
285: try {
286: // TEST APACHE AXIS
287: WSIFServiceFactory factory = WSIFServiceFactory
288: .newInstance();
289: WSIFService service = factory
290: .getService(
291: TestUtilities
292: .getWsdlPath("java\\test\\addressbook\\wsifservice")
293: + "AddressBook.wsdl", null, null,
294: "http://wsifservice.addressbook/",
295: "AddressBook");
296: WSIFPort port = service.getPort("NativeJmsPort");
297: WSIFOperation op = port.createOperation("addEntry",
298: "AddEntryWholeNameRequest", null);
299: doItPort(port, "WSIFPort_JMS");
300: } catch (Exception e) {
301: System.out
302: .println("\nAn error occured when running testWSIFPort_JMS "
303: + e);
304: assertTrue(false);
305: }
306: }
307:
308: public void testWSIFMessage() {
309: try {
310: // TEST JAVA
311: WSIFServiceFactory factory = WSIFServiceFactory
312: .newInstance();
313: WSIFService service = factory
314: .getService(
315: TestUtilities
316: .getWsdlPath("java\\test\\shop")
317: + "ShoppingCartAll.wsdl",
318: "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
319: "ShoppingCart_JavaService",
320: "http://www.shoppingcart.com/definitions/ShoppingCartInterface",
321: "ShoppingCart_JavaPortType");
322: WSIFPort port = service.getPort();
323: WSIFOperation op = port.createOperation("addItemOperation");
324: WSIFMessage message = op.createInputMessage();
325: message.setObjectPart("testPart1", "test1");
326: // message.setObjectPart("testPart2", new StringTokenizer("test2"));
327: doItMessage(message);
328: } catch (Exception e) {
329: System.out
330: .println("\nAn error occured when running testWSIFPort_Java "
331: + e);
332: assertTrue(false);
333: }
334: }
335:
336: public void testSchemaList() {
337: try {
338: List list = new ArrayList();
339: String wsdl = TestUtilities
340: .getWsdlPath("java\\test\\schema")
341: + "SchemaTest1.wsdl";
342: Definition def = WSIFUtils.readWSDL(null, wsdl);
343: Parser.getAllSchemaTypes(def, list, null);
344: doItSchemaList(list);
345: } catch (Exception e) {
346: System.out
347: .println("\nAn error occured when running testWSIFPort_Java "
348: + e);
349: assertTrue(false);
350: }
351: }
352:
353: private WSIFPort doItPort(WSIFPort port, String portType) {
354:
355: try {
356: WSIFPort p2 = (WSIFPort) writeItReadIt(port);
357: assertNotNull(p2);
358: System.out
359: .println("\n"
360: + portType
361: + " was serialized and deserialized without problem");
362: return p2;
363: } catch (Exception e) {
364: System.out.println("\nError testing port: " + portType
365: + " :- " + e);
366: System.out.println("The full stack trace is:");
367: e.printStackTrace();
368: assertTrue(false);
369: return null;
370: }
371: }
372:
373: private void doItTM(QName qn, Class cls) {
374: WSIFDynamicTypeMapping tm = new WSIFDynamicTypeMapping(qn, cls);
375:
376: try {
377: WSIFDynamicTypeMapping tma = (WSIFDynamicTypeMapping) writeItReadIt(tm);
378: assertNotNull(tma);
379: assertEquals(tm.getJavaType().getName(), tma.getJavaType()
380: .getName());
381: assertEquals(tm.getXmlType(), tma.getXmlType());
382: } catch (Exception e) {
383: System.out
384: .println("Error testing type mapping with for QName: "
385: + qn + "and Java type: " + cls + " :- " + e);
386: assertTrue(false);
387: }
388: }
389:
390: private void doItMessage(WSIFMessage message) {
391: try {
392: WSIFMessage m2 = (WSIFMessage) writeItReadIt(message);
393: assertNotNull(m2);
394: System.out
395: .println("\n WSIFMessage was serialized and deserialized without problem");
396: } catch (Exception e) {
397: System.out.println("\nError testing message:- " + e);
398: System.out.println("The full stack trace is:");
399: e.printStackTrace();
400: assertTrue(false);
401: }
402: }
403:
404: private void doItSchemaList(List list) {
405: try {
406: List l2 = (List) writeItReadIt(list);
407: assertNotNull(l2);
408: System.out
409: .println("\n Schema List was serialized and deserialized without problem");
410: } catch (Exception e) {
411: System.out.println("\nError testing schema list:- " + e);
412: System.out.println("The full stack trace is:");
413: e.printStackTrace();
414: assertTrue(false);
415: }
416: }
417:
418: private Object writeItReadIt(Object o) throws Exception {
419: ByteArrayOutputStream b = new ByteArrayOutputStream();
420: ObjectOutputStream out = new ObjectOutputStream(b);
421: out.writeObject(o);
422: out.flush();
423: out.close();
424: byte[] data = b.toByteArray();
425: ObjectInputStream in = new ObjectInputStream(
426: new ByteArrayInputStream(data));
427: Object o2 = in.readObject();
428: in.close();
429: return o2;
430: }
431: }
|