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.server;
018:
019: import java.util.Collections;
020:
021: import junit.framework.TestCase;
022: import org.easymock.MockControl;
023: import org.springframework.context.support.StaticApplicationContext;
024: import org.springframework.ws.MockWebServiceMessage;
025: import org.springframework.ws.NoEndpointFoundException;
026: import org.springframework.ws.WebServiceMessageFactory;
027: import org.springframework.ws.context.DefaultMessageContext;
028: import org.springframework.ws.context.MessageContext;
029: import org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter;
030: import org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping;
031: import org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver;
032:
033: public class MessageDispatcherTest extends TestCase {
034:
035: private MessageDispatcher dispatcher;
036:
037: private MessageContext messageContext;
038:
039: private MockControl factoryControl;
040:
041: private WebServiceMessageFactory factoryMock;
042:
043: protected void setUp() throws Exception {
044: dispatcher = new MessageDispatcher();
045: factoryControl = MockControl
046: .createControl(WebServiceMessageFactory.class);
047: factoryMock = (WebServiceMessageFactory) factoryControl
048: .getMock();
049: messageContext = new DefaultMessageContext(
050: new MockWebServiceMessage(), factoryMock);
051: }
052:
053: public void testGetEndpoint() throws Exception {
054: MockControl mappingControl = MockControl
055: .createControl(EndpointMapping.class);
056: EndpointMapping mappingMock = (EndpointMapping) mappingControl
057: .getMock();
058: dispatcher.setEndpointMappings(Collections
059: .singletonList(mappingMock));
060:
061: EndpointInvocationChain chain = new EndpointInvocationChain(
062: new Object());
063:
064: mappingControl.expectAndReturn(mappingMock
065: .getEndpoint(messageContext), chain);
066:
067: mappingControl.replay();
068: factoryControl.replay();
069: EndpointInvocationChain result = dispatcher
070: .getEndpoint(messageContext);
071: mappingControl.verify();
072: factoryControl.verify();
073: assertEquals(
074: "getEndpoint returns invalid EndpointInvocationChain",
075: chain, result);
076: }
077:
078: public void testGetEndpointAdapterSupportedEndpoint()
079: throws Exception {
080: MockControl adapterControl = MockControl
081: .createControl(EndpointAdapter.class);
082: EndpointAdapter adapterMock = (EndpointAdapter) adapterControl
083: .getMock();
084: dispatcher.setEndpointAdapters(Collections
085: .singletonList(adapterMock));
086:
087: Object endpoint = new Object();
088: adapterControl.expectAndReturn(adapterMock.supports(endpoint),
089: true);
090: adapterControl.replay();
091: factoryControl.replay();
092: EndpointAdapter result = dispatcher
093: .getEndpointAdapter(endpoint);
094: adapterControl.verify();
095: factoryControl.verify();
096: assertEquals(
097: "getEnpointAdapter returns invalid EndpointAdapter",
098: adapterMock, result);
099: }
100:
101: public void testGetEndpointAdapterUnsupportedEndpoint()
102: throws Exception {
103: MockControl adapterControl = MockControl
104: .createControl(EndpointAdapter.class);
105: EndpointAdapter adapterMock = (EndpointAdapter) adapterControl
106: .getMock();
107: dispatcher.setEndpointAdapters(Collections
108: .singletonList(adapterMock));
109:
110: Object endpoint = new Object();
111: adapterControl.expectAndReturn(adapterMock.supports(endpoint),
112: false);
113: adapterControl.replay();
114: factoryControl.replay();
115: try {
116: dispatcher.getEndpointAdapter(endpoint);
117: fail("getEndpointAdapter does not throw IllegalStateException for unsupported endpoint");
118: } catch (IllegalStateException ex) {
119: // Expected
120: }
121: adapterControl.verify();
122: factoryControl.verify();
123: }
124:
125: public void testResolveException() throws Exception {
126: final Exception ex = new Exception();
127: EndpointMapping endpointMapping = new EndpointMapping() {
128:
129: public EndpointInvocationChain getEndpoint(
130: MessageContext messageContext) throws Exception {
131: throw ex;
132: }
133: };
134: dispatcher.setEndpointMappings(Collections
135: .singletonList(endpointMapping));
136: EndpointExceptionResolver resolver = new EndpointExceptionResolver() {
137:
138: public boolean resolveException(
139: MessageContext givenMessageContext,
140: Object givenEndpoint, Exception givenException) {
141: assertEquals("Invalid message context", messageContext,
142: givenMessageContext);
143: assertNull("Invalid endpoint", givenEndpoint);
144: assertEquals("Invalid exception", ex, givenException);
145: givenMessageContext.getResponse();
146: return true;
147: }
148:
149: };
150: dispatcher.setEndpointExceptionResolvers(Collections
151: .singletonList(resolver));
152: factoryControl
153: .expectAndReturn(factoryMock.createWebServiceMessage(),
154: new MockWebServiceMessage());
155: factoryControl.replay();
156:
157: dispatcher.dispatch(messageContext);
158: assertNotNull("processEndpointException sets no response",
159: messageContext.getResponse());
160: factoryControl.verify();
161: }
162:
163: public void testProcessUnsupportedEndpointException()
164: throws Exception {
165: MockControl resolverControl = MockControl
166: .createControl(EndpointExceptionResolver.class);
167: EndpointExceptionResolver resolverMock = (EndpointExceptionResolver) resolverControl
168: .getMock();
169: dispatcher.setEndpointExceptionResolvers(Collections
170: .singletonList(resolverMock));
171:
172: Object endpoint = new Object();
173: Exception ex = new Exception();
174:
175: resolverControl.expectAndReturn(resolverMock.resolveException(
176: messageContext, endpoint, ex), false);
177:
178: resolverControl.replay();
179: try {
180: dispatcher.processEndpointException(messageContext,
181: endpoint, ex);
182: } catch (Exception result) {
183: assertEquals(
184: "processEndpointException throws invalid exception",
185: ex, result);
186: }
187: resolverControl.verify();
188: }
189:
190: public void testNormalFlow() throws Exception {
191: MockControl adapterControl = MockControl
192: .createControl(EndpointAdapter.class);
193: EndpointAdapter adapterMock = (EndpointAdapter) adapterControl
194: .getMock();
195: dispatcher.setEndpointAdapters(Collections
196: .singletonList(adapterMock));
197:
198: Object endpoint = new Object();
199: adapterControl.expectAndReturn(adapterMock.supports(endpoint),
200: true);
201:
202: MockControl mappingControl = MockControl
203: .createControl(EndpointMapping.class);
204: EndpointMapping mappingMock = (EndpointMapping) mappingControl
205: .getMock();
206: dispatcher.setEndpointMappings(Collections
207: .singletonList(mappingMock));
208:
209: MockControl interceptorControl = MockControl
210: .createStrictControl(EndpointInterceptor.class);
211: EndpointInterceptor interceptorMock1 = (EndpointInterceptor) interceptorControl
212: .getMock();
213: EndpointInterceptor interceptorMock2 = (EndpointInterceptor) interceptorControl
214: .getMock();
215:
216: interceptorControl.expectAndReturn(interceptorMock1
217: .handleRequest(messageContext, endpoint), true);
218: interceptorControl.expectAndReturn(interceptorMock2
219: .handleRequest(messageContext, endpoint), true);
220: adapterMock.invoke(messageContext, endpoint);
221: interceptorControl.expectAndReturn(interceptorMock2
222: .handleResponse(messageContext, endpoint), true);
223: interceptorControl.expectAndReturn(interceptorMock1
224: .handleResponse(messageContext, endpoint), true);
225:
226: EndpointInvocationChain chain = new EndpointInvocationChain(
227: endpoint, new EndpointInterceptor[] { interceptorMock1,
228: interceptorMock2 });
229:
230: mappingControl.expectAndReturn(mappingMock
231: .getEndpoint(messageContext), chain);
232: factoryControl
233: .expectAndReturn(factoryMock.createWebServiceMessage(),
234: new MockWebServiceMessage());
235:
236: mappingControl.replay();
237: interceptorControl.replay();
238: adapterControl.replay();
239: factoryControl.replay();
240: // response required for interceptor invocation
241: messageContext.getResponse();
242: dispatcher.dispatch(messageContext);
243:
244: mappingControl.verify();
245: interceptorControl.verify();
246: adapterControl.verify();
247: factoryControl.verify();
248: }
249:
250: public void testFlowNoResponse() throws Exception {
251: MockControl adapterControl = MockControl
252: .createControl(EndpointAdapter.class);
253: EndpointAdapter adapterMock = (EndpointAdapter) adapterControl
254: .getMock();
255: dispatcher.setEndpointAdapters(Collections
256: .singletonList(adapterMock));
257:
258: Object endpoint = new Object();
259: adapterControl.expectAndReturn(adapterMock.supports(endpoint),
260: true);
261:
262: MockControl mappingControl = MockControl
263: .createControl(EndpointMapping.class);
264: EndpointMapping mappingMock = (EndpointMapping) mappingControl
265: .getMock();
266: dispatcher.setEndpointMappings(Collections
267: .singletonList(mappingMock));
268:
269: MockControl interceptorControl = MockControl
270: .createStrictControl(EndpointInterceptor.class);
271: EndpointInterceptor interceptorMock1 = (EndpointInterceptor) interceptorControl
272: .getMock();
273: EndpointInterceptor interceptorMock2 = (EndpointInterceptor) interceptorControl
274: .getMock();
275:
276: EndpointInvocationChain chain = new EndpointInvocationChain(
277: endpoint, new EndpointInterceptor[] { interceptorMock1,
278: interceptorMock2 });
279: mappingControl.expectAndReturn(mappingMock
280: .getEndpoint(messageContext), chain);
281:
282: interceptorControl.expectAndReturn(interceptorMock1
283: .handleRequest(messageContext, endpoint), true);
284: interceptorControl.expectAndReturn(interceptorMock2
285: .handleRequest(messageContext, endpoint), true);
286: adapterMock.invoke(messageContext, endpoint);
287:
288: mappingControl.replay();
289: interceptorControl.replay();
290: adapterControl.replay();
291: factoryControl.replay();
292:
293: dispatcher.dispatch(messageContext);
294:
295: mappingControl.verify();
296: interceptorControl.verify();
297: adapterControl.verify();
298: factoryControl.verify();
299: }
300:
301: public void testInterceptedRequestFlow() throws Exception {
302: MockControl adapterControl = MockControl
303: .createControl(EndpointAdapter.class);
304: EndpointAdapter adapterMock = (EndpointAdapter) adapterControl
305: .getMock();
306: dispatcher.setEndpointAdapters(Collections
307: .singletonList(adapterMock));
308:
309: MockControl mappingControl = MockControl
310: .createControl(EndpointMapping.class);
311: EndpointMapping mappingMock = (EndpointMapping) mappingControl
312: .getMock();
313: dispatcher.setEndpointMappings(Collections
314: .singletonList(mappingMock));
315:
316: MockControl interceptorControl = MockControl
317: .createStrictControl(EndpointInterceptor.class);
318: EndpointInterceptor interceptorMock1 = (EndpointInterceptor) interceptorControl
319: .getMock();
320: EndpointInterceptor interceptorMock2 = (EndpointInterceptor) interceptorControl
321: .getMock();
322:
323: Object endpoint = new Object();
324: interceptorControl.expectAndReturn(interceptorMock1
325: .handleRequest(messageContext, endpoint), false);
326: interceptorControl.expectAndReturn(interceptorMock1
327: .handleResponse(messageContext, endpoint), true);
328:
329: EndpointInvocationChain chain = new EndpointInvocationChain(
330: endpoint, new EndpointInterceptor[] { interceptorMock1,
331: interceptorMock2 });
332:
333: mappingControl.expectAndReturn(mappingMock
334: .getEndpoint(messageContext), chain);
335: factoryControl
336: .expectAndReturn(factoryMock.createWebServiceMessage(),
337: new MockWebServiceMessage());
338:
339: mappingControl.replay();
340: interceptorControl.replay();
341: adapterControl.replay();
342: factoryControl.replay();
343:
344: // response required for interceptor invocation
345: messageContext.getResponse();
346:
347: dispatcher.dispatch(messageContext);
348:
349: mappingControl.verify();
350: interceptorControl.verify();
351: adapterControl.verify();
352: factoryControl.verify();
353: }
354:
355: public void testInterceptedResponseFlow() throws Exception {
356: MockControl adapterControl = MockControl
357: .createControl(EndpointAdapter.class);
358: EndpointAdapter adapterMock = (EndpointAdapter) adapterControl
359: .getMock();
360: dispatcher.setEndpointAdapters(Collections
361: .singletonList(adapterMock));
362:
363: MockControl mappingControl = MockControl
364: .createControl(EndpointMapping.class);
365: EndpointMapping mappingMock = (EndpointMapping) mappingControl
366: .getMock();
367: dispatcher.setEndpointMappings(Collections
368: .singletonList(mappingMock));
369:
370: MockControl interceptorControl = MockControl
371: .createStrictControl(EndpointInterceptor.class);
372: EndpointInterceptor interceptorMock1 = (EndpointInterceptor) interceptorControl
373: .getMock();
374: EndpointInterceptor interceptorMock2 = (EndpointInterceptor) interceptorControl
375: .getMock();
376:
377: Object endpoint = new Object();
378: interceptorControl.expectAndReturn(interceptorMock1
379: .handleRequest(messageContext, endpoint), true);
380: interceptorControl.expectAndReturn(interceptorMock2
381: .handleRequest(messageContext, endpoint), false);
382: interceptorControl.expectAndReturn(interceptorMock2
383: .handleResponse(messageContext, endpoint), false);
384:
385: EndpointInvocationChain chain = new EndpointInvocationChain(
386: endpoint, new EndpointInterceptor[] { interceptorMock1,
387: interceptorMock2 });
388:
389: mappingControl.expectAndReturn(mappingMock
390: .getEndpoint(messageContext), chain);
391: factoryControl
392: .expectAndReturn(factoryMock.createWebServiceMessage(),
393: new MockWebServiceMessage());
394:
395: mappingControl.replay();
396: interceptorControl.replay();
397: adapterControl.replay();
398: factoryControl.replay();
399: // response required for interceptor invocation
400: messageContext.getResponse();
401:
402: dispatcher.dispatch(messageContext);
403:
404: mappingControl.verify();
405: interceptorControl.verify();
406: adapterControl.verify();
407: factoryControl.verify();
408: }
409:
410: public void testFaultFlow() throws Exception {
411: MockControl adapterControl = MockControl
412: .createControl(EndpointAdapter.class);
413: EndpointAdapter adapterMock = (EndpointAdapter) adapterControl
414: .getMock();
415: dispatcher.setEndpointAdapters(Collections
416: .singletonList(adapterMock));
417:
418: Object endpoint = new Object();
419: adapterControl.expectAndReturn(adapterMock.supports(endpoint),
420: true);
421:
422: MockControl mappingControl = MockControl
423: .createControl(EndpointMapping.class);
424: EndpointMapping mappingMock = (EndpointMapping) mappingControl
425: .getMock();
426: dispatcher.setEndpointMappings(Collections
427: .singletonList(mappingMock));
428:
429: MockControl interceptorControl = MockControl
430: .createStrictControl(EndpointInterceptor.class);
431: EndpointInterceptor interceptorMock = (EndpointInterceptor) interceptorControl
432: .getMock();
433:
434: interceptorControl.expectAndReturn(interceptorMock
435: .handleRequest(messageContext, endpoint), true);
436: adapterMock.invoke(messageContext, endpoint);
437: interceptorControl.expectAndReturn(interceptorMock.handleFault(
438: messageContext, endpoint), true);
439:
440: EndpointInvocationChain chain = new EndpointInvocationChain(
441: endpoint, new EndpointInterceptor[] { interceptorMock });
442:
443: mappingControl.expectAndReturn(mappingMock
444: .getEndpoint(messageContext), chain);
445: MockWebServiceMessage response = new MockWebServiceMessage();
446: response.setFault(true);
447: factoryControl.expectAndReturn(factoryMock
448: .createWebServiceMessage(), response);
449:
450: mappingControl.replay();
451: interceptorControl.replay();
452: adapterControl.replay();
453: factoryControl.replay();
454: // response required for interceptor invocation
455: messageContext.getResponse();
456: dispatcher.dispatch(messageContext);
457:
458: mappingControl.verify();
459: interceptorControl.verify();
460: adapterControl.verify();
461: factoryControl.verify();
462: }
463:
464: public void testNoEndpointFound() throws Exception {
465: dispatcher.setEndpointMappings(Collections.EMPTY_LIST);
466: try {
467: dispatcher.receive(messageContext);
468: fail("NoEndpointFoundException expected");
469: } catch (NoEndpointFoundException ex) {
470: // expected
471: }
472: }
473:
474: public void testDetectStrategies() throws Exception {
475: StaticApplicationContext applicationContext = new StaticApplicationContext();
476: applicationContext.registerSingleton("mapping",
477: PayloadRootQNameEndpointMapping.class);
478: applicationContext.registerSingleton("adapter",
479: PayloadEndpointAdapter.class);
480: applicationContext.registerSingleton("resolver",
481: SimpleSoapExceptionResolver.class);
482: dispatcher.setApplicationContext(applicationContext);
483: assertEquals("Invalid amount of mappings detected", 1,
484: dispatcher.getEndpointMappings().size());
485: assertTrue(
486: "Invalid mappings detected",
487: dispatcher.getEndpointMappings().get(0) instanceof PayloadRootQNameEndpointMapping);
488: assertEquals("Invalid amount of adapters detected", 1,
489: dispatcher.getEndpointAdapters().size());
490: assertTrue(
491: "Invalid mappings detected",
492: dispatcher.getEndpointAdapters().get(0) instanceof PayloadEndpointAdapter);
493: assertEquals("Invalid amount of resolvers detected", 1,
494: dispatcher.getEndpointExceptionResolvers().size());
495: assertTrue(
496: "Invalid mappings detected",
497: dispatcher.getEndpointExceptionResolvers().get(0) instanceof SimpleSoapExceptionResolver);
498: }
499:
500: public void testDefaultStrategies() throws Exception {
501: StaticApplicationContext applicationContext = new StaticApplicationContext();
502: dispatcher.setApplicationContext(applicationContext);
503: }
504:
505: }
|