1 package org.springframework.ws.server.endpoint.adapter; 2 3 import java.lang.reflect.Method; 4 5 import junit.framework.TestCase; 6 import org.easymock.MockControl; 7 import org.springframework.oxm.Marshaller; 8 import org.springframework.oxm.Unmarshaller; 9 import org.springframework.ws.MockWebServiceMessage; 10 import org.springframework.ws.MockWebServiceMessageFactory; 11 import org.springframework.ws.context.DefaultMessageContext; 12 import org.springframework.ws.context.MessageContext; 13 import org.springframework.ws.server.endpoint.MethodEndpoint; 14 15 public class MarshallingMethodEndpointAdapterTest extends TestCase { 16 17 private MarshallingMethodEndpointAdapter adapter; 18 19 private boolean noResponseInvoked; 20 21 private MockControl marshallerControl; 22 23 private Marshaller marshallerMock; 24 25 private MockControl unmarshallerControl; 26 27 private Unmarshaller unmarshallerMock; 28 29 private MessageContext messageContext; 30 31 private boolean responseInvoked; 32 33 protected void setUp() throws Exception { 34 adapter = new MarshallingMethodEndpointAdapter(); 35 marshallerControl = MockControl.createControl(Marshaller.class); 36 marshallerMock = (Marshaller) marshallerControl.getMock(); 37 adapter.setMarshaller(marshallerMock); 38 unmarshallerControl = MockControl.createControl(Unmarshaller.class); 39 unmarshallerMock = (Unmarshaller) unmarshallerControl.getMock(); 40 adapter.setUnmarshaller(unmarshallerMock); 41 adapter.afterPropertiesSet(); 42 MockWebServiceMessage request = new MockWebServiceMessage("<request/>"); 43 messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory()); 44 } 45 46 public void testNoResponse() throws Exception { 47 Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class}); 48 MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse); 49 unmarshallerMock.unmarshal(messageContext.getRequest().getPayloadSource()); 50 unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER); 51 unmarshallerControl.setReturnValue(new MyType()); 52 marshallerControl.replay(); 53 unmarshallerControl.replay(); 54 assertFalse("Method invoked", noResponseInvoked); 55 adapter.invoke(messageContext, methodEndpoint); 56 assertTrue("Method not invoked", noResponseInvoked); 57 marshallerControl.verify(); 58 unmarshallerControl.verify(); 59 } 60 61 public void testNoRequestPayload() throws Exception { 62 MockWebServiceMessage request = new MockWebServiceMessage(); 63 messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory()); 64 Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class}); 65 MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse); 66 marshallerControl.replay(); 67 unmarshallerControl.replay(); 68 assertFalse("Method invoked", noResponseInvoked); 69 adapter.invoke(messageContext, methodEndpoint); 70 assertTrue("Method not invoked", noResponseInvoked); 71 marshallerControl.verify(); 72 unmarshallerControl.verify(); 73 } 74 75 public void testResponse() throws Exception { 76 Method response = getClass().getMethod("response", new Class[]{MyType.class}); 77 MethodEndpoint methodEndpoint = new MethodEndpoint(this, response); 78 unmarshallerMock.unmarshal(messageContext.getRequest().getPayloadSource()); 79 unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER); 80 unmarshallerControl.setReturnValue(new MyType()); 81 marshallerMock.marshal(new MyType(), messageContext.getResponse().getPayloadResult()); 82 marshallerControl.setMatcher(MockControl.ALWAYS_MATCHER); 83 marshallerControl.replay(); 84 unmarshallerControl.replay(); 85 assertFalse("Method invoked", responseInvoked); 86 adapter.invoke(messageContext, methodEndpoint); 87 assertTrue("Method not invoked", responseInvoked); 88 marshallerControl.verify(); 89 unmarshallerControl.verify(); 90 91 } 92 93 public void testSupportedNoResponse() throws NoSuchMethodException { 94 Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class}); 95 MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse); 96 unmarshallerControl.expectAndReturn(unmarshallerMock.supports(MyType.class), true); 97 marshallerControl.replay(); 98 unmarshallerControl.replay(); 99 assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint)); 100 marshallerControl.verify(); 101 unmarshallerControl.verify(); 102 } 103 104 public void testSupportedResponse() throws NoSuchMethodException { 105 Method response = getClass().getMethod("response", new Class[]{MyType.class}); 106 MethodEndpoint methodEndpoint = new MethodEndpoint(this, response); 107 unmarshallerControl.expectAndReturn(unmarshallerMock.supports(MyType.class), true); 108 marshallerControl.expectAndReturn(marshallerMock.supports(MyType.class), true); 109 marshallerControl.replay(); 110 unmarshallerControl.replay(); 111 assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint)); 112 marshallerControl.verify(); 113 unmarshallerControl.verify(); 114 } 115 116 public void testUnsupportedMethodMultipleParams() throws NoSuchMethodException { 117 Method unsupported = getClass().getMethod("unsupportedMultipleParams", new Class[]{String.class, String.class}); 118 marshallerControl.replay(); 119 unmarshallerControl.replay(); 120 assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported))); 121 marshallerControl.verify(); 122 unmarshallerControl.verify(); 123 } 124 125 public void testUnsupportedMethodWrongParam() throws NoSuchMethodException { 126 Method unsupported = getClass().getMethod("unsupportedWrongParam", new Class[]{String.class}); 127 unmarshallerControl.expectAndReturn(unmarshallerMock.supports(String.class), false); 128 marshallerControl.expectAndReturn(marshallerMock.supports(String.class), true); 129 marshallerControl.replay(); 130 unmarshallerControl.replay(); 131 assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported))); 132 marshallerControl.verify(); 133 unmarshallerControl.verify(); 134 } 135 136 public void testUnsupportedMethodWrongReturnType() throws NoSuchMethodException { 137 Method unsupported = getClass().getMethod("unsupportedWrongParam", new Class[]{String.class}); 138 marshallerControl.expectAndReturn(marshallerMock.supports(String.class), false); 139 marshallerControl.replay(); 140 unmarshallerControl.replay(); 141 assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported))); 142 marshallerControl.verify(); 143 unmarshallerControl.verify(); 144 } 145 146 public void noResponse(MyType type) { 147 noResponseInvoked = true; 148 149 } 150 151 public MyType response(MyType type) { 152 responseInvoked = true; 153 return new MyType(); 154 } 155 156 public void unsupportedMultipleParams(String s1, String s2) { 157 } 158 159 public String unsupportedWrongParam(String s) { 160 return s; 161 } 162 163 private static class MyType { 164 165 } 166 167 }