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: */
019: package org.apache.axis2.jaxws.dispatch;
020:
021: import java.util.concurrent.Future;
022:
023: import javax.xml.bind.JAXBContext;
024: import javax.xml.bind.JAXBElement;
025: import javax.xml.ws.Dispatch;
026: import javax.xml.ws.Service;
027:
028: import org.xmlsoap.schemas.soap.envelope.Body;
029: import org.xmlsoap.schemas.soap.envelope.Envelope;
030: import org.apache.axis2.jaxws.TestLogger;
031:
032: import junit.framework.TestCase;
033: import test.EchoString;
034: import test.EchoStringResponse;
035: import test.ObjectFactory;
036:
037: public class JAXBDispatch extends TestCase {
038:
039: private Dispatch<Object> dispatchPayload;
040: private Dispatch<Object> dispatchMessage;
041: private JAXBContext jbc;
042:
043: public JAXBDispatch(String name) {
044: super (name);
045: }
046:
047: public void setUp() throws Exception {
048: //Create the Service object
049: Service svc = Service
050: .create(DispatchTestConstants.QNAME_SERVICE);
051: svc.addPort(DispatchTestConstants.QNAME_PORT, null,
052: DispatchTestConstants.URL);
053:
054: //Create the JAX-B Dispatch object to recognize the test and soap packages
055: jbc = JAXBContext
056: .newInstance("test:org.xmlsoap.schemas.soap.envelope");
057:
058: // Create Payload and Message Dispatch
059: dispatchPayload = svc.createDispatch(
060: DispatchTestConstants.QNAME_PORT, jbc,
061: Service.Mode.PAYLOAD);
062: dispatchMessage = svc.createDispatch(
063: DispatchTestConstants.QNAME_PORT, jbc,
064: Service.Mode.MESSAGE);
065: }
066:
067: public void testSyncPayload() throws Exception {
068: TestLogger.logger
069: .debug("---------------------------------------");
070: TestLogger.logger.debug("test: " + getName());
071:
072: // Create the input param
073: ObjectFactory factory = new ObjectFactory();
074: EchoString request = factory.createEchoString();
075: request.setInput("SYNC JAXB PAYLOAD TEST");
076:
077: // Invoke the Dispatch<Object>
078: TestLogger.logger
079: .debug(">> Invoking sync Dispatch with JAX-B Parameter");
080: EchoStringResponse response = (EchoStringResponse) dispatchPayload
081: .invoke(request);
082:
083: assertNotNull(response);
084:
085: TestLogger.logger.debug(">> Response content: "
086: + response.getEchoStringReturn());
087:
088: assertTrue("[ERROR] - Response object was null",
089: response != null);
090: assertTrue("[ERROR] - No content in response object", response
091: .getEchoStringReturn() != null);
092: assertTrue("[ERROR] - Zero length content in response",
093: response.getEchoStringReturn().length() > 0);
094: }
095:
096: public void testAysncPayload() throws Exception {
097: TestLogger.logger
098: .debug("---------------------------------------");
099: TestLogger.logger.debug("test: " + getName());
100:
101: // Create the input param
102: ObjectFactory factory = new ObjectFactory();
103: EchoString request = factory.createEchoString();
104: request.setInput("ASYNC(CALLBACK) JAXB PAYLOAD TEST");
105:
106: // Create the callback for async responses
107: JAXBCallbackHandler<Object> callback = new JAXBCallbackHandler<Object>();
108:
109: // Invoke the Dispatch<Object> asynchronously
110: TestLogger.logger
111: .debug(">> Invoking async(callback) Dispatch with JAX-B Parameter");
112: Future<?> monitor = dispatchPayload.invokeAsync(request,
113: callback);
114:
115: while (!monitor.isDone()) {
116: TestLogger.logger
117: .debug(">> Async invocation still not complete");
118: Thread.sleep(1000);
119: }
120:
121: EchoStringResponse response = (EchoStringResponse) callback
122: .getData();
123: assertNotNull(response);
124:
125: TestLogger.logger.debug(">> Response content: "
126: + response.getEchoStringReturn());
127:
128: assertTrue("[ERROR] - Response object was null",
129: response != null);
130: assertTrue("[ERROR] - No content in response object", response
131: .getEchoStringReturn() != null);
132: assertTrue("[ERROR] - Zero length content in response",
133: response.getEchoStringReturn().length() > 0);
134:
135: }
136:
137: public void testOneWayPayload() throws Exception {
138: TestLogger.logger
139: .debug("---------------------------------------");
140: TestLogger.logger.debug("test: " + getName());
141:
142: // Create the input param
143: ObjectFactory factory = new ObjectFactory();
144: EchoString request = factory.createEchoString();
145: request.setInput("ONE-WAY JAXB PAYLOAD TEST");
146:
147: // Invoke the Dispatch<Object> one-way
148: TestLogger.logger
149: .debug(">> Invoking one-way Dispatch with JAX-B Parameter");
150: dispatchPayload.invokeOneWay(request);
151: }
152:
153: public void testSyncMessage() throws Exception {
154: TestLogger.logger
155: .debug("---------------------------------------");
156: TestLogger.logger.debug("test: " + getName());
157:
158: // Create the input param
159: ObjectFactory factory = new ObjectFactory();
160: EchoString echoString = factory.createEchoString();
161: echoString.setInput("SYNC JAXB MESSAGETEST");
162:
163: JAXBElement<Envelope> request = createJAXBEnvelope();
164: request.getValue().getBody().getAny().add(echoString);
165:
166: jbc.createMarshaller().marshal(request, System.out);
167:
168: // Invoke the Dispatch<Object>
169: TestLogger.logger
170: .debug(">> Invoking sync Dispatch with JAX-B Parameter");
171: JAXBElement<Envelope> jaxbResponse = (JAXBElement<Envelope>) dispatchMessage
172: .invoke(request);
173:
174: assertNotNull(jaxbResponse);
175: Envelope response = jaxbResponse.getValue();
176: assertNotNull(response);
177: assertNotNull(response.getBody());
178: EchoStringResponse echoStringResponse = (EchoStringResponse) response
179: .getBody().getAny().get(0);
180:
181: TestLogger.logger.debug(">> Response content: "
182: + echoStringResponse.getEchoStringReturn());
183: assertTrue("[ERROR] - No content in response object",
184: echoStringResponse.getEchoStringReturn() != null);
185: assertTrue("[ERROR] - Zero length content in response",
186: echoStringResponse.getEchoStringReturn().length() > 0);
187: }
188:
189: public void testAysncMessage() throws Exception {
190: TestLogger.logger
191: .debug("---------------------------------------");
192: TestLogger.logger.debug("test: " + getName());
193:
194: // Create the input param
195: ObjectFactory factory = new ObjectFactory();
196: EchoString echoString = factory.createEchoString();
197: echoString.setInput("ASYNC(CALLBACK) JAXB MESSAGE TEST");
198:
199: JAXBElement<Envelope> request = createJAXBEnvelope();
200: request.getValue().getBody().getAny().add(echoString);
201:
202: // Create the callback for async responses
203: JAXBCallbackHandler<Object> callback = new JAXBCallbackHandler<Object>();
204:
205: // Invoke the Dispatch<Object> asynchronously
206: TestLogger.logger
207: .debug(">> Invoking async(callback) Dispatch with JAX-B Parameter");
208: Future<?> monitor = dispatchMessage.invokeAsync(request,
209: callback);
210:
211: while (!monitor.isDone()) {
212: TestLogger.logger
213: .debug(">> Async invocation still not complete");
214: Thread.sleep(1000);
215: }
216:
217: JAXBElement<Envelope> jaxbResponse = (JAXBElement<Envelope>) callback
218: .getData();
219:
220: assertNotNull(jaxbResponse);
221: Envelope response = jaxbResponse.getValue();
222:
223: assertNotNull(response);
224: assertNotNull(response.getBody());
225: EchoStringResponse echoStringResponse = (EchoStringResponse) response
226: .getBody().getAny().get(0);
227:
228: TestLogger.logger.debug(">> Response content: "
229: + echoStringResponse.getEchoStringReturn());
230: assertTrue("[ERROR] - No content in response object",
231: echoStringResponse.getEchoStringReturn() != null);
232: assertTrue("[ERROR] - Zero length content in response",
233: echoStringResponse.getEchoStringReturn().length() > 0);
234:
235: }
236:
237: public void testOneWayMessge() throws Exception {
238: TestLogger.logger
239: .debug("---------------------------------------");
240: TestLogger.logger.debug("test: " + getName());
241:
242: // Create the input param
243: ObjectFactory factory = new ObjectFactory();
244: EchoString echoString = factory.createEchoString();
245: echoString.setInput("ONE-WAY JAXB MESSAGE TEST");
246:
247: JAXBElement<Envelope> request = createJAXBEnvelope();
248: request.getValue().getBody().getAny().add(echoString);
249:
250: // Invoke the Dispatch<Object> one-way
251: TestLogger.logger
252: .debug(">> Invoking one-way Dispatch with JAX-B Parameter");
253: dispatchMessage.invokeOneWay(request);
254: }
255:
256: private JAXBElement<Envelope> createJAXBEnvelope() {
257: org.xmlsoap.schemas.soap.envelope.ObjectFactory factory = new org.xmlsoap.schemas.soap.envelope.ObjectFactory();
258: Envelope env = new Envelope();
259:
260: Body body = new Body();
261: env.setBody(body);
262:
263: JAXBElement<Envelope> jaxbEnv = factory.createEnvelope(env);
264: return jaxbEnv;
265: }
266: }
|