1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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
78 }
79
80 @BeforeClass
81 public static void beforeClass() throws Exception {
82 TEST_PORT = JitrUtils.getRandomUnusedPort();
83 }
84 }