KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestArrayListConversions


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 JavaDoc;
14 import javax.xml.rpc.ParameterMode JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 public class TestArrayListConversions extends TestCase {
21     private static final String JavaDoc 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 JavaDoc name) {
31         super(name);
32     }
33
34     private static boolean equals(List JavaDoc list, Object JavaDoc obj) {
35         if ((list == null) || (obj == null))
36             return false;
37
38         if (!obj.getClass().isArray()) return false;
39
40         Object JavaDoc[] array = (Object JavaDoc[]) obj;
41         Iterator JavaDoc 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 JavaDoc {
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 JavaDoc exp) {
69             exp.printStackTrace();
70         }
71
72     }
73
74     public void testVectorConversion() throws Exception JavaDoc {
75         Call call = new Call(new Service());
76         call.setTransport(transport);
77
78         Vector JavaDoc v = new Vector JavaDoc();
79         v.addElement("Hi there!");
80         v.addElement("This'll be a SOAP Array and then a LinkedList!");
81         call.setOperationName(new QName JavaDoc(SERVICE_NAME, "echoLinkedList"));
82         Object JavaDoc ret = call.invoke(new Object JavaDoc[]{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 JavaDoc {
87         Call call = new Call(new Service());
88         call.setTransport(transport);
89
90         LinkedList JavaDoc l = new LinkedList JavaDoc();
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 JavaDoc(SERVICE_NAME, "echoVector"));
96         Object JavaDoc ret = call.invoke(new Object JavaDoc[]{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 JavaDoc {
101         Call call = new Call(new Service());
102         call.setTransport(transport);
103
104         Vector JavaDoc v = new Vector JavaDoc();
105         v.addElement("Hi there!");
106         v.addElement("This'll be a SOAP Array");
107
108         call.setOperationName(new QName JavaDoc(SERVICE_NAME, "echoArray"));
109         Object JavaDoc ret = call.invoke(new Object JavaDoc[]{v});
110         if (!equals(v, ret)) assertEquals("Echo Array mangled the result. Result is underneath\n" + ret, v, ret);
111     }
112
113     /**
114      * Test the setReturnClass() API on Call by asking the runtime to
115      * give us back a Vector instead of an array. Confirm we get a Vector
116      * back, and that it matches the data we send.
117      */

118     public void testReturnAsVector() throws Exception JavaDoc {
119         Call call = new Call(new Service());
120         call.setTransport(transport);
121
122         LinkedList JavaDoc l = new LinkedList JavaDoc();
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 JavaDoc(SERVICE_NAME, "echoArray"));
128         call.addParameter("arg0", null, LinkedList JavaDoc.class, ParameterMode.IN);
129         call.setReturnClass(Vector JavaDoc.class);
130         Object JavaDoc ret = call.invoke(new Object JavaDoc[]{l});
131         assertEquals("Return wasn't a Vector!", Vector JavaDoc.class, ret.getClass());
132         Vector JavaDoc v = (Vector JavaDoc)ret;
133         assertEquals("Sizes were different", l.size(), v.size());
134         for (int i = 0; i < l.size(); i++) {
135             String JavaDoc s = (String JavaDoc)l.get(i);
136             assertEquals("Value " + i + " didn't match", s, v.get(i));
137         }
138     }
139
140     public static void main(String JavaDoc[] 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 JavaDoc e) {
149             e.printStackTrace();
150         }
151     }
152
153     /****************************************************************
154      *
155      * Service methods - this class is also deployed as an Axis RPC
156      * service for convenience. These guys just echo various things.
157      *
158      */

159     public LinkedList JavaDoc echoLinkedList(LinkedList JavaDoc l) {
160         return l;
161     }
162
163     public Vector JavaDoc echoVector(Vector JavaDoc v) {
164         return v;
165     }
166
167     public Object JavaDoc[] echoArray(Object JavaDoc[] array) {
168         return array;
169     }
170 }
171
Popular Tags