1 package test.encoding; 2 3 import junit.framework.TestCase; 4 import org.apache.axis.client.Call; 5 import org.apache.axis.client.Service; 6 import org.apache.axis.configuration.SimpleProvider; 7 import org.apache.axis.description.ServiceDesc; 8 import org.apache.axis.handlers.soap.SOAPService; 9 import org.apache.axis.providers.java.RPCProvider; 10 import org.apache.axis.server.AxisServer; 11 import org.apache.axis.transport.local.LocalTransport; 12 13 import javax.xml.namespace.QName ; 14 import javax.xml.rpc.ParameterMode ; 15 import java.util.Iterator ; 16 import java.util.LinkedList ; 17 import java.util.List ; 18 import java.util.Vector ; 19 20 public class TestArrayListConversions extends TestCase { 21 private static final String SERVICE_NAME = "TestArrayConversions"; 22 23 private AxisServer server; 24 private LocalTransport transport; 25 26 public TestArrayListConversions() { 27 super("service"); 28 } 29 30 public TestArrayListConversions(String name) { 31 super(name); 32 } 33 34 private static boolean equals(List list, Object obj) { 35 if ((list == null) || (obj == null)) 36 return false; 37 38 if (!obj.getClass().isArray()) return false; 39 40 Object [] array = (Object []) obj; 41 Iterator iter = list.iterator(); 42 43 for (int i = 0; i < array.length; i++) { 44 if (!(array[i].equals(iter.next()))) { 45 return false; 46 } 47 } 48 49 return true; 50 } 51 52 protected void setUp() throws Exception { 53 try { 54 SimpleProvider provider = new SimpleProvider(); 55 server = new AxisServer(provider); 56 transport = new LocalTransport(server); 57 58 SOAPService service = new SOAPService(new RPCProvider()); 59 service.setEngine(server); 60 61 service.setOption("className", "test.encoding.TestArrayListConversions"); 62 service.setOption("allowedMethods", "*"); 63 64 ServiceDesc desc = service.getInitializedServiceDesc(null); 65 desc.setDefaultNamespace(SERVICE_NAME); 66 67 provider.deployService(SERVICE_NAME, service); 68 } catch (Exception exp) { 69 exp.printStackTrace(); 70 } 71 72 } 73 74 public void testVectorConversion() throws Exception { 75 Call call = new Call(new Service()); 76 call.setTransport(transport); 77 78 Vector v = new Vector (); 79 v.addElement("Hi there!"); 80 v.addElement("This'll be a SOAP Array and then a LinkedList!"); 81 call.setOperationName(new QName (SERVICE_NAME, "echoLinkedList")); 82 Object ret = call.invoke(new Object []{v}); 83 if (!equals(v, ret)) assertEquals("Echo LinkedList mangled the result. Result is underneath.\n" + ret, v, ret); 84 } 85 86 public void testLinkedListConversion() throws Exception { 87 Call call = new Call(new Service()); 88 call.setTransport(transport); 89 90 LinkedList l = new LinkedList (); 91 l.add("Linked list item #1"); 92 l.add("Second linked list item"); 93 l.add("This will be a SOAP Array then a Vector!"); 94 95 call.setOperationName(new QName (SERVICE_NAME, "echoVector")); 96 Object ret = call.invoke(new Object []{l}); 97 if (!equals(l, ret)) assertEquals("Echo Vector mangled the result. Result is underneath.\n" + ret, l, ret); 98 } 99 100 public void testArrayConversion() throws Exception { 101 Call call = new Call(new Service()); 102 call.setTransport(transport); 103 104 Vector v = new Vector (); 105 v.addElement("Hi there!"); 106 v.addElement("This'll be a SOAP Array"); 107 108 call.setOperationName(new QName (SERVICE_NAME, "echoArray")); 109 Object ret = call.invoke(new Object []{v}); 110 if (!equals(v, ret)) assertEquals("Echo Array mangled the result. Result is underneath\n" + ret, v, ret); 111 } 112 113 118 public void testReturnAsVector() throws Exception { 119 Call call = new Call(new Service()); 120 call.setTransport(transport); 121 122 LinkedList l = new LinkedList (); 123 l.add("Linked list item #1"); 124 l.add("Second linked list item"); 125 l.add("This will be a SOAP Array then a Vector!"); 126 127 call.setOperationName(new QName (SERVICE_NAME, "echoArray")); 128 call.addParameter("arg0", null, LinkedList .class, ParameterMode.IN); 129 call.setReturnClass(Vector .class); 130 Object ret = call.invoke(new Object []{l}); 131 assertEquals("Return wasn't a Vector!", Vector .class, ret.getClass()); 132 Vector v = (Vector )ret; 133 assertEquals("Sizes were different", l.size(), v.size()); 134 for (int i = 0; i < l.size(); i++) { 135 String s = (String )l.get(i); 136 assertEquals("Value " + i + " didn't match", s, v.get(i)); 137 } 138 } 139 140 public static void main(String [] args) { 141 TestArrayListConversions tester = new TestArrayListConversions("TestArrayListConversions"); 142 try { 143 tester.setUp(); 144 tester.testArrayConversion(); 145 tester.testLinkedListConversion(); 146 tester.testVectorConversion(); 147 tester.testReturnAsVector(); 148 } catch (Exception e) { 149 e.printStackTrace(); 150 } 151 } 152 153 159 public LinkedList echoLinkedList(LinkedList l) { 160 return l; 161 } 162 163 public Vector echoVector(Vector v) { 164 return v; 165 } 166 167 public Object [] echoArray(Object [] array) { 168 return array; 169 } 170 } 171 | Popular Tags |