001: /*
002: * Copyright 2005 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 java.util.Properties;
021: import javax.xml.soap.MessageFactory;
022: import javax.xml.soap.SOAPConstants;
023: import javax.xml.soap.SOAPMessage;
024:
025: import org.custommonkey.xmlunit.XMLTestCase;
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.SoapMessageException;
030: import org.springframework.ws.soap.SoapMessageFactory;
031: import org.springframework.ws.soap.SoapVersion;
032: import org.springframework.ws.soap.saaj.SaajSoapMessage;
033: import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
034: import org.springframework.ws.soap.soap11.Soap11Fault;
035: import org.springframework.ws.soap.soap12.Soap12Fault;
036:
037: public class SoapFaultMappingExceptionResolverTest extends XMLTestCase {
038:
039: private SoapFaultMappingExceptionResolver resolver;
040:
041: protected void setUp() throws Exception {
042: resolver = new SoapFaultMappingExceptionResolver();
043: }
044:
045: public void testGetDepth() throws Exception {
046: assertEquals("Invalid depth for Exception", 0, resolver
047: .getDepth("java.lang.Exception", new Exception()));
048: assertEquals("Invalid depth for IllegalArgumentException", 2,
049: resolver.getDepth("java.lang.Exception",
050: new IllegalArgumentException()));
051: assertEquals("Invalid depth for IllegalStateException", -1,
052: resolver.getDepth("IllegalArgumentException",
053: new IllegalStateException()));
054: }
055:
056: public void testResolveExceptionClientSoap11() throws Exception {
057: Properties mappings = new Properties();
058: mappings.setProperty(Exception.class.getName(),
059: "SERVER, Server error");
060: mappings.setProperty(RuntimeException.class.getName(),
061: "CLIENT, Client error");
062: resolver.setExceptionMappings(mappings);
063:
064: MessageFactory messageFactory = MessageFactory
065: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
066: SOAPMessage message = messageFactory.createMessage();
067: SoapMessageFactory factory = new SaajSoapMessageFactory(
068: messageFactory);
069: MessageContext context = new DefaultMessageContext(
070: new SaajSoapMessage(message), factory);
071:
072: boolean result = resolver.resolveException(context, null,
073: new IllegalArgumentException("bla"));
074: assertTrue("resolveException returns false", result);
075: assertTrue("Context has no response", context.hasResponse());
076: SoapMessage response = (SoapMessage) context.getResponse();
077: assertTrue("Response has no fault", response.getSoapBody()
078: .hasFault());
079: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
080: .getFault();
081: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
082: .getClientOrSenderFaultName(), fault.getFaultCode());
083: assertEquals("Invalid fault string on fault", "Client error",
084: fault.getFaultStringOrReason());
085: assertNull("Detail on fault", fault.getFaultDetail());
086: }
087:
088: public void testResolveExceptionSenderSoap12() throws Exception {
089: Properties mappings = new Properties();
090: mappings.setProperty(Exception.class.getName(),
091: "RECEIVER, Receiver error, en");
092: mappings.setProperty(RuntimeException.class.getName(),
093: "SENDER, Sender error, en");
094: resolver.setExceptionMappings(mappings);
095:
096: MessageFactory messageFactory = MessageFactory
097: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
098: SOAPMessage message = messageFactory.createMessage();
099: SoapMessageFactory factory = new SaajSoapMessageFactory(
100: messageFactory);
101: MessageContext context = new DefaultMessageContext(
102: new SaajSoapMessage(message), factory);
103:
104: boolean result = resolver.resolveException(context, null,
105: new IllegalArgumentException("bla"));
106: assertTrue("resolveException returns false", result);
107: assertTrue("Context has no response", context.hasResponse());
108: SoapMessage response = (SoapMessage) context.getResponse();
109: assertTrue("Response has no fault", response.getSoapBody()
110: .hasFault());
111: Soap12Fault fault = (Soap12Fault) response.getSoapBody()
112: .getFault();
113: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_12
114: .getClientOrSenderFaultName(), fault.getFaultCode());
115: assertEquals("Invalid fault string on fault", "Sender error",
116: fault.getFaultReasonText(Locale.ENGLISH));
117: assertNull("Detail on fault", fault.getFaultDetail());
118: }
119:
120: public void testResolveExceptionServerSoap11() throws Exception {
121: Properties mappings = new Properties();
122: mappings.setProperty(Exception.class.getName(),
123: "CLIENT, Client error");
124: mappings.setProperty(RuntimeException.class.getName(),
125: "SERVER, Server error");
126: resolver.setExceptionMappings(mappings);
127:
128: MessageFactory messageFactory = MessageFactory
129: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
130: SOAPMessage message = messageFactory.createMessage();
131: SoapMessageFactory factory = new SaajSoapMessageFactory(
132: messageFactory);
133: MessageContext context = new DefaultMessageContext(
134: new SaajSoapMessage(message), factory);
135:
136: boolean result = resolver.resolveException(context, null,
137: new IllegalArgumentException("bla"));
138: assertTrue("resolveException returns false", result);
139: assertTrue("Context has no response", context.hasResponse());
140: SoapMessage response = (SoapMessage) context.getResponse();
141: assertTrue("Response has no fault", response.getSoapBody()
142: .hasFault());
143: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
144: .getFault();
145: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
146: .getServerOrReceiverFaultName(), fault.getFaultCode());
147: assertEquals("Invalid fault string on fault", "Server error",
148: fault.getFaultStringOrReason());
149: assertNull("Detail on fault", fault.getFaultDetail());
150: }
151:
152: public void testResolveExceptionReceiverSoap12() throws Exception {
153: Properties mappings = new Properties();
154: mappings.setProperty(Exception.class.getName(),
155: "SENDER, Sender error");
156: mappings.setProperty(RuntimeException.class.getName(),
157: "RECEIVER, Receiver error");
158: resolver.setExceptionMappings(mappings);
159:
160: MessageFactory messageFactory = MessageFactory
161: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
162: SOAPMessage message = messageFactory.createMessage();
163: SoapMessageFactory factory = new SaajSoapMessageFactory(
164: messageFactory);
165: MessageContext context = new DefaultMessageContext(
166: new SaajSoapMessage(message), factory);
167:
168: boolean result = resolver.resolveException(context, null,
169: new IllegalArgumentException("bla"));
170: assertTrue("resolveException returns false", result);
171: assertTrue("Context has no response", context.hasResponse());
172: SoapMessage response = (SoapMessage) context.getResponse();
173: assertTrue("Response has no fault", response.getSoapBody()
174: .hasFault());
175: Soap12Fault fault = (Soap12Fault) response.getSoapBody()
176: .getFault();
177: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_12
178: .getServerOrReceiverFaultName(), fault.getFaultCode());
179: assertEquals("Invalid fault string on fault", "Receiver error",
180: fault.getFaultReasonText(Locale.ENGLISH));
181: assertNull("Detail on fault", fault.getFaultDetail());
182: }
183:
184: public void testResolveExceptionDefault() throws Exception {
185: Properties mappings = new Properties();
186: mappings.setProperty(SoapMessageException.class.getName(),
187: "SERVER,Server error");
188: resolver.setExceptionMappings(mappings);
189: SoapFaultDefinition defaultFault = new SoapFaultDefinition();
190: defaultFault.setFaultCode(SoapFaultDefinition.CLIENT);
191: defaultFault.setFaultStringOrReason("faultstring");
192: resolver.setDefaultFault(defaultFault);
193: MessageFactory messageFactory = MessageFactory
194: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
195: SOAPMessage message = messageFactory.createMessage();
196: SoapMessageFactory factory = new SaajSoapMessageFactory(
197: messageFactory);
198: MessageContext context = new DefaultMessageContext(
199: new SaajSoapMessage(message), factory);
200:
201: boolean result = resolver.resolveException(context, null,
202: new IllegalArgumentException("bla"));
203: assertTrue("resolveException returns false", result);
204: assertTrue("Context has no response", context.hasResponse());
205: SoapMessage response = (SoapMessage) context.getResponse();
206: assertTrue("Response has no fault", response.getSoapBody()
207: .hasFault());
208: Soap11Fault fault = (Soap11Fault) response.getSoapBody()
209: .getFault();
210: assertEquals("Invalid fault code on fault", SoapVersion.SOAP_11
211: .getClientOrSenderFaultName(), fault.getFaultCode());
212: assertEquals("Invalid fault string on fault", "faultstring",
213: fault.getFaultStringOrReason());
214: assertNull("Detail on fault", fault.getFaultDetail());
215: }
216:
217: }
|