KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > service > F_AxisService


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_AxisService.java,v 1.11 2005/05/12 15:29:35 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jtests.clients.service;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.BufferedWriter JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.Reader JavaDoc;
33 import java.io.StringReader JavaDoc;
34 import java.io.StringWriter JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Properties JavaDoc;
39 import javax.naming.RefAddr JavaDoc;
40 import javax.naming.Reference JavaDoc;
41 import javax.naming.StringRefAddr JavaDoc;
42 import javax.xml.namespace.QName JavaDoc;
43 import junit.framework.Test;
44 import junit.framework.TestSuite;
45 import org.objectweb.jonas_lib.deployment.digester.JDigester;
46 import org.objectweb.jonas_lib.deployment.rules.JonasServiceRefRuleSet;
47 import org.objectweb.jonas_lib.deployment.rules.ServiceRefRuleSet;
48 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
49 import org.objectweb.jonas_ws.deployment.api.WSDLFile;
50
51 import org.objectweb.jonas.common.JNDIUtils;
52 import org.objectweb.jonas.jtests.util.JWebServicesTestCase;
53 import org.objectweb.jonas.ws.JServiceFactory;
54 import org.objectweb.jonas.ws.axis.JAxisServiceFactory;
55
56 /**
57  * test case for axis service instanciation
58  */

59 public class F_AxisService extends JWebServicesTestCase {
60     private String JavaDoc resources = null;
61
62     private JServiceFactory jfactory = null;
63
64     public F_AxisService(String JavaDoc name) {
65         super(name);
66     }
67
68     public static Test suite() {
69         return new TestSuite(F_AxisService.class);
70     }
71
72     public void setUp() throws Exception JavaDoc {
73         super.setUp();
74         resources = System.getProperty("ws.resources");
75         jfactory = new JAxisServiceFactory();
76     }
77
78     public void tearDown() throws Exception JavaDoc {
79         jfactory = null;
80         super.tearDown();
81     }
82
83     public void testClientWSDDDefaultWhenWSDDNotSpecified() throws Exception JavaDoc {
84         String JavaDoc expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
85                 + "<deployment name=\"defaultClientConfig\""
86                 + " xmlns=\"http://xml.apache.org/axis/wsdd/\""
87                 + " xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">\n"
88                 + " <globalConfiguration>\n"
89                 + " <!-- Add global configurations elements here -->\n"
90                 + " <parameter name=\"disablePrettyXML\" value=\"true\"/>\n"
91                 + " <parameter name=\"sendMultiRefs\" value=\"false\"/>\n"
92                 + " </globalConfiguration>\n"
93                 + " <transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\"/>\n"
94                 + " <transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>\n"
95                 + " <transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\"/>\n"
96                 + "</deployment>\n";
97         Reference JavaDoc ref = createServiceReference("no-wsdd-specified-j2ee.xml",
98                 "no-wsdd-specified-jonas.xml");
99         assertNotNull("Shouldn't have a wsdd", ref
100                 .get(JAxisServiceFactory.REF_CLIENT_CONFIG));
101         assertEquals("WSDD is not the default one", expected, ref.get(
102                 JAxisServiceFactory.REF_CLIENT_CONFIG).getContent());
103     }
104
105     public void testClientWSDDMergingWhenWSDDSpecified() throws Exception JavaDoc {
106         String JavaDoc expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
107                 + "<deployment name=\"defaultClientConfig\""
108                 + " xmlns=\"http://xml.apache.org/axis/wsdd/\""
109                 + " xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">\n"
110                 + " <globalConfiguration>\n"
111                 + " <!-- Add global configurations elements here -->\n"
112                 + " <parameter name=\"disablePrettyXML\" value=\"true\"/>\n"
113                 + " <parameter name=\"sendMultiRefs\" value=\"false\"/>\n"
114                 + " </globalConfiguration>\n"
115                 + " <transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\"/>\n"
116                 + " <transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>\n"
117                 + " <transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\"/>\n"
118                 + " <service name=\"HelloService\"/>\n" + "</deployment>\n";
119         Reference JavaDoc ref = createServiceReference("wsdd-specified-j2ee.xml",
120                 "wsdd-specified-jonas.xml");
121         assertEquals("Something is missing in serialized WSDD", expected, ref
122                 .get(JAxisServiceFactory.REF_CLIENT_CONFIG).getContent());
123     }
124
125     public void testWSDLUrlWithNoWSDLOverride() throws Exception JavaDoc {
126         Reference JavaDoc ref = createServiceReference("wsdl-url-default-j2ee.xml",
127                 "wsdl-url-default-jonas.xml");
128         assertEquals("WSDL URL is not set appropriately", "file:"
129                 + this.resources + "/wsdl/oneService.wsdl", ref.get(
130                 JAxisServiceFactory.REF_SERVICE_WSDL).getContent());
131     }
132
133     public void testWSDLUrlWithWSDLOverride() throws Exception JavaDoc {
134         Reference JavaDoc ref = createServiceReference("wsdl-url-override-j2ee.xml",
135                 "wsdl-url-override-jonas.xml");
136         // create matching URL
137
URL JavaDoc urlMatching = new File JavaDoc(new File JavaDoc(resources, "wsdl"), "twoService.wsdl").toURL();
138         assertEquals(
139                 "WSDL URL is not set appropriately",
140                 urlMatching.toString(),
141                 ref.get(JAxisServiceFactory.REF_SERVICE_WSDL).getContent());
142     }
143
144     public void testServiceQNameWhenNotSpecified() throws Exception JavaDoc {
145         Reference JavaDoc ref = createServiceReference("service-qname-auto-j2ee.xml",
146                 "service-qname-auto-jonas.xml");
147         QName JavaDoc exp = new QName JavaDoc("http://hello.simple", "HelloWsService");
148         QName JavaDoc auto = (QName JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) ref.get(
149                 JAxisServiceFactory.REF_SERVICE_QNAME).getContent());
150         assertEquals("Service QName is incorrect", exp, auto);
151     }
152
153     public void testServiceQNameWhenExplicitelySet() throws Exception JavaDoc {
154         Reference JavaDoc ref = createServiceReference(
155                 "service-qname-explicit-j2ee.xml",
156                 "service-qname-explicit-jonas.xml");
157         QName JavaDoc exp = new QName JavaDoc("http://hello.simple", "HelloWsService2");
158         QName JavaDoc auto = (QName JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) ref.get(
159                 JAxisServiceFactory.REF_SERVICE_QNAME).getContent());
160         assertEquals("Service QName is incorrect", exp, auto);
161     }
162
163     public void testPort2WSDLMapWhenNoPortComponentRefSpecified()
164             throws Exception JavaDoc {
165         Reference JavaDoc ref = createServiceReference("no-port2wsdl-map-j2ee.xml",
166                 "no-port2wsdl-map-jonas.xml");
167         assertNull("Shouldn't have a Port2WSDL Map", ref
168                 .get(JAxisServiceFactory.REF_SERVICE_PORT2WSDL));
169     }
170
171     public void testPort2WSDLMapWithMultiplePortComponent() throws Exception JavaDoc {
172         Reference JavaDoc ref = createServiceReference("port2wsdl-map-j2ee.xml",
173                 "port2wsdl-map-jonas.xml");
174         Map JavaDoc exp = new HashMap JavaDoc();
175         exp.put("org.objectweb.jonas.jtests.hello.HelloWs", new QName JavaDoc(
176                 "http://hello.simple", "HelloPortComponent"));
177         exp.put("org.objectweb.jonas.jtests.hello.HelloWs2", new QName JavaDoc(
178                 "http://hello.simple", "HelloPortComponent2"));
179         Map JavaDoc returned = (Map JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) ref.get(
180                 JAxisServiceFactory.REF_SERVICE_PORT2WSDL).getContent());
181         assertEquals("Map are not identical", exp, returned);
182     }
183
184     public void testStubPropertiesNull() throws Exception JavaDoc {
185         Reference JavaDoc ref = createServiceReference("stub-props-null-j2ee.xml",
186                 "stub-props-null-jonas.xml");
187         assertNull("Stub properties should be null", ref
188                 .get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
189                         + "_HelloPortComponent"));
190     }
191
192     public void testStubPropertiesEmpty() throws Exception JavaDoc {
193         Reference JavaDoc ref = createServiceReference("stub-props-empty-j2ee.xml",
194                 "stub-props-empty-jonas.xml");
195         RefAddr JavaDoc ra = ref.get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
196                 + "_HelloPortComponent");
197         assertNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
198                 + "_HelloPortComponent" + " must be null", ra);
199         RefAddr JavaDoc ra2 = ref.get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
200                 + "_HelloPortComponent2");
201         assertNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
202                 + "_HelloPortComponent2" + " must be null", ra2);
203     }
204
205     public void testStubPropertiesForGivenPortName() throws Exception JavaDoc {
206         Reference JavaDoc ref = createServiceReference("stub-props-j2ee.xml",
207                 "stub-props-jonas.xml");
208         // port #1
209
RefAddr JavaDoc ra = ref.get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
210                 + "_HelloPortComponent");
211         assertNotNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
212                 + "_HelloPortComponent" + " must be not null", ra);
213         Map JavaDoc returned = (Map JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) ra
214                 .getContent());
215         assertFalse("Stub properties shouldn't be empty", returned.isEmpty());
216         Properties JavaDoc exp = new Properties JavaDoc();
217         exp.setProperty("my.test.property", "test.value");
218         exp.setProperty("my.test.property.2", "test.value.2");
219         assertEquals(
220                 "Stub Properties for port 'HelloPortComponent' not identical",
221                 exp, returned);
222         // port #2
223
RefAddr JavaDoc ra2 = ref.get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
224                 + "_HelloPortComponent2");
225         assertNotNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
226                 + "_HelloPortComponent2" + " must be not null", ra2);
227         Map JavaDoc returned2 = (Map JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) ra2
228                 .getContent());
229         assertFalse("Stub properties shouldn't be empty", returned2.isEmpty());
230         Properties JavaDoc exp2 = new Properties JavaDoc();
231         exp2.setProperty("other.test.property", "test.value");
232         exp2.setProperty("other.test.property.2", "test.value.2");
233         assertEquals(
234                 "Stub Properties for port 'HelloPortComponent' not identical",
235                 exp2, returned2);
236     }
237
238     public void testCallPropertiesNull() throws Exception JavaDoc {
239         Reference JavaDoc ref = createServiceReference("call-props-null-j2ee.xml",
240                 "call-props-null-jonas.xml");
241         assertNull("Call properties should be null", ref
242                 .get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
243                         + "_HelloPortComponent"));
244     }
245
246     public void testCallPropertiesEmpty() throws Exception JavaDoc {
247         Reference JavaDoc ref = createServiceReference("call-props-empty-j2ee.xml",
248                 "call-props-empty-jonas.xml");
249         RefAddr JavaDoc ra = ref.get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
250                 + "_HelloPortComponent");
251         assertNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
252                 + "_HelloPortComponent" + " must be null", ra);
253         RefAddr JavaDoc ra2 = ref.get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
254                 + "_HelloPortComponent2");
255         assertNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
256                 + "_HelloPortComponent2" + " must be null", ra2);
257     }
258
259     public void testCallPropertiesForGivenPortName() throws Exception JavaDoc {
260         Reference JavaDoc ref = createServiceReference("call-props-j2ee.xml",
261                 "call-props-jonas.xml");
262         // port #1
263
RefAddr JavaDoc ra = ref.get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
264                 + "_HelloPortComponent");
265         assertNotNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
266                 + "_HelloPortComponent" + " must be not null", ra);
267         Map JavaDoc returned = (Map JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) ra
268                 .getContent());
269         assertFalse("Stub properties shouldn't be empty", returned.isEmpty());
270         Properties JavaDoc exp = new Properties JavaDoc();
271         exp.setProperty("my.test.property", "test.value");
272         exp.setProperty("my.test.property.2", "test.value.2");
273         assertEquals(
274                 "Stub Properties for port 'HelloPortComponent' not identical",
275                 exp, returned);
276         // port #2
277
RefAddr JavaDoc ra2 = ref.get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
278                 + "_HelloPortComponent2");
279         assertNotNull("RefAddr " + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
280                 + "_HelloPortComponent2" + " must be not null", ra2);
281         Map JavaDoc returned2 = (Map JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) ra2
282                 .getContent());
283         assertFalse("Call properties shouldn't be empty", returned2.isEmpty());
284         Properties JavaDoc exp2 = new Properties JavaDoc();
285         exp2.setProperty("other.test.property", "test.value");
286         exp2.setProperty("other.test.property.2", "test.value.2");
287         assertEquals(
288                 "Call Properties for port 'HelloPortComponent' not identical",
289                 exp2, returned2);
290     }
291
292     public void testPortNameListEmpty() throws Exception JavaDoc {
293         Reference JavaDoc ref = createServiceReference("port-name-list-empty-j2ee.xml",
294                 "port-name-list-empty-jonas.xml");
295         assertNull(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST
296                 + " should be null", ref
297                 .get(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST));
298     }
299
300     public void testPortNameListWithMultiplePortComponent() throws Exception JavaDoc {
301         Reference JavaDoc ref = createServiceReference("port-name-list-j2ee.xml",
302                 "port-name-list-jonas.xml");
303         RefAddr JavaDoc ra = ref.get(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST);
304         assertNotNull(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST
305                 + " shouldn't be null", ra);
306         assertEquals("portname list incorrect",
307                 "HelloPortComponent,HelloPortComponent2", ra.getContent());
308     }
309
310     private Reference JavaDoc createServiceReference(String JavaDoc standard, String JavaDoc specific)
311             throws Exception JavaDoc {
312         // Get a ServiceRef for the test
313
ServiceRefDesc sr = createServiceRefDesc(standard, specific);
314         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
315         return jfactory.getServiceReference(sr, cl);
316     }
317
318     public void testGetObjectInstance() throws Exception JavaDoc {
319         String JavaDoc cfg = "<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" "
320                 + "xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">"
321                 + "<service name=\"AxisServiceTest\"/>"
322                 + "<transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\"/>"
323                 + "<transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\"/>"
324                 + "<transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>"
325                 + "</deployment>";
326         Reference JavaDoc ref = new Reference JavaDoc(
327                 "org.objectweb.jonas.jtests.hello.HelloWsServiceLocator",
328                 jfactory.getClass().getName(), null);
329         ref.add(new StringRefAddr JavaDoc(JAxisServiceFactory.REF_CLIENT_CONFIG, cfg));
330         try {
331             jfactory.getObjectInstance(ref, null, null, null);
332         } catch (Exception JavaDoc e) {
333             fail("JAxisServiceFactory.getObjectInstance has failed " + e);
334         }
335         // cannot success when using Proxies
336
}
337
338     private ServiceRefDesc createServiceRefDesc(String JavaDoc srFilename,
339             String JavaDoc jsrFilename) throws Exception JavaDoc {
340         // Get Context ClassLoader
341
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
342         ServiceRefTLE srTopLevel = new ServiceRefTLE();
343         // Load ServiceRef Object
344
//-----------------------------
345
// Create Digester
346
JDigester serviceRefDigester = new JDigester(new ServiceRefRuleSet(""),
347                 true, true, null, null);
348         // Get Reader
349
Reader JavaDoc r = new InputStreamReader JavaDoc(cl.getResourceAsStream(srFilename));
350         // Parse
351
serviceRefDigester.parse(r, srFilename, srTopLevel);
352         // Load JonasServiceRef Object
353
//-----------------------------
354
// Create Digester
355
JDigester jonasServiceRefDigester = new JDigester(
356                 new JonasServiceRefRuleSet(""), true, true, null, null);
357
358         // Read xml file and modify path containing RESOURCES_DIR
359
InputStream JavaDoc contentIs = cl.getResourceAsStream(jsrFilename);
360         BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(contentIs));
361         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
362         String JavaDoc line = null;
363         while (((line = bufferedReader.readLine()) != null)) {
364             if (line.indexOf("@@RESOURCES_DIR@@") != -1) {
365                 line = line.replaceAll("@@RESOURCES_DIR@@", "file://" + resources);
366             }
367             stringWriter.write(line);
368         }
369
370
371
372         // Get Reader
373
r = new StringReader JavaDoc(stringWriter.toString());
374         bufferedReader.close();
375         stringWriter.close();
376         // Parse
377
jonasServiceRefDigester.parse(r, jsrFilename, srTopLevel);
378         ServiceRefDesc sr = new ServiceRefDesc(cl, srTopLevel.getServiceRef(),
379                 srTopLevel.getJonasServiceRef(), resources);
380
381         return sr;
382     }
383
384     public static void main(String JavaDoc args[]) {
385         String JavaDoc testtorun = null;
386         // Get args
387
for (int argn = 0; argn < args.length; argn++) {
388             String JavaDoc s_arg = args[argn];
389             if (s_arg.equals("-n")) {
390                 testtorun = args[++argn];
391             }
392         }
393         if (testtorun == null) {
394             junit.textui.TestRunner.run(suite());
395         } else {
396             junit.textui.TestRunner.run(new F_AxisService(testtorun));
397         }
398     }
399 }
Popular Tags