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:
020: /**
021: *
022: */package org.apache.axis2.jaxws.sample;
023:
024: import java.net.UnknownHostException;
025: import java.util.concurrent.ExecutionException;
026: import java.util.concurrent.Future;
027:
028: import javax.xml.ws.AsyncHandler;
029: import javax.xml.ws.BindingProvider;
030: import javax.xml.ws.Response;
031: import javax.xml.ws.WebServiceException;
032: import javax.xml.ws.soap.SOAPFaultException;
033:
034: import junit.framework.TestCase;
035:
036: import org.apache.axis2.AxisFault;
037: import org.apache.axis2.context.MessageContext;
038: import org.apache.axis2.jaxws.sample.faults.FaultyWebServiceFault_Exception;
039: import org.apache.axis2.jaxws.sample.faults.FaultyWebServicePortType;
040: import org.apache.axis2.jaxws.sample.faults.FaultyWebServiceService;
041: import org.apache.axis2.jaxws.sample.wrap.sei.DocLitWrap;
042: import org.apache.axis2.jaxws.sample.wrap.sei.DocLitWrapService;
043: import org.apache.axis2.jaxws.util.Constants;
044: import org.apache.axis2.jaxws.TestLogger;
045: import org.apache.axis2.util.MessageContextBuilder;
046: import org.apache.axis2.util.ThreadContextMigrator;
047: import org.apache.axis2.util.ThreadContextMigratorUtil;
048: import org.test.faults.FaultyWebServiceResponse;
049:
050: public class FaultyWebServiceTests extends TestCase {
051: String axisEndpoint = "http://localhost:8080/axis2/services/FaultyWebServiceService";
052:
053: public void testFaultyWebService() {
054: FaultyWebServiceFault_Exception exception = null;
055: try {
056: TestLogger.logger
057: .debug("----------------------------------");
058: TestLogger.logger.debug("test: " + getName());
059: FaultyWebServiceService service = new FaultyWebServiceService();
060: FaultyWebServicePortType proxy = service
061: .getFaultyWebServicePort();
062: BindingProvider p = (BindingProvider) proxy;
063: p.getRequestContext().put(
064: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
065: axisEndpoint);
066:
067: // the invoke will throw an exception, if the test is performed right
068: int total = proxy.faultyWebService(10);
069:
070: } catch (FaultyWebServiceFault_Exception e) {
071: exception = e;
072: } catch (Exception e) {
073: e.printStackTrace();
074: fail(e.toString());
075: }
076:
077: TestLogger.logger.debug("----------------------------------");
078:
079: assertNotNull(exception);
080: assertEquals("custom exception", exception.getMessage());
081: assertNotNull(exception.getFaultInfo());
082: assertEquals("bean custom fault info", exception.getFaultInfo()
083: .getFaultInfo());
084: assertEquals("bean custom message", exception.getFaultInfo()
085: .getMessage());
086:
087: }
088:
089: public void testFaultyWebService_badEndpoint() {
090:
091: String host = "this.is.a.bad.endpoint.terrible.in.fact";
092: String badEndpoint = "http://" + host;
093:
094: WebServiceException exception = null;
095:
096: try {
097: TestLogger.logger
098: .debug("----------------------------------");
099: TestLogger.logger.debug("test: " + getName());
100: FaultyWebServiceService service = new FaultyWebServiceService();
101: FaultyWebServicePortType proxy = service
102: .getFaultyWebServicePort();
103: BindingProvider p = (BindingProvider) proxy;
104: p.getRequestContext().put(
105: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
106: badEndpoint);
107:
108: // the invoke will throw an exception, if the test is performed right
109: int total = proxy.faultyWebService(10);
110:
111: } catch (FaultyWebServiceFault_Exception e) {
112: // shouldn't get this exception
113: fail(e.toString());
114: } catch (WebServiceException e) {
115: exception = e;
116: } catch (Exception e) {
117: fail("This testcase should only produce a WebServiceException. We got: "
118: + e.toString());
119: }
120:
121: TestLogger.logger.debug("----------------------------------");
122:
123: assertNotNull(exception);
124: assertTrue(exception.getCause() instanceof UnknownHostException);
125: assertEquals(exception.getCause().getMessage(), host);
126:
127: }
128:
129: // TODO should also have an invoke oneway bad endpoint test to make sure
130: // we get an exception as indicated in JAXWS 6.4.2.
131:
132: public void testFaultyWebService_badEndpoint_oneWay() {
133:
134: String host = "this.is.a.bad.endpoint.terrible.in.fact";
135: String badEndpoint = "http://" + host;
136:
137: WebServiceException exception = null;
138:
139: TestLogger.logger.debug("------------------------------");
140: TestLogger.logger.debug("Test : " + getName());
141: try {
142:
143: DocLitWrapService service = new DocLitWrapService();
144: DocLitWrap proxy = service.getDocLitWrapPort();
145: BindingProvider p = (BindingProvider) proxy;
146: p.getRequestContext().put(
147: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
148: badEndpoint);
149: proxy.oneWayVoid();
150:
151: } catch (WebServiceException e) {
152: exception = e;
153: } catch (Exception e) {
154: fail("This testcase should only produce a WebServiceException. We got: "
155: + e.toString());
156: }
157:
158: TestLogger.logger.debug("----------------------------------");
159:
160: assertNotNull(exception);
161: assertTrue(exception.getCause() instanceof UnknownHostException);
162: assertEquals(exception.getCause().getMessage(), host);
163:
164: }
165:
166: public void testFaultyWebService_badEndpoint_AsyncCallback()
167: throws Exception {
168:
169: String host = "this.is.a.bad.endpoint.terrible.in.fact";
170: String badEndpoint = "http://" + host;
171:
172: TestLogger.logger.debug("------------------------------");
173: TestLogger.logger.debug("Test : " + getName());
174:
175: FaultyWebServiceService service = new FaultyWebServiceService();
176: FaultyWebServicePortType proxy = service
177: .getFaultyWebServicePort();
178: BindingProvider p = (BindingProvider) proxy;
179: p.getRequestContext().put(
180: BindingProvider.ENDPOINT_ADDRESS_PROPERTY, badEndpoint);
181:
182: FaultyAsyncHandler callback = new FaultyAsyncHandler();
183: Future<?> future = proxy.faultyWebServiceAsync(1, callback);
184:
185: while (!future.isDone()) {
186: Thread.sleep(1000);
187: TestLogger.logger.debug("Async invocation incomplete");
188: }
189:
190: Exception e = callback.getException();
191:
192: // Section 4.3.3 states that the top level Exception should be
193: // an ExecutionException, with a WebServiceException underneath.
194: assertNotNull("The exception was null.", e);
195: assertTrue(
196: "The thrown exception should be an ExecutionException.",
197: e.getClass().equals(ExecutionException.class));
198: assertTrue(
199: "The expected fault type under the ExecutionException should be a "
200: + "SOAPFaultException. Found type: "
201: + e.getCause().getClass(), e.getCause()
202: .getClass().isAssignableFrom(
203: SOAPFaultException.class));
204:
205: }
206:
207: public void testFaultyWebService_badEndpoint_AsyncPolling()
208: throws Exception {
209:
210: String host = "this.is.a.bad.endpoint.terrible.in.fact";
211: String badEndpoint = "http://" + host;
212:
213: TestLogger.logger.debug("------------------------------");
214: TestLogger.logger.debug("Test : " + getName());
215:
216: FaultyWebServiceService service = new FaultyWebServiceService();
217: FaultyWebServicePortType proxy = service
218: .getFaultyWebServicePort();
219: BindingProvider p = (BindingProvider) proxy;
220: p.getRequestContext().put(
221: BindingProvider.ENDPOINT_ADDRESS_PROPERTY, badEndpoint);
222:
223: Future<?> future = proxy.faultyWebServiceAsync(1);
224: while (!future.isDone()) {
225: Thread.sleep(1000);
226: TestLogger.logger.debug("Async invocation incomplete");
227: }
228:
229: Exception e = null;
230: try {
231: Object obj = future.get();
232: } catch (Exception ex) {
233: e = ex;
234: }
235:
236: // Section 4.3.3 states that the top level Exception should be
237: // an ExecutionException, with a WebServiceException underneath.
238: assertNotNull("The exception was null.", e);
239: assertTrue(
240: "The thrown exception should be an ExecutionException.",
241: e.getClass().equals(ExecutionException.class));
242: assertTrue(
243: "The expected fault type under the ExecutionException should be a "
244: + "SOAPFaultException. Found type: "
245: + e.getCause().getClass(), e.getCause()
246: .getClass().isAssignableFrom(
247: SOAPFaultException.class));
248:
249: }
250:
251: /*
252: * Tests fault processing for user defined fault types
253: */
254: public void testCustomFault_AsyncCallback() throws Exception {
255: TestLogger.logger.debug("------------------------------");
256: TestLogger.logger.debug("test: " + getName());
257:
258: FaultyWebServiceService service = new FaultyWebServiceService();
259: FaultyWebServicePortType proxy = service
260: .getFaultyWebServicePort();
261: BindingProvider p = (BindingProvider) proxy;
262: p.getRequestContext()
263: .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
264: axisEndpoint);
265:
266: FaultyAsyncHandler callback = new FaultyAsyncHandler();
267: Future<?> future = proxy.faultyWebServiceAsync(1, callback);
268:
269: while (!future.isDone()) {
270: Thread.sleep(1000);
271: TestLogger.logger.debug("Async invocation incomplete");
272: }
273:
274: Exception e = callback.getException();
275: e.printStackTrace();
276:
277: // Section 4.3.3 states that the top level Exception should be
278: // an ExecutionException, with a WebServiceException underneath.
279: assertNotNull("The exception was null.", e);
280: assertTrue(
281: "The thrown exception should be an ExecutionException.",
282: e.getClass().equals(ExecutionException.class));
283: assertTrue(
284: "The expected fault type under the ExecutionException should be a "
285: + "FaultyWebServiceFault_Exception. Found type: "
286: + e.getCause().getClass(), e.getCause()
287: .getClass().isAssignableFrom(
288: FaultyWebServiceFault_Exception.class));
289: }
290:
291: /*
292: * A callback implementation that can be used to collect the exceptions
293: */
294: class FaultyAsyncHandler implements
295: AsyncHandler<FaultyWebServiceResponse> {
296:
297: Exception exception;
298:
299: public void handleResponse(
300: Response<FaultyWebServiceResponse> response) {
301: try {
302: TestLogger.logger
303: .debug("FaultyAsyncHandler.handleResponse() was called");
304: FaultyWebServiceResponse r = response.get();
305: TestLogger.logger
306: .debug("No exception was thrown from Response.get()");
307: } catch (Exception e) {
308: TestLogger.logger.debug("An exception was thrown: "
309: + e.getClass());
310: exception = e;
311: }
312: }
313:
314: public Exception getException() {
315: return exception;
316: }
317: }
318:
319: }
|