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 javax.xml.namespace.QName;
025: import javax.xml.soap.DetailEntry;
026: import javax.xml.soap.SOAPFault;
027: import javax.xml.ws.BindingProvider;
028: import javax.xml.ws.soap.SOAPFaultException;
029:
030: import junit.framework.TestCase;
031: import org.apache.axis2.jaxws.sample.faultsservice.BaseFault_Exception;
032: import org.apache.axis2.jaxws.sample.faultsservice.ComplexFault_Exception;
033: import org.apache.axis2.jaxws.sample.faultsservice.DerivedFault1_Exception;
034: import org.apache.axis2.jaxws.sample.faultsservice.DerivedFault2_Exception;
035: import org.apache.axis2.jaxws.sample.faultsservice.FaultsService;
036: import org.apache.axis2.jaxws.sample.faultsservice.FaultsServicePortType;
037: import org.apache.axis2.jaxws.sample.faultsservice.InvalidTickerFault_Exception;
038: import org.apache.axis2.jaxws.sample.faultsservice.SimpleFault;
039: import org.apache.axis2.jaxws.TestLogger;
040: import org.test.polymorphicfaults.BaseFault;
041: import org.test.polymorphicfaults.ComplexFault;
042: import org.test.polymorphicfaults.DerivedFault1;
043: import org.test.polymorphicfaults.DerivedFault2;
044:
045: public class FaultsServiceTests extends TestCase {
046:
047: String axisEndpoint = "http://localhost:8080/axis2/services/FaultsService";
048:
049: /**
050: * Utility method to get the proxy
051: * @return proxy
052: */
053: private FaultsServicePortType getProxy() {
054: FaultsService service = new FaultsService();
055: FaultsServicePortType proxy = service.getFaultsPort();
056: BindingProvider p = (BindingProvider) proxy;
057: p.getRequestContext()
058: .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
059: axisEndpoint);
060: return proxy;
061: }
062:
063: /**
064: * Tests that that BaseFault is thrown
065: */
066: public void testFaultsService0() {
067: Exception exception = null;
068: try {
069: FaultsServicePortType proxy = getProxy();
070: // the invoke will throw an exception, if the test is performed right
071: int total = proxy.throwFault(2, "BaseFault", 2);
072:
073: } catch (BaseFault_Exception e) {
074: exception = e;
075: } catch (ComplexFault_Exception e) {
076: fail("Should not get ComplexFault_Exception in this testcase");
077: }
078:
079: TestLogger.logger.debug("----------------------------------");
080:
081: assertNotNull(exception);
082: Object fault = ((BaseFault_Exception) exception).getFaultInfo();
083: assertTrue(fault.getClass() == BaseFault.class);
084: BaseFault bf = (BaseFault) fault;
085: assertTrue(bf.getA() == 2);
086:
087: }
088:
089: /**
090: * Tests that that BaseFault (DerivedFault1) is thrown
091: */
092: public void testFaultsService1() {
093: Exception exception = null;
094: try {
095: FaultsServicePortType proxy = getProxy();
096: // the invoke will throw an exception, if the test is performed right
097: int total = proxy.throwFault(2, "DerivedFault1", 2);
098:
099: } catch (BaseFault_Exception e) {
100: exception = e;
101: } catch (ComplexFault_Exception e) {
102: fail("Should not get ComplexFault_Exception in this testcase");
103: }
104:
105: TestLogger.logger.debug("----------------------------------");
106:
107: assertNotNull(exception);
108: Object fault = ((BaseFault_Exception) exception).getFaultInfo();
109: assertTrue(fault.getClass() == DerivedFault1.class);
110: DerivedFault1 df = (DerivedFault1) fault;
111: assertTrue(df.getA() == 2);
112: assertTrue(df.getB().equals("DerivedFault1"));
113:
114: }
115:
116: /**
117: * Tests that that BaseFault (DerivedFault1) is thrown
118: */
119: public void testFaultsService2() {
120: Exception exception = null;
121: try {
122: FaultsServicePortType proxy = getProxy();
123: // the invoke will throw an exception, if the test is performed right
124: int total = proxy.throwFault(2, "DerivedFault2", 2);
125:
126: } catch (BaseFault_Exception e) {
127: exception = e;
128: } catch (ComplexFault_Exception e) {
129: fail("Should not get ComplexFault_Exception in this testcase");
130: }
131:
132: TestLogger.logger.debug("----------------------------------");
133:
134: assertNotNull(exception);
135: Object fault = ((BaseFault_Exception) exception).getFaultInfo();
136: assertTrue(fault.getClass() == DerivedFault2.class);
137: DerivedFault2 df = (DerivedFault2) fault;
138: assertTrue(df.getA() == 2);
139: assertTrue(df.getB().equals("DerivedFault2"));
140: assertTrue(df.getC() == 2);
141: }
142:
143: /**
144: * Tests that that ComplxFaultFault is thrown
145: */
146: public void testFaultsService3() {
147: Exception exception = null;
148: try {
149: FaultsServicePortType proxy = getProxy();
150:
151: // the invoke will throw an exception, if the test is performed right
152: int total = proxy.throwFault(2, "Complex", 2); // "Complex" will cause service to throw ComplexFault_Exception
153:
154: } catch (BaseFault_Exception e) {
155: fail("Should not get BaseFault_Exception in this testcase");
156: } catch (ComplexFault_Exception e) {
157: exception = e;
158: }
159:
160: TestLogger.logger.debug("----------------------------------");
161:
162: assertNotNull(exception);
163: Object fault = ((ComplexFault_Exception) exception)
164: .getFaultInfo();
165: assertTrue(fault.getClass() == ComplexFault.class);
166: ComplexFault cf = (ComplexFault) fault;
167: assertTrue(cf.getA() == 2);
168: assertTrue(cf.getB().equals("Complex"));
169: assertTrue(cf.getC() == 2);
170: assertTrue(cf.getD() == 5);
171: }
172:
173: /**
174: * Tests that throwing of SimpleFault
175: */
176: public void testFaultsService4() {
177: Exception exception = null;
178: try {
179: FaultsServicePortType proxy = getProxy();
180:
181: // the invoke will throw an exception, if the test is performed right
182: float total = proxy.getQuote("SMPL");
183: fail("Expected SimpleFault but no fault was thrown ");
184: } catch (SimpleFault e) {
185: SimpleFault fault = (SimpleFault) e;
186:
187: int faultInfo = fault.getFaultInfo();
188: assertTrue(faultInfo == 100);
189: } catch (Exception e) {
190: fail("Wrong exception thrown. Expected SimpleFault but received "
191: + e.getClass());
192: }
193: }
194:
195: /**
196: * Test throwing legacy fault
197: * Disabled while I fix this test
198: */
199: public void testFaultsService5() {
200: Exception exception = null;
201: try {
202: FaultsServicePortType proxy = getProxy();
203:
204: // the invoke will throw an exception, if the test is performed right
205: float total = proxy.getQuote("LEGC");
206: fail("Expected InvalidTickerFault_Exception but no fault was thrown ");
207: } catch (InvalidTickerFault_Exception e) {
208: InvalidTickerFault_Exception fault = (InvalidTickerFault_Exception) e;
209:
210: assertTrue(fault.getLegacyData1().equals("LEGC"));
211: assertTrue(fault.getLegacyData2() == 123);
212: } catch (Exception e) {
213: fail("Wrong exception thrown. Expected InvalidTickerFault_Exception but received "
214: + e.getClass());
215: }
216: }
217:
218: /**
219: * Tests that throwing of BaseFault_Exception
220: */
221: public void testFaultsService6() {
222: Exception exception = null;
223: try {
224: FaultsServicePortType proxy = getProxy();
225:
226: // the invoke will throw an exception, if the test is performed right
227: float total = proxy.getQuote("BASE");
228: fail("Expected BaseFault_Exception but no fault was thrown ");
229: } catch (BaseFault_Exception e) {
230: BaseFault_Exception fault = (BaseFault_Exception) e;
231:
232: BaseFault faultInfo = fault.getFaultInfo();
233: assertTrue(faultInfo != null);
234: assertTrue(faultInfo.getA() == 400);
235: } catch (Exception e) {
236: fail("Wrong exception thrown. Expected BaseFault_Exception but received "
237: + e.getClass());
238: }
239: }
240:
241: /**
242: * Tests that throwing of DerivedFault1_Exception
243: */
244: public void testFaultsService7() {
245: Exception exception = null;
246: try {
247: FaultsServicePortType proxy = getProxy();
248:
249: // the invoke will throw an exception, if the test is performed right
250: float total = proxy.getQuote("DF1");
251: fail("Expected DerivedFault1_Exception but no fault was thrown");
252: } catch (DerivedFault1_Exception e) {
253: DerivedFault1_Exception fault = (DerivedFault1_Exception) e;
254:
255: DerivedFault1 faultInfo = fault.getFaultInfo();
256: assertTrue(faultInfo != null);
257: assertTrue(faultInfo.getA() == 100);
258: assertTrue(faultInfo.getB().equals("DF1"));
259: } catch (Exception e) {
260: fail("Wrong exception thrown. Expected DerivedFault1_Exception but received "
261: + e.getClass());
262: }
263: }
264:
265: /**
266: * Tests that throwing of DerivedFault1_Exception
267: */
268: public void testFaultsService8() {
269: Exception exception = null;
270: try {
271: FaultsServicePortType proxy = getProxy();
272:
273: // the invoke will throw an exception, if the test is performed right
274: float total = proxy.getQuote("DF2");
275: fail("Expected DerivedFault2_Exception but no fault was thrown ");
276: } catch (DerivedFault2_Exception e) {
277: DerivedFault2_Exception fault = (DerivedFault2_Exception) e;
278:
279: DerivedFault2 faultInfo = fault.getFaultInfo();
280: assertTrue(faultInfo != null);
281: assertTrue(faultInfo.getA() == 200);
282: assertTrue(faultInfo.getB().equals("DF2"));
283: assertTrue(faultInfo.getC() == 80.0F);
284: } catch (Exception e) {
285: fail("Wrong exception thrown. Expected DerivedFault1_Exception but received "
286: + e.getClass());
287: }
288: }
289:
290: /**
291: * Tests that that SOAPFaultException is thrown
292: */
293: public void testFaultsService9a() {
294: Exception exception = null;
295: try {
296: FaultsServicePortType proxy = getProxy();
297:
298: // the invoke will throw an exception, if the test is performed right
299: int total = proxy.throwFault(2, "SOAPFaultException", 2); // "SOAPFaultException" will cause service to throw SOAPFaultException
300:
301: } catch (SOAPFaultException e) {
302: // Okay
303: exception = e;
304: } catch (Exception e) {
305: fail("Did not get a SOAPFaultException");
306: }
307:
308: TestLogger.logger.debug("----------------------------------");
309:
310: assertNotNull(exception);
311: SOAPFaultException sfe = (SOAPFaultException) exception;
312: SOAPFault soapFault = sfe.getFault();
313: assertTrue(soapFault != null);
314: assertTrue(soapFault.getFaultString().equals("hello world"));
315: assertTrue(soapFault.getFaultActor().equals("actor"));
316: assertTrue(soapFault.getDetail() == null);
317: }
318:
319: /**
320: * Tests that that SOAPFaultException is thrown
321: */
322: public void testFaultsService9b() {
323: Exception exception = null;
324: try {
325: FaultsServicePortType proxy = getProxy();
326:
327: // the invoke will throw an exception, if the test is performed right
328: int total = proxy.throwFault(2, "SOAPFaultException2", 2); // "SOAPFaultException" will cause service to throw SOAPFaultException
329:
330: } catch (SOAPFaultException e) {
331: // Okay
332: exception = e;
333: } catch (Exception e) {
334: fail("Did not get a SOAPFaultException");
335: }
336:
337: TestLogger.logger.debug("----------------------------------");
338:
339: assertNotNull(exception);
340: SOAPFaultException sfe = (SOAPFaultException) exception;
341: SOAPFault soapFault = sfe.getFault();
342: assertTrue(soapFault != null);
343: assertTrue(soapFault.getFaultString().equals("hello world2"));
344: assertTrue(soapFault.getFaultActor().equals("actor2"));
345: assertTrue(soapFault.getDetail() != null);
346: DetailEntry de = (DetailEntry) soapFault.getDetail()
347: .getDetailEntries().next();
348: assertTrue(de != null);
349: assertTrue(de.getNamespaceURI().equals("urn://sample"));
350: assertTrue(de.getLocalName().equals("detailEntry"));
351: assertTrue(de.getValue().equals("Texas"));
352: }
353:
354: /**
355: * Tests that that SOAPFaultException (NPE) is thrown
356: */
357: public void testFaultsService10() {
358: Exception exception = null;
359: try {
360: FaultsServicePortType proxy = getProxy();
361:
362: // the invoke will throw an exception, if the test is performed right
363: int total = proxy.throwFault(2, "NPE", 2); // "NPE" will cause service to throw NPE System Exception
364:
365: } catch (SOAPFaultException e) {
366: // Okay
367: exception = e;
368: } catch (Exception e) {
369: fail("Did not get a SOAPFaultException");
370: }
371:
372: TestLogger.logger.debug("----------------------------------");
373:
374: assertNotNull(exception);
375: SOAPFaultException sfe = (SOAPFaultException) exception;
376: SOAPFault soapFault = sfe.getFault();
377: assertTrue(soapFault != null);
378: assertTrue(soapFault.getFaultString().equals(
379: "java.lang.NullPointerException"));
380: }
381:
382: /**
383: * Tests that that SOAPFaultException (NPE) is thrown
384: */
385: public void testFaultsService10a() {
386: Exception exception = null;
387: try {
388: FaultsServicePortType proxy = getProxy();
389:
390: // the invoke will throw an exception, if the test is performed right
391: int total = proxy.throwFault(2, "NPE2", 2); // "NPE" will cause service to throw NPE System Exception
392:
393: } catch (SOAPFaultException e) {
394: // Okay
395: exception = e;
396: } catch (Exception e) {
397: fail("Did not get a SOAPFaultException");
398: }
399:
400: TestLogger.logger.debug("----------------------------------");
401:
402: assertNotNull(exception);
403: SOAPFaultException sfe = (SOAPFaultException) exception;
404: SOAPFault soapFault = sfe.getFault();
405: assertTrue(soapFault != null);
406: assertTrue(soapFault.getFaultString().equals(
407: "Null Pointer Exception occurred"));
408: }
409:
410: /**
411: * Tests that that SOAPFaultException (for WebServiceException) is thrown
412: */
413: public void testFaultsService11() {
414: Exception exception = null;
415: try {
416: FaultsServicePortType proxy = getProxy();
417:
418: // the invoke will throw an exception, if the test is performed right
419: int total = proxy.throwFault(2, "WSE", 2); // "WSE" will cause service to throw WebServiceException System Exception
420:
421: } catch (SOAPFaultException e) {
422: // Okay...on the client a SOAPFaultException should be thrown
423: exception = e;
424: } catch (Exception e) {
425: fail("Did not get a SOAPFaultException");
426: }
427:
428: TestLogger.logger.debug("----------------------------------");
429:
430: assertNotNull(exception);
431: SOAPFaultException sfe = (SOAPFaultException) exception;
432: SOAPFault soapFault = sfe.getFault();
433: assertTrue(soapFault != null);
434: assertTrue(soapFault.getFaultString().equals(
435: "This is a WebServiceException"));
436: }
437:
438: /**
439: * Tests Resource injection
440: */
441: public void testResourceInjection() throws Exception {
442: FaultsServicePortType proxy = getProxy();
443:
444: float total = proxy.getQuote("INJECTION");
445:
446: // If resource injection occurred properly, then the a value of 1234567 is expected
447: assertTrue("Resource Injection Failed", total == 1234567);
448: }
449: }
|