001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.server.endpoint;
018:
019: import java.util.Locale;
020: import javax.xml.namespace.QName;
021: import javax.xml.soap.MessageFactory;
022: import javax.xml.soap.SOAPConstants;
023: import javax.xml.soap.SOAPMessage;
024:
025: import junit.framework.TestCase;
026: import org.springframework.ws.context.DefaultMessageContext;
027: import org.springframework.ws.context.MessageContext;
028: import org.springframework.ws.soap.SoapMessage;
029: import org.springframework.ws.soap.SoapMessageFactory;
030: import org.springframework.ws.soap.SoapVersion;
031: import org.springframework.ws.soap.saaj.SaajSoapMessage;
032: import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
033: import org.springframework.ws.soap.server.endpoint.annotation.FaultCode;
034: import org.springframework.ws.soap.server.endpoint.annotation.SoapFault;
035: import org.springframework.ws.soap.soap11.Soap11Fault;
036: import org.springframework.ws.soap.soap12.Soap12Fault;
037:
038: public class SoapFaultAnnotationExceptionResolverTest extends TestCase {
039:
040: private SoapFaultAnnotationExceptionResolver resolver;
041:
042: protected void setUp() throws Exception {
043: resolver = new SoapFaultAnnotationExceptionResolver();
044: }
045:
046: public void testResolveExceptionClientSoap11() throws Exception {
047: MessageFactory saajFactory = MessageFactory
048: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
049: SoapMessageFactory factory = new SaajSoapMessageFactory(
050: saajFactory);
051: MessageContext context = new DefaultMessageContext(factory);
052:
053: boolean result = resolver.resolveException(context, null,
054: new MyClientException());
055: assertTrue("resolveException returns false", result);
056: assertTrue("Context has no response", context.hasResponse());
057: SoapMessage response = (SoapMessage) context.getResponse();
058: assertTrue("Resonse has no fault", response.getSoapBody()
059: .hasFault());
060: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
061: .getFault();
062: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
063: .getClientOrSenderFaultName(), fault.getFaultCode());
064: assertEquals("Invalid fault string on fault", "Client error",
065: fault.getFaultStringOrReason());
066: assertNull("Detail on fault", fault.getFaultDetail());
067: }
068:
069: public void testResolveExceptionSenderSoap12() throws Exception {
070: MessageFactory saajFactory = MessageFactory
071: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
072: SoapMessageFactory factory = new SaajSoapMessageFactory(
073: saajFactory);
074: MessageContext context = new DefaultMessageContext(factory);
075:
076: boolean result = resolver.resolveException(context, null,
077: new MySenderException());
078: assertTrue("resolveException returns false", result);
079: assertTrue("Context has no response", context.hasResponse());
080: SoapMessage response = (SoapMessage) context.getResponse();
081: assertTrue("Resonse has no fault", response.getSoapBody()
082: .hasFault());
083: Soap12Fault fault = (Soap12Fault) response.getSoapBody()
084: .getFault();
085: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_12
086: .getClientOrSenderFaultName(), fault.getFaultCode());
087: assertEquals("Invalid fault string on fault", "Sender error",
088: fault.getFaultReasonText(Locale.ENGLISH));
089: assertNull("Detail on fault", fault.getFaultDetail());
090: }
091:
092: public void testResolveExceptionServerSoap11() throws Exception {
093: MessageFactory saajFactory = MessageFactory
094: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
095: SoapMessageFactory factory = new SaajSoapMessageFactory(
096: saajFactory);
097: MessageContext context = new DefaultMessageContext(factory);
098:
099: boolean result = resolver.resolveException(context, null,
100: new MyServerException());
101: assertTrue("resolveException returns false", result);
102: assertTrue("Context has no response", context.hasResponse());
103: SoapMessage response = (SoapMessage) context.getResponse();
104: assertTrue("Resonse has no fault", response.getSoapBody()
105: .hasFault());
106: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
107: .getFault();
108: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
109: .getServerOrReceiverFaultName(), fault.getFaultCode());
110: assertEquals("Invalid fault string on fault", "Server error",
111: fault.getFaultStringOrReason());
112: assertNull("Detail on fault", fault.getFaultDetail());
113: }
114:
115: public void testResolveExceptionReceiverSoap12() throws Exception {
116: MessageFactory saajFactory = MessageFactory
117: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
118: SOAPMessage message = saajFactory.createMessage();
119: SoapMessageFactory factory = new SaajSoapMessageFactory(
120: saajFactory);
121: MessageContext context = new DefaultMessageContext(
122: new SaajSoapMessage(message), factory);
123:
124: boolean result = resolver.resolveException(context, null,
125: new MyReceiverException());
126: assertTrue("resolveException returns false", result);
127: assertTrue("Context has no response", context.hasResponse());
128: SoapMessage response = (SoapMessage) context.getResponse();
129: assertTrue("Resonse has no fault", response.getSoapBody()
130: .hasFault());
131: Soap12Fault fault = (Soap12Fault) response.getSoapBody()
132: .getFault();
133: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_12
134: .getServerOrReceiverFaultName(), fault.getFaultCode());
135: assertEquals("Invalid fault string on fault", "Receiver error",
136: fault.getFaultReasonText(Locale.ENGLISH));
137: assertNull("Detail on fault", fault.getFaultDetail());
138: }
139:
140: public void testResolveExceptionDefault() throws Exception {
141: SoapFaultDefinition defaultFault = new SoapFaultDefinition();
142: defaultFault.setFaultCode(SoapFaultDefinition.CLIENT);
143: defaultFault.setFaultStringOrReason("faultstring");
144: resolver.setDefaultFault(defaultFault);
145: MessageFactory saajFactory = MessageFactory
146: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
147: SoapMessageFactory factory = new SaajSoapMessageFactory(
148: saajFactory);
149: MessageContext context = new DefaultMessageContext(factory);
150:
151: boolean result = resolver.resolveException(context, null,
152: new NonAnnotatedException());
153: assertTrue("resolveException returns false", result);
154: assertTrue("Context has no response", context.hasResponse());
155: SoapMessage response = (SoapMessage) context.getResponse();
156: assertTrue("Resonse has no fault", response.getSoapBody()
157: .hasFault());
158: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
159: .getFault();
160: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
161: .getClientOrSenderFaultName(), fault.getFaultCode());
162: assertEquals("Invalid fault string on fault", "faultstring",
163: fault.getFaultStringOrReason());
164: assertNull("Detail on fault", fault.getFaultDetail());
165: }
166:
167: public void testResolveExceptionCustom() throws Exception {
168: MessageFactory saajFactory = MessageFactory
169: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
170: SoapMessageFactory factory = new SaajSoapMessageFactory(
171: saajFactory);
172: MessageContext context = new DefaultMessageContext(factory);
173:
174: boolean result = resolver.resolveException(context, null,
175: new MyCustomException());
176: assertTrue("resolveException returns false", result);
177: assertTrue("Context has no response", context.hasResponse());
178: SoapMessage response = (SoapMessage) context.getResponse();
179: assertTrue("Resonse has no fault", response.getSoapBody()
180: .hasFault());
181: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
182: .getFault();
183: assertEquals("Invalid fault code on fault", new QName(
184: "http://springframework.org/spring-ws", "Fault"), fault
185: .getFaultCode());
186: assertEquals("Invalid fault string on fault",
187: "MyCustomException thrown", fault
188: .getFaultStringOrReason());
189: assertEquals("Invalid fault locale on fault", new Locale("nl"),
190: fault.getFaultStringLocale());
191: }
192:
193: public void testResolveExceptionInheritance() throws Exception {
194: MessageFactory saajFactory = MessageFactory
195: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
196: SoapMessageFactory factory = new SaajSoapMessageFactory(
197: saajFactory);
198: MessageContext context = new DefaultMessageContext(factory);
199:
200: boolean result = resolver.resolveException(context, null,
201: new MySubClientException());
202: assertTrue("resolveException returns false", result);
203: assertTrue("Context has no response", context.hasResponse());
204: SoapMessage response = (SoapMessage) context.getResponse();
205: assertTrue("Resonse has no fault", response.getSoapBody()
206: .hasFault());
207: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
208: .getFault();
209: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
210: .getClientOrSenderFaultName(), fault.getFaultCode());
211: assertEquals("Invalid fault string on fault", "Client error",
212: fault.getFaultStringOrReason());
213: assertNull("Detail on fault", fault.getFaultDetail());
214: }
215:
216: public void testResolveExceptionExceptionMessage() throws Exception {
217: MessageFactory saajFactory = MessageFactory
218: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
219: SoapMessageFactory factory = new SaajSoapMessageFactory(
220: saajFactory);
221: MessageContext context = new DefaultMessageContext(factory);
222:
223: boolean result = resolver.resolveException(context, null,
224: new NoStringOrReasonException("Exception message"));
225: assertTrue("resolveException returns false", result);
226: assertTrue("Context has no response", context.hasResponse());
227: SoapMessage response = (SoapMessage) context.getResponse();
228: assertTrue("Resonse has no fault", response.getSoapBody()
229: .hasFault());
230: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
231: .getFault();
232: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
233: .getClientOrSenderFaultName(), fault.getFaultCode());
234: assertEquals("Invalid fault string on fault",
235: "Exception message", fault.getFaultStringOrReason());
236: assertNull("Detail on fault", fault.getFaultDetail());
237: }
238:
239: @SoapFault(faultCode=FaultCode.CLIENT,faultStringOrReason="Client error")
240: public class MyClientException extends Exception {
241:
242: }
243:
244: public class MySubClientException extends MyClientException {
245:
246: }
247:
248: @SoapFault(faultCode=FaultCode.CLIENT)
249: public class NoStringOrReasonException extends Exception {
250:
251: public NoStringOrReasonException(String message) {
252: super (message);
253: }
254: }
255:
256: @SoapFault(faultCode=FaultCode.SENDER,faultStringOrReason="Sender error")
257: public class MySenderException extends Exception {
258:
259: }
260:
261: @SoapFault(faultCode=FaultCode.SERVER,faultStringOrReason="Server error")
262: public class MyServerException extends Exception {
263:
264: }
265:
266: @SoapFault(faultCode=FaultCode.RECEIVER,faultStringOrReason="Receiver error")
267: public class MyReceiverException extends Exception {
268:
269: }
270:
271: @SoapFault(faultCode=FaultCode.CUSTOM,customFaultCode="{http://springframework.org/spring-ws}Fault",faultStringOrReason="MyCustomException thrown",locale="nl")
272: public class MyCustomException extends Exception {
273:
274: }
275:
276: public class NonAnnotatedException extends Exception {
277:
278: }
279:
280: }
|