1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.http;
19
20 import org.apache.hadoop.hbase.testclassification.SmallTests;
21 import org.junit.Test;
22 import org.junit.experimental.categories.Category;
23
24 @Category(SmallTests.class)
25 public class TestHttpServerLifecycle extends HttpServerFunctionalTest {
26
27
28
29
30
31
32 private void assertAlive(HttpServer server) {
33 assertTrue("Server is not alive", server.isAlive());
34 assertToStringContains(server, HttpServer.STATE_DESCRIPTION_ALIVE);
35 }
36
37 private void assertNotLive(HttpServer server) {
38 assertTrue("Server should not be live", !server.isAlive());
39 assertToStringContains(server, HttpServer.STATE_DESCRIPTION_NOT_LIVE);
40 }
41
42
43
44
45
46
47 @Test(timeout=60000)
48 public void testCreatedServerIsNotAlive() throws Throwable {
49 HttpServer server = createTestServer();
50 assertNotLive(server);
51 }
52
53 @Test(timeout=60000)
54 public void testStopUnstartedServer() throws Throwable {
55 HttpServer server = createTestServer();
56 stop(server);
57 }
58
59
60
61
62
63
64 @Test(timeout=60000)
65 public void testStartedServerIsAlive() throws Throwable {
66 HttpServer server = null;
67 server = createTestServer();
68 assertNotLive(server);
69 server.start();
70 assertAlive(server);
71 stop(server);
72 }
73
74
75
76
77
78
79 private void assertToStringContains(HttpServer server, String text) {
80 String description = server.toString();
81 assertTrue("Did not find \"" + text + "\" in \"" + description + "\"",
82 description.contains(text));
83 }
84
85
86
87
88
89
90 @Test(timeout=60000)
91 public void testStoppedServerIsNotAlive() throws Throwable {
92 HttpServer server = createAndStartTestServer();
93 assertAlive(server);
94 stop(server);
95 assertNotLive(server);
96 }
97
98
99
100
101
102
103 @Test(timeout=60000)
104 public void testStoppingTwiceServerIsAllowed() throws Throwable {
105 HttpServer server = createAndStartTestServer();
106 assertAlive(server);
107 stop(server);
108 assertNotLive(server);
109 stop(server);
110 assertNotLive(server);
111 }
112
113
114
115
116
117
118
119 @Test(timeout=60000)
120 public void testWepAppContextAfterServerStop() throws Throwable {
121 HttpServer server = null;
122 String key = "test.attribute.key";
123 String value = "test.attribute.value";
124 server = createTestServer();
125 assertNotLive(server);
126 server.start();
127 server.setAttribute(key, value);
128 assertAlive(server);
129 assertEquals(value, server.getAttribute(key));
130 stop(server);
131 assertNull("Server context should have cleared", server.getAttribute(key));
132 }
133 }