001: /*
002: * Copyright 2006 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;
018:
019: import java.util.Iterator;
020: import java.util.Locale;
021: import javax.xml.namespace.QName;
022: import javax.xml.soap.MessageFactory;
023: import javax.xml.soap.SOAPConstants;
024: import javax.xml.soap.SOAPHeaderElement;
025: import javax.xml.soap.SOAPMessage;
026:
027: import junit.framework.TestCase;
028: import org.easymock.MockControl;
029: import org.springframework.ws.context.DefaultMessageContext;
030: import org.springframework.ws.context.MessageContext;
031: import org.springframework.ws.soap.SoapBody;
032: import org.springframework.ws.soap.SoapHeader;
033: import org.springframework.ws.soap.SoapHeaderElement;
034: import org.springframework.ws.soap.SoapMessage;
035: import org.springframework.ws.soap.SoapMessageFactory;
036: import org.springframework.ws.soap.saaj.SaajSoapMessage;
037: import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
038: import org.springframework.ws.soap.soap11.Soap11Fault;
039: import org.springframework.ws.soap.soap12.Soap12Fault;
040:
041: public class SoapMessageDispatcherTest extends TestCase {
042:
043: private SoapMessageDispatcher dispatcher;
044:
045: private MockControl interceptorControl;
046:
047: private SoapEndpointInterceptor interceptorMock;
048:
049: protected void setUp() throws Exception {
050: interceptorControl = MockControl
051: .createControl(SoapEndpointInterceptor.class);
052: interceptorMock = (SoapEndpointInterceptor) interceptorControl
053: .getMock();
054: dispatcher = new SoapMessageDispatcher();
055: }
056:
057: public void testProcessMustUnderstandHeadersUnderstoodSoap11()
058: throws Exception {
059: MessageFactory messageFactory = MessageFactory
060: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
061: SOAPMessage request = messageFactory.createMessage();
062: SOAPHeaderElement header = request.getSOAPHeader()
063: .addHeaderElement(
064: new QName("http://www.springframework.org",
065: "Header"));
066: header.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
067: header.setMustUnderstand(true);
068: SoapMessageFactory factory = new SaajSoapMessageFactory(
069: messageFactory);
070: MessageContext context = new DefaultMessageContext(
071: new SaajSoapMessage(request), factory);
072: interceptorMock.understands(null);
073: interceptorControl.setMatcher(MockControl.ALWAYS_MATCHER);
074: interceptorControl.setReturnValue(true);
075: interceptorControl.replay();
076:
077: SoapEndpointInvocationChain chain = new SoapEndpointInvocationChain(
078: new Object(),
079: new SoapEndpointInterceptor[] { interceptorMock });
080:
081: boolean result = dispatcher.handleRequest(chain, context);
082: assertTrue("Header not understood", result);
083: interceptorControl.verify();
084: }
085:
086: public void testProcessMustUnderstandHeadersUnderstoodSoap12()
087: throws Exception {
088: MessageFactory messageFactory = MessageFactory
089: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
090: SOAPMessage request = messageFactory.createMessage();
091: SOAPHeaderElement header = request.getSOAPHeader()
092: .addHeaderElement(
093: new QName("http://www.springframework.org",
094: "Header"));
095: header.setMustUnderstand(true);
096: header.setRole(SOAPConstants.URI_SOAP_1_2_ROLE_NEXT);
097: SoapMessageFactory factory = new SaajSoapMessageFactory(
098: messageFactory);
099: MessageContext context = new DefaultMessageContext(
100: new SaajSoapMessage(request), factory);
101: interceptorMock.understands(null);
102: interceptorControl.setMatcher(MockControl.ALWAYS_MATCHER);
103: interceptorControl.setReturnValue(true);
104: interceptorControl.replay();
105:
106: SoapEndpointInvocationChain chain = new SoapEndpointInvocationChain(
107: new Object(),
108: new SoapEndpointInterceptor[] { interceptorMock });
109:
110: boolean result = dispatcher.handleRequest(chain, context);
111: assertTrue("Header not understood", result);
112: interceptorControl.verify();
113: }
114:
115: public void testProcessMustUnderstandHeadersNotUnderstoodSoap11()
116: throws Exception {
117: MessageFactory messageFactory = MessageFactory
118: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
119: SOAPMessage request = messageFactory.createMessage();
120: SOAPHeaderElement header = request.getSOAPHeader()
121: .addHeaderElement(
122: new QName("http://www.springframework.org",
123: "Header", "spring-ws"));
124: header.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
125: header.setMustUnderstand(true);
126: SoapMessageFactory factory = new SaajSoapMessageFactory(
127: messageFactory);
128: MessageContext context = new DefaultMessageContext(
129: new SaajSoapMessage(request), factory);
130: interceptorMock.understands(null);
131: interceptorControl.setMatcher(MockControl.ALWAYS_MATCHER);
132: interceptorControl.setReturnValue(false);
133: interceptorControl.replay();
134:
135: SoapEndpointInvocationChain chain = new SoapEndpointInvocationChain(
136: new Object(),
137: new SoapEndpointInterceptor[] { interceptorMock });
138:
139: boolean result = dispatcher.handleRequest(chain, context);
140: assertFalse("Header understood", result);
141: assertTrue("Context has no response", context.hasResponse());
142: SoapBody responseBody = ((SoapMessage) context.getResponse())
143: .getSoapBody();
144: assertTrue("Response body has no fault", responseBody
145: .hasFault());
146: Soap11Fault fault = (Soap11Fault) responseBody.getFault();
147: assertEquals("Invalid fault code", new QName(
148: SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE,
149: "MustUnderstand"), fault.getFaultCode());
150: assertEquals(
151: "Invalid fault string",
152: SoapMessageDispatcher.DEFAULT_MUST_UNDERSTAND_FAULT_STRING,
153: fault.getFaultStringOrReason());
154: assertEquals("Invalid fault string locale", Locale.ENGLISH,
155: fault.getFaultStringLocale());
156: interceptorControl.verify();
157: }
158:
159: public void testProcessMustUnderstandHeadersNotUnderstoodSoap12()
160: throws Exception {
161: MessageFactory messageFactory = MessageFactory
162: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
163: SOAPMessage request = messageFactory.createMessage();
164: SOAPHeaderElement header = request.getSOAPHeader()
165: .addHeaderElement(
166: new QName("http://www.springframework.org",
167: "Header", "spring-ws"));
168: header.setMustUnderstand(true);
169: header.setRole(SOAPConstants.URI_SOAP_1_2_ROLE_NEXT);
170: SoapMessageFactory factory = new SaajSoapMessageFactory(
171: messageFactory);
172: MessageContext context = new DefaultMessageContext(
173: new SaajSoapMessage(request), factory);
174: interceptorMock.understands(null);
175: interceptorControl.setMatcher(MockControl.ALWAYS_MATCHER);
176: interceptorControl.setReturnValue(false);
177: interceptorControl.replay();
178:
179: SoapEndpointInvocationChain chain = new SoapEndpointInvocationChain(
180: new Object(),
181: new SoapEndpointInterceptor[] { interceptorMock });
182:
183: boolean result = dispatcher.handleRequest(chain, context);
184: assertFalse("Header understood", result);
185: assertTrue("Context has no response", context.hasResponse());
186: SoapMessage response = (SoapMessage) context.getResponse();
187: SoapBody responseBody = response.getSoapBody();
188: assertTrue("Response body has no fault", responseBody
189: .hasFault());
190: Soap12Fault fault = (Soap12Fault) responseBody.getFault();
191: assertEquals("Invalid fault code", new QName(
192: SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE,
193: "MustUnderstand"), fault.getFaultCode());
194: assertEquals(
195: "Invalid fault string",
196: SoapMessageDispatcher.DEFAULT_MUST_UNDERSTAND_FAULT_STRING,
197: fault.getFaultReasonText(Locale.ENGLISH));
198: SoapHeader responseHeader = response.getSoapHeader();
199: Iterator iterator = responseHeader.examineAllHeaderElements();
200: assertTrue("Response header has no elements", iterator
201: .hasNext());
202: SoapHeaderElement headerElement = (SoapHeaderElement) iterator
203: .next();
204: assertEquals("No NotUnderstood header",
205: new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE,
206: "NotUnderstood"), headerElement.getName());
207: interceptorControl.verify();
208: }
209:
210: public void testProcessMustUnderstandHeadersForActorSoap11()
211: throws Exception {
212: MessageFactory messageFactory = MessageFactory
213: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
214: SOAPMessage request = messageFactory.createMessage();
215: SOAPHeaderElement header = request.getSOAPHeader()
216: .addHeaderElement(
217: new QName("http://www.springframework.org",
218: "Header", "spring-ws"));
219: String headerActor = "http://www/springframework.org/role";
220: header.setActor(headerActor);
221: header.setMustUnderstand(true);
222: SoapMessageFactory factory = new SaajSoapMessageFactory(
223: messageFactory);
224: MessageContext context = new DefaultMessageContext(
225: new SaajSoapMessage(request), factory);
226: interceptorMock.understands(null);
227: interceptorControl.setMatcher(MockControl.ALWAYS_MATCHER);
228: interceptorControl.setReturnValue(true);
229: interceptorControl.replay();
230:
231: SoapEndpointInvocationChain chain = new SoapEndpointInvocationChain(
232: new Object(),
233: new SoapEndpointInterceptor[] { interceptorMock },
234: new String[] { headerActor }, true);
235:
236: boolean result = dispatcher.handleRequest(chain, context);
237: assertTrue("actor-specific header not understood", result);
238: interceptorControl.verify();
239: }
240:
241: public void testProcessMustUnderstandHeadersForRoleSoap12()
242: throws Exception {
243: MessageFactory messageFactory = MessageFactory
244: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
245: SOAPMessage request = messageFactory.createMessage();
246: SOAPHeaderElement header = request.getSOAPHeader()
247: .addHeaderElement(
248: new QName("http://www.springframework.org",
249: "Header", "spring-ws"));
250: String headerRole = "http://www/springframework.org/role";
251: header.setRole(headerRole);
252: header.setMustUnderstand(true);
253: SoapMessageFactory factory = new SaajSoapMessageFactory(
254: messageFactory);
255: MessageContext context = new DefaultMessageContext(
256: new SaajSoapMessage(request), factory);
257: interceptorMock.understands(null);
258: interceptorControl.setMatcher(MockControl.ALWAYS_MATCHER);
259: interceptorControl.setReturnValue(true);
260: interceptorControl.replay();
261:
262: SoapEndpointInvocationChain chain = new SoapEndpointInvocationChain(
263: new Object(),
264: new SoapEndpointInterceptor[] { interceptorMock },
265: new String[] { headerRole }, true);
266:
267: boolean result = dispatcher.handleRequest(chain, context);
268: assertTrue("role-specific header not understood", result);
269: interceptorControl.verify();
270: }
271:
272: public void testProcessNoHeader() throws Exception {
273: MessageFactory messageFactory = MessageFactory
274: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
275: SOAPMessage request = messageFactory.createMessage();
276: request.getSOAPHeader().detachNode();
277: SoapMessageFactory factory = new SaajSoapMessageFactory(
278: messageFactory);
279: MessageContext context = new DefaultMessageContext(
280: new SaajSoapMessage(request), factory);
281: interceptorControl.replay();
282:
283: SoapEndpointInvocationChain chain = new SoapEndpointInvocationChain(
284: new Object(),
285: new SoapEndpointInterceptor[] { interceptorMock },
286: new String[] { "role" }, true);
287:
288: boolean result = dispatcher.handleRequest(chain, context);
289: assertTrue("Invalid result", result);
290: interceptorControl.verify();
291: }
292: }
|