1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.server.endpoint.adapter.method.dom;
18  
19  import javax.xml.parsers.DocumentBuilder;
20  import javax.xml.parsers.DocumentBuilderFactory;
21  import javax.xml.parsers.ParserConfigurationException;
22  
23  import org.springframework.core.MethodParameter;
24  import org.springframework.ws.server.endpoint.adapter.method.AbstractPayloadMethodProcessorTestCase;
25  import org.springframework.ws.server.endpoint.adapter.method.AbstractPayloadSourceMethodProcessor;
26  import org.springframework.ws.server.endpoint.annotation.RequestPayload;
27  import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
28  
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  public class DomPayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTestCase {
36  
37      @Override
38      protected AbstractPayloadSourceMethodProcessor createProcessor() {
39          return new DomPayloadMethodProcessor();
40      }
41  
42      @Override
43      protected MethodParameter[] createSupportedParameters() throws NoSuchMethodException {
44          return new MethodParameter[]{
45                  new MethodParameter(getClass().getMethod("element", Element.class), 0)};
46      }
47  
48      @Override
49      protected MethodParameter[] createSupportedReturnTypes() throws NoSuchMethodException {
50          return new MethodParameter[]{new MethodParameter(getClass().getMethod("element", Element.class), -1)};
51      }
52  
53      @Override
54      protected void testArgument(Object argument, MethodParameter parameter) {
55          assertTrue("argument not a element", argument instanceof Element);
56          Element element = (Element) argument;
57          assertEquals("Invalid namespace", NAMESPACE_URI, element.getNamespaceURI());
58          assertEquals("Invalid local name", LOCAL_NAME, element.getLocalName());
59      }
60  
61      @Override
62      protected Element getReturnValue(MethodParameter returnType) throws ParserConfigurationException {
63          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
64          DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
65          Document document = documentBuilder.newDocument();
66          return document.createElementNS(NAMESPACE_URI, LOCAL_NAME);
67      }
68  
69      @ResponsePayload
70      public Element element(@RequestPayload Element element) {
71          return element;
72      }
73  
74  }