1   /*
2    * Copyright 2005-2012 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.transport.http;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.InputStream;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.xml.parsers.DocumentBuilder;
23  import javax.xml.parsers.DocumentBuilderFactory;
24  
25  import org.springframework.core.io.ClassPathResource;
26  import org.springframework.core.io.Resource;
27  import org.springframework.mock.web.MockHttpServletRequest;
28  import org.springframework.mock.web.MockHttpServletResponse;
29  import org.springframework.util.FileCopyUtils;
30  import org.springframework.xml.xsd.SimpleXsdSchema;
31  
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.w3c.dom.Document;
35  
36  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
37  import static org.junit.Assert.assertEquals;
38  
39  public class XsdSchemaHandlerAdapterTest {
40  
41      private XsdSchemaHandlerAdapter adapter;
42  
43      private MockHttpServletRequest request;
44  
45      private MockHttpServletResponse response;
46  
47      @Before
48      public void setUp() throws Exception {
49          adapter = new XsdSchemaHandlerAdapter();
50          adapter.afterPropertiesSet();
51          request = new MockHttpServletRequest();
52          response = new MockHttpServletResponse();
53      }
54  
55      @Test
56      public void getLastModified() throws Exception {
57          Resource single = new ClassPathResource("single.xsd", getClass());
58          SimpleXsdSchema schema = new SimpleXsdSchema(single);
59          schema.afterPropertiesSet();
60          long lastModified = single.getFile().lastModified();
61          assertEquals("Invalid last modified", lastModified, adapter.getLastModified(null, schema));
62      }
63  
64      @Test
65      public void handleGet() throws Exception {
66          request.setMethod(HttpTransportConstants.METHOD_GET);
67          Resource single = new ClassPathResource("single.xsd", getClass());
68          SimpleXsdSchema schema = new SimpleXsdSchema(single);
69          schema.afterPropertiesSet();
70          adapter.handle(request, response, schema);
71          String expected = new String(FileCopyUtils.copyToByteArray(single.getFile()));
72          assertXMLEqual(expected, response.getContentAsString());
73      }
74  
75      @Test
76      public void handleNonGet() throws Exception {
77          request.setMethod(HttpTransportConstants.METHOD_POST);
78          adapter.handle(request, response, null);
79          assertEquals("METHOD_NOT_ALLOWED expected", HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getStatus());
80      }
81  
82      @Test
83      public void handleGetWithTransformLocation() throws Exception {
84          adapter.setTransformSchemaLocations(true);
85  
86          request.setMethod(HttpTransportConstants.METHOD_GET);
87          request.setScheme("http");
88          request.setServerName("example.com");
89          request.setServerPort(80);
90          request.setContextPath("/context");
91          request.setServletPath("/service.xsd");
92          request.setPathInfo(null);
93          request.setRequestURI("/context/service.xsd");
94  
95          Resource importing = new ClassPathResource("importing-input.xsd", getClass());
96          SimpleXsdSchema schema = new SimpleXsdSchema(importing);
97          schema.afterPropertiesSet();
98  
99          adapter.handle(request, response, schema);
100 
101         InputStream inputStream = new ByteArrayInputStream(response.getContentAsByteArray());
102         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
103         documentBuilderFactory.setNamespaceAware(true);
104         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
105         Document resultingDocument = documentBuilder.parse(inputStream);
106 
107         documentBuilder = documentBuilderFactory.newDocumentBuilder();
108         Document expectedDocument = documentBuilder.parse(getClass().getResourceAsStream("importing-expected.xsd"));
109         assertXMLEqual("Invalid WSDL returned", expectedDocument, resultingDocument);
110     }
111 
112 }