View Javadoc
1   /*
2    * Copyright 2009 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  package org.openehealth.ipf.commons.ihe.ws.server;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.apache.http.HttpResponse;
20  import org.apache.http.client.HttpClient;
21  import org.apache.http.client.methods.HttpPost;
22  import org.apache.http.entity.StringEntity;
23  import org.apache.http.impl.client.HttpClients;
24  import org.junit.Test;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import java.io.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.net.URL;
33  
34  import static org.junit.Assert.assertEquals;
35  
36  /**
37   * @author Jens Riemschneider
38   */
39  public class TestServers {
40      /**
41       * Tests the embedded Tomcat server
42       * @throws Exception
43       *          for anything problematic.
44       */
45      @Test
46      public void testTomcat() throws Exception {
47          checkServer(new TomcatServer(), 9090);
48      }
49  
50      /**
51       * Tests the embedded Jetty server
52       * @throws Exception
53       *          for anything problematic.
54       */
55      @Test
56      public void testJetty() throws Exception {
57          checkServer(new JettyServer(), 9091);
58      }
59  
60      private void checkServer(ServletServer server, int port) throws Exception {
61          URL contextResource = getClass().getResource("/test.xml");
62  
63          server.setServlet(new Servlet());
64          server.setPort(port);
65          server.setContextPath("/testContext");
66          server.setServletPath("/testServlet/*");
67          server.setContextResource(contextResource.toURI().toString());
68          server.start();
69          checkPostRequest(port);
70          server.stop();
71      }
72  
73      private void checkPostRequest(int port) throws Exception {
74          HttpClient client = HttpClients.createDefault();
75          HttpPost method = new HttpPost("http://localhost:" + port + "/testContext/testServlet/bla");
76          method.setEntity(new StringEntity("hello world"));
77          try {
78              ByteArrayOutputStream stream = new ByteArrayOutputStream();
79              HttpResponse response = client.execute(method);
80              response.getEntity().writeTo(stream);
81              assertEquals(200, response.getStatusLine().getStatusCode());
82              assertEquals("hello world", new String(stream.toByteArray()));
83          }
84          finally {
85              method.releaseConnection();
86          }
87      }
88  
89      /** Simple test servlet class */
90      public static class Servlet extends HttpServlet {
91          private static final long serialVersionUID = 1L;
92  
93          @Override
94          protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
95              IOUtils.copy(request.getInputStream(), response.getOutputStream());
96          }
97      }
98  }