001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.systest.jms;
019:
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Proxy;
022: import java.lang.reflect.UndeclaredThrowableException;
023: import java.net.URL;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import javax.xml.namespace.QName;
028: import javax.xml.ws.BindingProvider;
029:
030: import org.apache.cxf.hello_world_jms.BadRecordLitFault;
031: import org.apache.cxf.hello_world_jms.HelloWorldOneWayPort;
032: import org.apache.cxf.hello_world_jms.HelloWorldOneWayQueueService;
033: import org.apache.cxf.hello_world_jms.HelloWorldPortType;
034: import org.apache.cxf.hello_world_jms.HelloWorldPubSubPort;
035: import org.apache.cxf.hello_world_jms.HelloWorldPubSubService;
036: import org.apache.cxf.hello_world_jms.HelloWorldService;
037: import org.apache.cxf.hello_world_jms.NoSuchCodeLitFault;
038: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
039: import org.apache.cxf.transport.jms.JMSConstants;
040: import org.apache.cxf.transport.jms.JMSMessageHeadersType;
041: import org.apache.cxf.transport.jms.JMSPropertyType;
042: import org.apache.hello_world_doc_lit.Greeter;
043: import org.apache.hello_world_doc_lit.PingMeFault;
044: import org.apache.hello_world_doc_lit.SOAPService2;
045: import org.junit.Before;
046: import org.junit.Test;
047:
048: import org.springframework.context.support.ClassPathXmlApplicationContext;
049:
050: public class JMSClientServerTest extends
051: AbstractBusClientServerTestBase {
052:
053: protected static boolean serversStarted;
054:
055: @Before
056: public void startServers() throws Exception {
057: if (serversStarted) {
058: return;
059: }
060: Map<String, String> props = new HashMap<String, String>();
061: if (System.getProperty("activemq.store.dir") != null) {
062: props.put("activemq.store.dir", System
063: .getProperty("activemq.store.dir"));
064: }
065: props.put("java.util.logging.config.file", System
066: .getProperty("java.util.logging.config.file"));
067:
068: assertTrue("server did not launch correctly", launchServer(
069: EmbeddedJMSBrokerLauncher.class, props, null));
070:
071: assertTrue("server did not launch correctly", launchServer(
072: Server.class, false));
073: serversStarted = true;
074: }
075:
076: public URL getWSDLURL(String s) throws Exception {
077: return getClass().getResource(s);
078: }
079:
080: public QName getServiceName(QName q) {
081: return q;
082: }
083:
084: public QName getPortName(QName q) {
085: return q;
086: }
087:
088: @Test
089: public void testDocBasicConnection() throws Exception {
090: QName serviceName = getServiceName(new QName(
091: "http://apache.org/hello_world_doc_lit", "SOAPService2"));
092: QName portName = getPortName(new QName(
093: "http://apache.org/hello_world_doc_lit", "SoapPort2"));
094: URL wsdl = getWSDLURL("/wsdl/hello_world_doc_lit.wsdl");
095: assertNotNull(wsdl);
096:
097: SOAPService2 service = new SOAPService2(wsdl, serviceName);
098: assertNotNull(service);
099:
100: String response1 = new String("Hello Milestone-");
101: String response2 = new String("Bonjour");
102: try {
103: Greeter greeter = service.getPort(portName, Greeter.class);
104: for (int idx = 0; idx < 5; idx++) {
105: String greeting = greeter.greetMe("Milestone-" + idx);
106: assertNotNull("no response received from service",
107: greeting);
108: String exResponse = response1 + idx;
109: assertEquals(exResponse, greeting);
110:
111: String reply = greeter.sayHi();
112: assertNotNull("no response received from service",
113: reply);
114: assertEquals(response2, reply);
115:
116: try {
117: greeter.pingMe();
118: fail("Should have thrown FaultException");
119: } catch (PingMeFault ex) {
120: assertNotNull(ex.getFaultInfo());
121: }
122:
123: }
124: } catch (UndeclaredThrowableException ex) {
125: throw (Exception) ex.getCause();
126: }
127: }
128:
129: @Test
130: public void testBasicConnection() throws Exception {
131: QName serviceName = getServiceName(new QName(
132: "http://cxf.apache.org/hello_world_jms",
133: "HelloWorldService"));
134: QName portName = getPortName(new QName(
135: "http://cxf.apache.org/hello_world_jms",
136: "HelloWorldPort"));
137: URL wsdl = getWSDLURL("/wsdl/jms_test.wsdl");
138: assertNotNull(wsdl);
139:
140: HelloWorldService service = new HelloWorldService(wsdl,
141: serviceName);
142: assertNotNull(service);
143:
144: String response1 = new String("Hello Milestone-");
145: String response2 = new String("Bonjour");
146: try {
147: HelloWorldPortType greeter = service.getPort(portName,
148: HelloWorldPortType.class);
149: for (int idx = 0; idx < 5; idx++) {
150: String greeting = greeter.greetMe("Milestone-" + idx);
151: assertNotNull("no response received from service",
152: greeting);
153: String exResponse = response1 + idx;
154: assertEquals(exResponse, greeting);
155:
156: String reply = greeter.sayHi();
157: assertNotNull("no response received from service",
158: reply);
159: assertEquals(response2, reply);
160:
161: try {
162: greeter.testRpcLitFault("BadRecordLitFault");
163: fail("Should have thrown BadRecoedLitFault");
164: } catch (BadRecordLitFault ex) {
165: assertNotNull(ex.getFaultInfo());
166: }
167:
168: try {
169: greeter.testRpcLitFault("NoSuchCodeLitFault");
170: fail("Should have thrown NoSuchCodeLitFault exception");
171: } catch (NoSuchCodeLitFault nslf) {
172: assertNotNull(nslf.getFaultInfo());
173: assertNotNull(nslf.getFaultInfo().getCode());
174: }
175: }
176: } catch (UndeclaredThrowableException ex) {
177: throw (Exception) ex.getCause();
178: }
179: }
180:
181: @Test
182: public void testOneWayTopicConnection() throws Exception {
183: QName serviceName = getServiceName(new QName(
184: "http://cxf.apache.org/hello_world_jms",
185: "HelloWorldPubSubService"));
186: QName portName = getPortName(new QName(
187: "http://cxf.apache.org/hello_world_jms",
188: "HelloWorldPubSubPort"));
189: URL wsdl = getClass().getResource("/wsdl/jms_test.wsdl");
190: assertNotNull(wsdl);
191:
192: HelloWorldPubSubService service = new HelloWorldPubSubService(
193: wsdl, serviceName);
194: assertNotNull(service);
195:
196: try {
197: HelloWorldPubSubPort greeter = service.getPort(portName,
198: HelloWorldPubSubPort.class);
199: for (int idx = 0; idx < 5; idx++) {
200: greeter.greetMeOneWay("JMS:PubSub:Milestone-" + idx);
201: }
202: //Give some time to complete one-way calls.
203: Thread.sleep(100L);
204: } catch (UndeclaredThrowableException ex) {
205: throw (Exception) ex.getCause();
206: }
207: }
208:
209: @Test
210: public void testConnectionsWithinSpring() throws Exception {
211: ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
212: new String[] { "/org/apache/cxf/systest/jms/JMSClients.xml" });
213:
214: HelloWorldPortType greeter = (HelloWorldPortType) ctx
215: .getBean("jmsRPCClient");
216: assertNotNull(greeter);
217:
218: String response1 = new String("Hello Milestone-");
219: String response2 = new String("Bonjour");
220: try {
221:
222: for (int idx = 0; idx < 5; idx++) {
223: String greeting = greeter.greetMe("Milestone-" + idx);
224: assertNotNull("no response received from service",
225: greeting);
226: String exResponse = response1 + idx;
227: assertEquals(exResponse, greeting);
228:
229: String reply = greeter.sayHi();
230: assertNotNull("no response received from service",
231: reply);
232: assertEquals(response2, reply);
233:
234: try {
235: greeter.testRpcLitFault("BadRecordLitFault");
236: fail("Should have thrown BadRecoedLitFault");
237: } catch (BadRecordLitFault ex) {
238: assertNotNull(ex.getFaultInfo());
239: }
240:
241: try {
242: greeter.testRpcLitFault("NoSuchCodeLitFault");
243: fail("Should have thrown NoSuchCodeLitFault exception");
244: } catch (NoSuchCodeLitFault nslf) {
245: assertNotNull(nslf.getFaultInfo());
246: assertNotNull(nslf.getFaultInfo().getCode());
247: }
248: }
249: } catch (UndeclaredThrowableException ex) {
250: throw (Exception) ex.getCause();
251: }
252:
253: HelloWorldOneWayPort greeter1 = (HelloWorldOneWayPort) ctx
254: .getBean("jmsQueueOneWayServiceClient");
255: assertNotNull(greeter1);
256: try {
257: greeter1.greetMeOneWay("hello");
258: } catch (Exception ex) {
259: fail("There should not throw the exception" + ex);
260: }
261:
262: }
263:
264: @Test
265: public void testOneWayQueueConnection() throws Exception {
266: QName serviceName = getServiceName(new QName(
267: "http://cxf.apache.org/hello_world_jms",
268: "HelloWorldOneWayQueueService"));
269: QName portName = getPortName(new QName(
270: "http://cxf.apache.org/hello_world_jms",
271: "HelloWorldOneWayQueuePort"));
272: URL wsdl = getClass().getResource("/wsdl/jms_test.wsdl");
273: assertNotNull(wsdl);
274:
275: HelloWorldOneWayQueueService service = new HelloWorldOneWayQueueService(
276: wsdl, serviceName);
277: assertNotNull(service);
278:
279: try {
280: HelloWorldOneWayPort greeter = service.getPort(portName,
281: HelloWorldOneWayPort.class);
282: for (int idx = 0; idx < 5; idx++) {
283: greeter.greetMeOneWay("JMS:Queue:Milestone-" + idx);
284: }
285: //Give some time to complete one-way calls.
286: Thread.sleep(100L);
287: } catch (UndeclaredThrowableException ex) {
288: throw (Exception) ex.getCause();
289: }
290: }
291:
292: @Test
293: public void testContextPropogation() throws Exception {
294: final String testReturnPropertyName = "Test_Prop";
295: final String testIgnoredPropertyName = "Test_Prop_No_Return";
296: QName serviceName = getServiceName(new QName(
297: "http://cxf.apache.org/hello_world_jms",
298: "HelloWorldService"));
299: QName portName = getPortName(new QName(
300: "http://cxf.apache.org/hello_world_jms",
301: "HelloWorldPort"));
302: URL wsdl = getClass().getResource("/wsdl/jms_test.wsdl");
303: assertNotNull(wsdl);
304:
305: HelloWorldService service = new HelloWorldService(wsdl,
306: serviceName);
307: assertNotNull(service);
308:
309: try {
310: HelloWorldPortType greeter = service.getPort(portName,
311: HelloWorldPortType.class);
312: InvocationHandler handler = Proxy
313: .getInvocationHandler(greeter);
314: BindingProvider bp = null;
315:
316: if (handler instanceof BindingProvider) {
317: bp = (BindingProvider) handler;
318: //System.out.println(bp.toString());
319: Map<String, Object> requestContext = bp
320: .getRequestContext();
321: JMSMessageHeadersType requestHeader = new JMSMessageHeadersType();
322: requestHeader
323: .setJMSCorrelationID("JMS_SAMPLE_CORRELATION_ID");
324: requestHeader.setJMSExpiration(3600000L);
325: JMSPropertyType propType = new JMSPropertyType();
326: propType.setName(testReturnPropertyName);
327: propType.setValue("mustReturn");
328: requestHeader.getProperty().add(propType);
329: propType = new JMSPropertyType();
330: propType.setName(testIgnoredPropertyName);
331: propType.setValue("mustNotReturn");
332: requestContext.put(
333: JMSConstants.JMS_CLIENT_REQUEST_HEADERS,
334: requestHeader);
335: }
336:
337: String greeting = greeter.greetMe("Milestone-");
338: assertNotNull("no response received from service", greeting);
339:
340: assertEquals("Hello Milestone-", greeting);
341:
342: if (bp != null) {
343: Map<String, Object> responseContext = bp
344: .getResponseContext();
345: JMSMessageHeadersType responseHdr = (JMSMessageHeadersType) responseContext
346: .get(JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
347: if (responseHdr == null) {
348: fail("response Header should not be null");
349: }
350:
351: assertTrue("CORRELATION ID should match :",
352: "JMS_SAMPLE_CORRELATION_ID".equals(responseHdr
353: .getJMSCorrelationID()));
354: assertTrue(
355: "response Headers must conain the app property set in request context.",
356: responseHdr.getProperty() != null);
357: assertEquals(
358: "response Headers must match the app property set in request context.",
359: testReturnPropertyName, responseHdr
360: .getProperty().iterator().next()
361: .getName());
362: }
363: } catch (UndeclaredThrowableException ex) {
364: throw (Exception) ex.getCause();
365: }
366: }
367:
368: }
|