001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: F_AxisService.java 6743 2005-05-12 15:29:35Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jtests.clients.service;
025:
026: import java.io.BufferedReader;
027: import java.io.BufferedWriter;
028: import java.io.File;
029: import java.io.InputStream;
030: import java.io.InputStreamReader;
031: import java.io.Reader;
032: import java.io.StringReader;
033: import java.io.StringWriter;
034: import java.net.URL;
035: import java.util.HashMap;
036: import java.util.Map;
037: import java.util.Properties;
038: import javax.naming.RefAddr;
039: import javax.naming.Reference;
040: import javax.naming.StringRefAddr;
041: import javax.xml.namespace.QName;
042: import junit.framework.Test;
043: import junit.framework.TestSuite;
044: import org.objectweb.jonas_lib.deployment.digester.JDigester;
045: import org.objectweb.jonas_lib.deployment.rules.JonasServiceRefRuleSet;
046: import org.objectweb.jonas_lib.deployment.rules.ServiceRefRuleSet;
047: import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
048: import org.objectweb.jonas_ws.deployment.api.WSDLFile;
049:
050: import org.objectweb.jonas.common.JNDIUtils;
051: import org.objectweb.jonas.jtests.util.JWebServicesTestCase;
052: import org.objectweb.jonas.ws.JServiceFactory;
053: import org.objectweb.jonas.ws.axis.JAxisServiceFactory;
054:
055: /**
056: * test case for axis service instanciation
057: */
058: public class F_AxisService extends JWebServicesTestCase {
059: private String resources = null;
060:
061: private JServiceFactory jfactory = null;
062:
063: public F_AxisService(String name) {
064: super (name);
065: }
066:
067: public static Test suite() {
068: return new TestSuite(F_AxisService.class);
069: }
070:
071: public void setUp() throws Exception {
072: super .setUp();
073: resources = System.getProperty("ws.resources");
074: jfactory = new JAxisServiceFactory();
075: }
076:
077: public void tearDown() throws Exception {
078: jfactory = null;
079: super .tearDown();
080: }
081:
082: public void testClientWSDDDefaultWhenWSDDNotSpecified()
083: throws Exception {
084: String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
085: + "<deployment name=\"defaultClientConfig\""
086: + " xmlns=\"http://xml.apache.org/axis/wsdd/\""
087: + " xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">\n"
088: + " <globalConfiguration>\n"
089: + " <!-- Add global configurations elements here -->\n"
090: + " <parameter name=\"disablePrettyXML\" value=\"true\"/>\n"
091: + " <parameter name=\"sendMultiRefs\" value=\"false\"/>\n"
092: + " </globalConfiguration>\n"
093: + " <transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\"/>\n"
094: + " <transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>\n"
095: + " <transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\"/>\n"
096: + "</deployment>\n";
097: Reference ref = createServiceReference(
098: "no-wsdd-specified-j2ee.xml",
099: "no-wsdd-specified-jonas.xml");
100: assertNotNull("Shouldn't have a wsdd", ref
101: .get(JAxisServiceFactory.REF_CLIENT_CONFIG));
102: assertEquals("WSDD is not the default one", expected, ref.get(
103: JAxisServiceFactory.REF_CLIENT_CONFIG).getContent());
104: }
105:
106: public void testClientWSDDMergingWhenWSDDSpecified()
107: throws Exception {
108: String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
109: + "<deployment name=\"defaultClientConfig\""
110: + " xmlns=\"http://xml.apache.org/axis/wsdd/\""
111: + " xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">\n"
112: + " <globalConfiguration>\n"
113: + " <!-- Add global configurations elements here -->\n"
114: + " <parameter name=\"disablePrettyXML\" value=\"true\"/>\n"
115: + " <parameter name=\"sendMultiRefs\" value=\"false\"/>\n"
116: + " </globalConfiguration>\n"
117: + " <transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\"/>\n"
118: + " <transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>\n"
119: + " <transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\"/>\n"
120: + " <service name=\"HelloService\"/>\n"
121: + "</deployment>\n";
122: Reference ref = createServiceReference(
123: "wsdd-specified-j2ee.xml", "wsdd-specified-jonas.xml");
124: assertEquals("Something is missing in serialized WSDD",
125: expected, ref
126: .get(JAxisServiceFactory.REF_CLIENT_CONFIG)
127: .getContent());
128: }
129:
130: public void testWSDLUrlWithNoWSDLOverride() throws Exception {
131: Reference ref = createServiceReference(
132: "wsdl-url-default-j2ee.xml",
133: "wsdl-url-default-jonas.xml");
134: assertEquals("WSDL URL is not set appropriately", "file:"
135: + this .resources + "/wsdl/oneService.wsdl", ref.get(
136: JAxisServiceFactory.REF_SERVICE_WSDL).getContent());
137: }
138:
139: public void testWSDLUrlWithWSDLOverride() throws Exception {
140: Reference ref = createServiceReference(
141: "wsdl-url-override-j2ee.xml",
142: "wsdl-url-override-jonas.xml");
143: // create matching URL
144: URL urlMatching = new File(new File(resources, "wsdl"),
145: "twoService.wsdl").toURL();
146: assertEquals("WSDL URL is not set appropriately", urlMatching
147: .toString(), ref.get(
148: JAxisServiceFactory.REF_SERVICE_WSDL).getContent());
149: }
150:
151: public void testServiceQNameWhenNotSpecified() throws Exception {
152: Reference ref = createServiceReference(
153: "service-qname-auto-j2ee.xml",
154: "service-qname-auto-jonas.xml");
155: QName exp = new QName("http://hello.simple", "HelloWsService");
156: QName auto = (QName) JNDIUtils.getObjectFromBytes((byte[]) ref
157: .get(JAxisServiceFactory.REF_SERVICE_QNAME)
158: .getContent());
159: assertEquals("Service QName is incorrect", exp, auto);
160: }
161:
162: public void testServiceQNameWhenExplicitelySet() throws Exception {
163: Reference ref = createServiceReference(
164: "service-qname-explicit-j2ee.xml",
165: "service-qname-explicit-jonas.xml");
166: QName exp = new QName("http://hello.simple", "HelloWsService2");
167: QName auto = (QName) JNDIUtils.getObjectFromBytes((byte[]) ref
168: .get(JAxisServiceFactory.REF_SERVICE_QNAME)
169: .getContent());
170: assertEquals("Service QName is incorrect", exp, auto);
171: }
172:
173: public void testPort2WSDLMapWhenNoPortComponentRefSpecified()
174: throws Exception {
175: Reference ref = createServiceReference(
176: "no-port2wsdl-map-j2ee.xml",
177: "no-port2wsdl-map-jonas.xml");
178: assertNull("Shouldn't have a Port2WSDL Map", ref
179: .get(JAxisServiceFactory.REF_SERVICE_PORT2WSDL));
180: }
181:
182: public void testPort2WSDLMapWithMultiplePortComponent()
183: throws Exception {
184: Reference ref = createServiceReference(
185: "port2wsdl-map-j2ee.xml", "port2wsdl-map-jonas.xml");
186: Map exp = new HashMap();
187: exp.put("org.objectweb.jonas.jtests.hello.HelloWs", new QName(
188: "http://hello.simple", "HelloPortComponent"));
189: exp.put("org.objectweb.jonas.jtests.hello.HelloWs2", new QName(
190: "http://hello.simple", "HelloPortComponent2"));
191: Map returned = (Map) JNDIUtils.getObjectFromBytes((byte[]) ref
192: .get(JAxisServiceFactory.REF_SERVICE_PORT2WSDL)
193: .getContent());
194: assertEquals("Map are not identical", exp, returned);
195: }
196:
197: public void testStubPropertiesNull() throws Exception {
198: Reference ref = createServiceReference(
199: "stub-props-null-j2ee.xml", "stub-props-null-jonas.xml");
200: assertNull("Stub properties should be null", ref
201: .get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
202: + "_HelloPortComponent"));
203: }
204:
205: public void testStubPropertiesEmpty() throws Exception {
206: Reference ref = createServiceReference(
207: "stub-props-empty-j2ee.xml",
208: "stub-props-empty-jonas.xml");
209: RefAddr ra = ref.get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
210: + "_HelloPortComponent");
211: assertNull("RefAddr "
212: + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
213: + "_HelloPortComponent" + " must be null", ra);
214: RefAddr ra2 = ref
215: .get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
216: + "_HelloPortComponent2");
217: assertNull("RefAddr "
218: + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
219: + "_HelloPortComponent2" + " must be null", ra2);
220: }
221:
222: public void testStubPropertiesForGivenPortName() throws Exception {
223: Reference ref = createServiceReference("stub-props-j2ee.xml",
224: "stub-props-jonas.xml");
225: // port #1
226: RefAddr ra = ref.get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
227: + "_HelloPortComponent");
228: assertNotNull("RefAddr "
229: + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
230: + "_HelloPortComponent" + " must be not null", ra);
231: Map returned = (Map) JNDIUtils.getObjectFromBytes((byte[]) ra
232: .getContent());
233: assertFalse("Stub properties shouldn't be empty", returned
234: .isEmpty());
235: Properties exp = new Properties();
236: exp.setProperty("my.test.property", "test.value");
237: exp.setProperty("my.test.property.2", "test.value.2");
238: assertEquals(
239: "Stub Properties for port 'HelloPortComponent' not identical",
240: exp, returned);
241: // port #2
242: RefAddr ra2 = ref
243: .get(JAxisServiceFactory.REF_SERVICE_STUB_PROPS
244: + "_HelloPortComponent2");
245: assertNotNull("RefAddr "
246: + JAxisServiceFactory.REF_SERVICE_STUB_PROPS
247: + "_HelloPortComponent2" + " must be not null", ra2);
248: Map returned2 = (Map) JNDIUtils.getObjectFromBytes((byte[]) ra2
249: .getContent());
250: assertFalse("Stub properties shouldn't be empty", returned2
251: .isEmpty());
252: Properties exp2 = new Properties();
253: exp2.setProperty("other.test.property", "test.value");
254: exp2.setProperty("other.test.property.2", "test.value.2");
255: assertEquals(
256: "Stub Properties for port 'HelloPortComponent' not identical",
257: exp2, returned2);
258: }
259:
260: public void testCallPropertiesNull() throws Exception {
261: Reference ref = createServiceReference(
262: "call-props-null-j2ee.xml", "call-props-null-jonas.xml");
263: assertNull("Call properties should be null", ref
264: .get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
265: + "_HelloPortComponent"));
266: }
267:
268: public void testCallPropertiesEmpty() throws Exception {
269: Reference ref = createServiceReference(
270: "call-props-empty-j2ee.xml",
271: "call-props-empty-jonas.xml");
272: RefAddr ra = ref.get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
273: + "_HelloPortComponent");
274: assertNull("RefAddr "
275: + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
276: + "_HelloPortComponent" + " must be null", ra);
277: RefAddr ra2 = ref
278: .get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
279: + "_HelloPortComponent2");
280: assertNull("RefAddr "
281: + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
282: + "_HelloPortComponent2" + " must be null", ra2);
283: }
284:
285: public void testCallPropertiesForGivenPortName() throws Exception {
286: Reference ref = createServiceReference("call-props-j2ee.xml",
287: "call-props-jonas.xml");
288: // port #1
289: RefAddr ra = ref.get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
290: + "_HelloPortComponent");
291: assertNotNull("RefAddr "
292: + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
293: + "_HelloPortComponent" + " must be not null", ra);
294: Map returned = (Map) JNDIUtils.getObjectFromBytes((byte[]) ra
295: .getContent());
296: assertFalse("Stub properties shouldn't be empty", returned
297: .isEmpty());
298: Properties exp = new Properties();
299: exp.setProperty("my.test.property", "test.value");
300: exp.setProperty("my.test.property.2", "test.value.2");
301: assertEquals(
302: "Stub Properties for port 'HelloPortComponent' not identical",
303: exp, returned);
304: // port #2
305: RefAddr ra2 = ref
306: .get(JAxisServiceFactory.REF_SERVICE_CALL_PROPS
307: + "_HelloPortComponent2");
308: assertNotNull("RefAddr "
309: + JAxisServiceFactory.REF_SERVICE_CALL_PROPS
310: + "_HelloPortComponent2" + " must be not null", ra2);
311: Map returned2 = (Map) JNDIUtils.getObjectFromBytes((byte[]) ra2
312: .getContent());
313: assertFalse("Call properties shouldn't be empty", returned2
314: .isEmpty());
315: Properties exp2 = new Properties();
316: exp2.setProperty("other.test.property", "test.value");
317: exp2.setProperty("other.test.property.2", "test.value.2");
318: assertEquals(
319: "Call Properties for port 'HelloPortComponent' not identical",
320: exp2, returned2);
321: }
322:
323: public void testPortNameListEmpty() throws Exception {
324: Reference ref = createServiceReference(
325: "port-name-list-empty-j2ee.xml",
326: "port-name-list-empty-jonas.xml");
327: assertNull(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST
328: + " should be null", ref
329: .get(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST));
330: }
331:
332: public void testPortNameListWithMultiplePortComponent()
333: throws Exception {
334: Reference ref = createServiceReference(
335: "port-name-list-j2ee.xml", "port-name-list-jonas.xml");
336: RefAddr ra = ref
337: .get(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST);
338: assertNotNull(JAxisServiceFactory.REF_SERVICE_WSDL_PORT_LIST
339: + " shouldn't be null", ra);
340: assertEquals("portname list incorrect",
341: "HelloPortComponent,HelloPortComponent2", ra
342: .getContent());
343: }
344:
345: private Reference createServiceReference(String standard,
346: String specific) throws Exception {
347: // Get a ServiceRef for the test
348: ServiceRefDesc sr = createServiceRefDesc(standard, specific);
349: ClassLoader cl = Thread.currentThread().getContextClassLoader();
350: return jfactory.getServiceReference(sr, cl);
351: }
352:
353: public void testGetObjectInstance() throws Exception {
354: String cfg = "<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" "
355: + "xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">"
356: + "<service name=\"AxisServiceTest\"/>"
357: + "<transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\"/>"
358: + "<transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\"/>"
359: + "<transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>"
360: + "</deployment>";
361: Reference ref = new Reference(
362: "org.objectweb.jonas.jtests.hello.HelloWsServiceLocator",
363: jfactory.getClass().getName(), null);
364: ref.add(new StringRefAddr(
365: JAxisServiceFactory.REF_CLIENT_CONFIG, cfg));
366: try {
367: jfactory.getObjectInstance(ref, null, null, null);
368: } catch (Exception e) {
369: fail("JAxisServiceFactory.getObjectInstance has failed "
370: + e);
371: }
372: // cannot success when using Proxies
373: }
374:
375: private ServiceRefDesc createServiceRefDesc(String srFilename,
376: String jsrFilename) throws Exception {
377: // Get Context ClassLoader
378: ClassLoader cl = Thread.currentThread().getContextClassLoader();
379: ServiceRefTLE srTopLevel = new ServiceRefTLE();
380: // Load ServiceRef Object
381: //-----------------------------
382: // Create Digester
383: JDigester serviceRefDigester = new JDigester(
384: new ServiceRefRuleSet(""), true, true, null, null);
385: // Get Reader
386: Reader r = new InputStreamReader(cl
387: .getResourceAsStream(srFilename));
388: // Parse
389: serviceRefDigester.parse(r, srFilename, srTopLevel);
390: // Load JonasServiceRef Object
391: //-----------------------------
392: // Create Digester
393: JDigester jonasServiceRefDigester = new JDigester(
394: new JonasServiceRefRuleSet(""), true, true, null, null);
395:
396: // Read xml file and modify path containing RESOURCES_DIR
397: InputStream contentIs = cl.getResourceAsStream(jsrFilename);
398: BufferedReader bufferedReader = new BufferedReader(
399: new InputStreamReader(contentIs));
400: StringWriter stringWriter = new StringWriter();
401: String line = null;
402: while (((line = bufferedReader.readLine()) != null)) {
403: if (line.indexOf("@@RESOURCES_DIR@@") != -1) {
404: line = line.replaceAll("@@RESOURCES_DIR@@", "file://"
405: + resources);
406: }
407: stringWriter.write(line);
408: }
409:
410: // Get Reader
411: r = new StringReader(stringWriter.toString());
412: bufferedReader.close();
413: stringWriter.close();
414: // Parse
415: jonasServiceRefDigester.parse(r, jsrFilename, srTopLevel);
416: ServiceRefDesc sr = new ServiceRefDesc(cl, srTopLevel
417: .getServiceRef(), srTopLevel.getJonasServiceRef(),
418: resources);
419:
420: return sr;
421: }
422:
423: public static void main(String args[]) {
424: String testtorun = null;
425: // Get args
426: for (int argn = 0; argn < args.length; argn++) {
427: String s_arg = args[argn];
428: if (s_arg.equals("-n")) {
429: testtorun = args[++argn];
430: }
431: }
432: if (testtorun == null) {
433: junit.textui.TestRunner.run(suite());
434: } else {
435: junit.textui.TestRunner.run(new F_AxisService(testtorun));
436: }
437: }
438: }
|