001: /*
002: * Copyright 2007 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.endpoint.adapter;
018:
019: import java.util.Properties;
020: import javax.xml.parsers.DocumentBuilder;
021: import javax.xml.parsers.DocumentBuilderFactory;
022: import javax.xml.transform.Source;
023: import javax.xml.transform.dom.DOMSource;
024:
025: import junit.framework.TestCase;
026: import static org.easymock.EasyMock.*;
027: import org.springframework.ws.WebServiceMessage;
028: import org.springframework.ws.WebServiceMessageFactory;
029: import org.springframework.ws.context.DefaultMessageContext;
030: import org.springframework.ws.context.MessageContext;
031: import org.springframework.ws.server.endpoint.MethodEndpoint;
032: import org.springframework.ws.server.endpoint.annotation.XPathParam;
033: import org.springframework.xml.transform.StringResult;
034: import org.springframework.xml.transform.StringSource;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039: import org.w3c.dom.Text;
040:
041: public class XPathParamAnnotationMethodEndpointAdapterTest extends
042: TestCase {
043:
044: private static final String CONTENTS = "<root><child><text>text</text><number>42.0</number></child></root>";
045:
046: private XPathParamAnnotationMethodEndpointAdapter adapter;
047:
048: private boolean supportedTypesInvoked = false;
049:
050: private boolean supportedSourceInvoked;
051:
052: private boolean namespacesInvoked;
053:
054: protected void setUp() throws Exception {
055: adapter = new XPathParamAnnotationMethodEndpointAdapter();
056: adapter.afterPropertiesSet();
057: }
058:
059: public void testUnsupportedInvalidParam()
060: throws NoSuchMethodException {
061: MethodEndpoint endpoint = new MethodEndpoint(this ,
062: "unsupportedInvalidParamType",
063: new Class[] { Integer.TYPE });
064: assertFalse("Method supported", adapter.supports(endpoint));
065: }
066:
067: public void testUnsupportedInvalidReturnType()
068: throws NoSuchMethodException {
069: MethodEndpoint endpoint = new MethodEndpoint(this ,
070: "unsupportedInvalidReturnType",
071: new Class[] { String.class });
072: assertFalse("Method supported", adapter.supports(endpoint));
073: }
074:
075: public void testUnsupportedInvalidParams()
076: throws NoSuchMethodException {
077: MethodEndpoint endpoint = new MethodEndpoint(this ,
078: "unsupportedInvalidParams", new Class[] { String.class,
079: String.class });
080: assertFalse("Method supported", adapter.supports(endpoint));
081: }
082:
083: public void testSupportedTypes() throws NoSuchMethodException {
084: MethodEndpoint endpoint = new MethodEndpoint(this ,
085: "supportedTypes", new Class[] { Boolean.TYPE,
086: Double.TYPE, Node.class, NodeList.class,
087: String.class });
088: assertTrue("Not all types supported", adapter
089: .supports(endpoint));
090: }
091:
092: public void testSupportsStringSource() throws NoSuchMethodException {
093: MethodEndpoint endpoint = new MethodEndpoint(this ,
094: "supportedStringSource", new Class[] { String.class });
095: assertTrue("StringSource method not supported", adapter
096: .supports(endpoint));
097: }
098:
099: public void testSupportsSource() throws NoSuchMethodException {
100: MethodEndpoint endpoint = new MethodEndpoint(this ,
101: "supportedSource", new Class[] { String.class });
102: assertTrue("Source method not supported", adapter
103: .supports(endpoint));
104: }
105:
106: public void testSupportsVoid() throws NoSuchMethodException {
107: MethodEndpoint endpoint = new MethodEndpoint(this ,
108: "supportedVoid", new Class[] { String.class });
109: assertTrue("void method not supported", adapter
110: .supports(endpoint));
111: }
112:
113: public void testInvokeTypes() throws Exception {
114: WebServiceMessage messageMock = createMock(WebServiceMessage.class);
115: expect(messageMock.getPayloadSource()).andReturn(
116: new StringSource(CONTENTS));
117: WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
118: replay(messageMock, factoryMock);
119:
120: MessageContext messageContext = new DefaultMessageContext(
121: messageMock, factoryMock);
122: MethodEndpoint endpoint = new MethodEndpoint(this ,
123: "supportedTypes", new Class[] { Boolean.TYPE,
124: Double.TYPE, Node.class, NodeList.class,
125: String.class });
126: adapter.invoke(messageContext, endpoint);
127: assertTrue("Method not invoked", supportedTypesInvoked);
128:
129: verify(messageMock, factoryMock);
130: }
131:
132: public void testInvokeSource() throws Exception {
133: WebServiceMessage requestMock = createMock(WebServiceMessage.class);
134: WebServiceMessage responseMock = createMock(WebServiceMessage.class);
135: expect(requestMock.getPayloadSource()).andReturn(
136: new StringSource(CONTENTS));
137: expect(responseMock.getPayloadResult()).andReturn(
138: new StringResult());
139: WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
140: expect(factoryMock.createWebServiceMessage()).andReturn(
141: responseMock);
142: replay(requestMock, responseMock, factoryMock);
143:
144: MessageContext messageContext = new DefaultMessageContext(
145: requestMock, factoryMock);
146: MethodEndpoint endpoint = new MethodEndpoint(this ,
147: "supportedSource", new Class[] { String.class });
148: adapter.invoke(messageContext, endpoint);
149: assertTrue("Method not invoked", supportedSourceInvoked);
150:
151: verify(requestMock, responseMock, factoryMock);
152: }
153:
154: public void testInvokeVoidDom() throws Exception {
155: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
156: .newInstance();
157: DocumentBuilder documentBuilder = documentBuilderFactory
158: .newDocumentBuilder();
159: Document document = documentBuilder.newDocument();
160: String rootNamespace = "http://rootnamespace";
161: Element rootElement = document.createElementNS(rootNamespace,
162: "root");
163: document.appendChild(rootElement);
164: String childNamespace = "http://childnamespace";
165: Element first = document.createElementNS(childNamespace,
166: "child");
167: rootElement.appendChild(first);
168: Text text = document.createTextNode("value");
169: first.appendChild(text);
170: Element second = document.createElementNS(rootNamespace,
171: "other-child");
172: rootElement.appendChild(second);
173: text = document.createTextNode("other-value");
174: second.appendChild(text);
175:
176: WebServiceMessage requestMock = createMock(WebServiceMessage.class);
177: expect(requestMock.getPayloadSource()).andReturn(
178: new DOMSource(first));
179: WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
180:
181: replay(requestMock, factoryMock);
182:
183: Properties namespaces = new Properties();
184: namespaces.setProperty("root", rootNamespace);
185: namespaces.setProperty("child", childNamespace);
186: adapter.setNamespaces(namespaces);
187:
188: MessageContext messageContext = new DefaultMessageContext(
189: requestMock, factoryMock);
190: MethodEndpoint endpoint = new MethodEndpoint(this ,
191: "namespaces", new Class[] { Node.class });
192: adapter.invoke(messageContext, endpoint);
193: assertTrue("Method not invoked", namespacesInvoked);
194: }
195:
196: public void supportedVoid(@XPathParam("/")
197: String param1) {
198: }
199:
200: public Source supportedSource(@XPathParam("/")
201: String param1) {
202: supportedSourceInvoked = true;
203: return new StringSource("<response/>");
204: }
205:
206: public StringSource supportedStringSource(@XPathParam("/")
207: String param1) {
208: return null;
209: }
210:
211: public void supportedTypes(@XPathParam("/root/child")
212: boolean param1, @XPathParam("/root/child/number")
213: double param2, @XPathParam("/root/child")
214: Node param3, @XPathParam("/root/*")
215: NodeList param4, @XPathParam("/root/child/text")
216: String param5) {
217: supportedTypesInvoked = true;
218: assertTrue("Invalid boolean value", param1);
219: assertEquals("Invalid double value", 42D, param2, 0.00001D);
220: assertEquals("Invalid Node value", "child", param3
221: .getLocalName());
222: assertEquals("Invalid NodeList value", 1, param4.getLength());
223: assertEquals("Invalid Node value", "child", param4.item(0)
224: .getLocalName());
225: assertEquals("Invalid Node value", "text", param5);
226: }
227:
228: public void unsupportedInvalidParams(@XPathParam("/")
229: String param1, String param2) {
230:
231: }
232:
233: public String unsupportedInvalidReturnType(@XPathParam("/")
234: String param1) {
235: return null;
236: }
237:
238: public void unsupportedInvalidParamType(@XPathParam("/")
239: int param1) {
240: }
241:
242: public void namespaces(@XPathParam(".")
243: Node param) {
244: namespacesInvoked = true;
245: assertEquals("Invalid parameter", "child", param.getLocalName());
246: }
247: }
|