001: package org.springframework.ws.server.endpoint.adapter;
002:
003: import java.lang.reflect.Method;
004:
005: import junit.framework.TestCase;
006: import org.easymock.MockControl;
007: import org.springframework.oxm.Marshaller;
008: import org.springframework.oxm.Unmarshaller;
009: import org.springframework.ws.MockWebServiceMessageFactory;
010: import org.springframework.ws.context.DefaultMessageContext;
011: import org.springframework.ws.context.MessageContext;
012: import org.springframework.ws.server.endpoint.MethodEndpoint;
013:
014: public class MarshallingMethodEndpointAdapterTest extends TestCase {
015:
016: private MarshallingMethodEndpointAdapter adapter;
017:
018: private boolean noResponseInvoked;
019:
020: private MockControl marshallerControl;
021:
022: private Marshaller marshallerMock;
023:
024: private MockControl unmarshallerControl;
025:
026: private Unmarshaller unmarshallerMock;
027:
028: private MessageContext messageContext;
029:
030: private boolean responseInvoked;
031:
032: protected void setUp() throws Exception {
033: adapter = new MarshallingMethodEndpointAdapter();
034: marshallerControl = MockControl.createControl(Marshaller.class);
035: marshallerMock = (Marshaller) marshallerControl.getMock();
036: adapter.setMarshaller(marshallerMock);
037: unmarshallerControl = MockControl
038: .createControl(Unmarshaller.class);
039: unmarshallerMock = (Unmarshaller) unmarshallerControl.getMock();
040: adapter.setUnmarshaller(unmarshallerMock);
041: adapter.afterPropertiesSet();
042: messageContext = new DefaultMessageContext(
043: new MockWebServiceMessageFactory());
044: }
045:
046: public void testNoResponse() throws Exception {
047: Method noResponse = getClass().getMethod("noResponse",
048: new Class[] { MyType.class });
049: MethodEndpoint methodEndpoint = new MethodEndpoint(this ,
050: noResponse);
051: unmarshallerMock.unmarshal(messageContext.getRequest()
052: .getPayloadSource());
053: unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
054: unmarshallerControl.setReturnValue(new MyType());
055: marshallerControl.replay();
056: unmarshallerControl.replay();
057: assertFalse("Method invoked", noResponseInvoked);
058: adapter.invoke(messageContext, methodEndpoint);
059: assertTrue("Method not invoked", noResponseInvoked);
060: marshallerControl.verify();
061: unmarshallerControl.verify();
062: }
063:
064: public void testResponse() throws Exception {
065: Method response = getClass().getMethod("response",
066: new Class[] { MyType.class });
067: MethodEndpoint methodEndpoint = new MethodEndpoint(this ,
068: response);
069: unmarshallerMock.unmarshal(messageContext.getRequest()
070: .getPayloadSource());
071: unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
072: unmarshallerControl.setReturnValue(new MyType());
073: marshallerMock.marshal(new MyType(), messageContext
074: .getResponse().getPayloadResult());
075: marshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
076: marshallerControl.replay();
077: unmarshallerControl.replay();
078: assertFalse("Method invoked", responseInvoked);
079: adapter.invoke(messageContext, methodEndpoint);
080: assertTrue("Method not invoked", responseInvoked);
081: marshallerControl.verify();
082: unmarshallerControl.verify();
083:
084: }
085:
086: public void testSupportedNoResponse() throws NoSuchMethodException {
087: Method noResponse = getClass().getMethod("noResponse",
088: new Class[] { MyType.class });
089: MethodEndpoint methodEndpoint = new MethodEndpoint(this ,
090: noResponse);
091: unmarshallerControl.expectAndReturn(unmarshallerMock
092: .supports(MyType.class), true);
093: marshallerControl.replay();
094: unmarshallerControl.replay();
095: assertTrue("Method unsupported", adapter
096: .supportsInternal(methodEndpoint));
097: marshallerControl.verify();
098: unmarshallerControl.verify();
099: }
100:
101: public void testSupportedResponse() throws NoSuchMethodException {
102: Method response = getClass().getMethod("response",
103: new Class[] { MyType.class });
104: MethodEndpoint methodEndpoint = new MethodEndpoint(this ,
105: response);
106: unmarshallerControl.expectAndReturn(unmarshallerMock
107: .supports(MyType.class), true);
108: marshallerControl.expectAndReturn(marshallerMock
109: .supports(MyType.class), true);
110: marshallerControl.replay();
111: unmarshallerControl.replay();
112: assertTrue("Method unsupported", adapter
113: .supportsInternal(methodEndpoint));
114: marshallerControl.verify();
115: unmarshallerControl.verify();
116: }
117:
118: public void testUnsupportedMethodMultipleParams()
119: throws NoSuchMethodException {
120: Method unsupported = getClass().getMethod(
121: "unsupportedMultipleParams",
122: new Class[] { String.class, String.class });
123: marshallerControl.replay();
124: unmarshallerControl.replay();
125: assertFalse("Method supported",
126: adapter.supportsInternal(new MethodEndpoint(this ,
127: unsupported)));
128: marshallerControl.verify();
129: unmarshallerControl.verify();
130: }
131:
132: public void testUnsupportedMethodWrongParam()
133: throws NoSuchMethodException {
134: Method unsupported = getClass().getMethod(
135: "unsupportedWrongParam", new Class[] { String.class });
136: unmarshallerControl.expectAndReturn(unmarshallerMock
137: .supports(String.class), false);
138: marshallerControl.expectAndReturn(marshallerMock
139: .supports(String.class), true);
140: marshallerControl.replay();
141: unmarshallerControl.replay();
142: assertFalse("Method supported",
143: adapter.supportsInternal(new MethodEndpoint(this ,
144: unsupported)));
145: marshallerControl.verify();
146: unmarshallerControl.verify();
147: }
148:
149: public void testUnsupportedMethodWrongReturnType()
150: throws NoSuchMethodException {
151: Method unsupported = getClass().getMethod(
152: "unsupportedWrongParam", new Class[] { String.class });
153: marshallerControl.expectAndReturn(marshallerMock
154: .supports(String.class), false);
155: marshallerControl.replay();
156: unmarshallerControl.replay();
157: assertFalse("Method supported",
158: adapter.supportsInternal(new MethodEndpoint(this ,
159: unsupported)));
160: marshallerControl.verify();
161: unmarshallerControl.verify();
162: }
163:
164: public void noResponse(MyType type) {
165: noResponseInvoked = true;
166:
167: }
168:
169: public MyType response(MyType type) {
170: responseInvoked = true;
171: return new MyType();
172: }
173:
174: public void unsupportedMultipleParams(String s1, String s2) {
175: }
176:
177: public String unsupportedWrongParam(String s) {
178: return s;
179: }
180:
181: public static class MyType {
182:
183: }
184: }
|