1 57 58 package serialization; 59 60 import java.io.ByteArrayInputStream ; 61 import java.io.ByteArrayOutputStream ; 62 import java.io.ObjectInputStream ; 63 import java.io.ObjectOutputStream ; 64 import java.util.ArrayList ; 65 import java.util.List ; 66 import java.util.StringTokenizer ; 67 68 import javax.wsdl.Definition; 69 import javax.xml.namespace.QName ; 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 92 public class SerializationTest extends TestCase { 93 static String server = TestUtilities.getSoapServer().toUpperCase(); 94 95 public SerializationTest(String name) { 96 super(name); 97 } 98 99 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 QName qn1 = new QName (dummyNS, "string"); 117 doItTM(qn1, java.lang.String .class); 118 119 QName qn2 = new QName (dummyNS, "int"); 121 doItTM(qn2, int.class); 122 123 QName qn3 = new QName (dummyNS, "float"); 125 doItTM(qn3, float.class); 126 127 QName qn4 = new QName (dummyNS, "double"); 129 doItTM(qn4, double.class); 130 131 QName qn5 = new QName (dummyNS, "long"); 133 doItTM(qn5, long.class); 134 135 QName qn6 = new QName (dummyNS, "short"); 137 doItTM(qn6, short.class); 138 139 QName qn7 = new QName (dummyNS, "byte"); 141 doItTM(qn7, byte.class); 142 143 QName qn8 = new QName (dummyNS, "void"); 145 doItTM(qn8, void.class); 146 147 } catch (Exception 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 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 doItPort(port, "WSIFPort_Java"); 170 } catch (Exception 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 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 doItPort(port, "WSIFPort_EJB"); 193 } catch (Exception 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 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 ( 216 "http://wsiftypes.addressbook/", 217 "address"), 218 Class.forName("addressbook.wsiftypes.Address")); 219 220 service.mapType( 221 new javax.xml.namespace.QName ( 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 String 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 inputMessage.setObjectPart("name", nameToAdd); 246 inputMessage.setObjectPart("address", addressToAdd); 247 248 op2.executeInputOnlyOperation(inputMessage); 250 } catch (Exception 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 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 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 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 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 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 doItMessage(message); 323 } catch (Exception 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 list = new ArrayList (); 333 String 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 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 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 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 qn, Class 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 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 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 list) { 397 try { 398 List l2 = (List ) writeItReadIt(list); 399 assertNotNull(l2); 400 System.out.println("\n Schema List was serialized and deserialized without problem"); 401 } catch (Exception 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 writeItReadIt(Object o) throws Exception { 410 ByteArrayOutputStream b = new ByteArrayOutputStream (); 411 ObjectOutputStream out = new ObjectOutputStream (b); 412 out.writeObject(o); 413 out.flush(); 414 out.close(); 415 byte[] data = b.toByteArray(); 416 ObjectInputStream in = 417 new ObjectInputStream (new ByteArrayInputStream (data)); 418 Object o2 = in.readObject(); 419 in.close(); 420 return o2; 421 } 422 } 423 | Popular Tags |