1   /*
2    * Copyright 2008 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 javax.servlet.http.HttpServletResponse;
20  
21  import org.custommonkey.xmlunit.XMLTestCase;
22  
23  import org.springframework.core.io.ClassPathResource;
24  import org.springframework.core.io.Resource;
25  import org.springframework.mock.web.MockHttpServletRequest;
26  import org.springframework.mock.web.MockHttpServletResponse;
27  import org.springframework.util.FileCopyUtils;
28  import org.springframework.xml.xsd.SimpleXsdSchema;
29  
30  public class XsdSchemaHandlerAdapterTest extends XMLTestCase {
31  
32      private XsdSchemaHandlerAdapter adapter;
33  
34      private MockHttpServletRequest request;
35  
36      private MockHttpServletResponse response;
37  
38      protected void setUp() throws Exception {
39          adapter = new XsdSchemaHandlerAdapter();
40          request = new MockHttpServletRequest();
41          response = new MockHttpServletResponse();
42      }
43  
44      public void testGetLastModified() throws Exception {
45          Resource single = new ClassPathResource("single.xsd", getClass());
46          SimpleXsdSchema schema = new SimpleXsdSchema(single);
47          schema.afterPropertiesSet();
48          long lastModified = single.getFile().lastModified();
49          assertEquals("Invalid last modified", lastModified, adapter.getLastModified(null, schema));
50      }
51  
52      public void testHandleGet() throws Exception {
53          request.setMethod(HttpTransportConstants.METHOD_GET);
54          Resource single = new ClassPathResource("single.xsd", getClass());
55          SimpleXsdSchema schema = new SimpleXsdSchema(single);
56          schema.afterPropertiesSet();
57          adapter.handle(request, response, schema);
58          String expected = new String(FileCopyUtils.copyToByteArray(single.getFile()));
59          assertXMLEqual(expected, response.getContentAsString());
60      }
61  
62      public void testHandleNonGet() throws Exception {
63          request.setMethod(HttpTransportConstants.METHOD_POST);
64          adapter.handle(request, response, null);
65          assertEquals("METHOD_NOT_ALLOWED expected", HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getStatus());
66      }
67  }