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