1   /*
2    * Copyright 2009, Josh Devins, Jitr.org.
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.jitr.container;
18  
19  import org.apache.commons.httpclient.HttpClient;
20  import org.apache.commons.httpclient.methods.PostMethod;
21  import org.apache.commons.httpclient.methods.StringRequestEntity;
22  import org.jitr.JitrUtils;
23  import org.jitr.core.ConfigurationModel;
24  import org.jitr.core.Container;
25  import org.jitr.core.Defaults;
26  import org.junit.Assert;
27  import org.junit.BeforeClass;
28  import org.junit.Test;
29  
30  /**
31   * FIXME: Not sure this is the best way to test the various container warpper classes.
32   * 
33   * @author Josh Devins (info@joshdevins.net)
34   */
35  public class ContainerTest {
36  
37      private static int TEST_PORT;
38  
39      private static final String TEST_CONTEXT_PATH = "/TEST_CONTEXT_PATH";
40  
41      private static final String TEST_BASE_URI = "http://localhost:" + TEST_PORT + TEST_CONTEXT_PATH;
42  
43      private static final String TEST_STRING = "TEST_STRING";
44  
45      @Test
46      public void testJettyContainer() throws Exception {
47          testContainer(new JettyContainer());
48      }
49  
50      @Test
51      public void testTomcatContainer() throws Exception {
52          testContainer(new TomcatContainer());
53      }
54  
55      private void testContainer(final Container<?> container) throws Exception {
56  
57          ConfigurationModel config =
58                  new ConfigurationModel(TEST_BASE_URI, container.getClass(),
59                          Defaults.DEFAULT_CONTAINER_WORK_PATH, TEST_CONTEXT_PATH,
60                          Defaults.DEFAULT_MODE, TEST_PORT, Defaults.DEFAULT_WAR_PATH);
61          container.initialize(config, null);
62  
63          container.start();
64  
65          PostMethod postMethod =
66                  new PostMethod("http://localhost:" + TEST_PORT + TEST_CONTEXT_PATH + "/string/echo");
67          postMethod.setRequestEntity(new StringRequestEntity(TEST_STRING, "text/plain", "UTF-8"));
68  
69          HttpClient httpClient = new HttpClient();
70          httpClient.executeMethod(postMethod);
71  
72          String body = new String(postMethod.getResponseBody());
73          Assert.assertEquals(TEST_STRING, body);
74  
75          container.stop();
76  
77          // TODO: Test that the container has actually stopped.
78      }
79  
80      @BeforeClass
81      public static void beforeClass() throws Exception {
82          TEST_PORT = JitrUtils.getRandomUnusedPort();
83      }
84  }