View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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     * Check that a server is alive by probing the {@link HttpServer#isAlive()} method
29     * and the text of its toString() description
30     * @param server server
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     * Test that the server is alive once started
44     *
45     * @throws Throwable on failure
46     */
47    @Test public void testCreatedServerIsNotAlive() throws Throwable {
48      HttpServer server = createTestServer();
49      assertNotLive(server);
50    }
51  
52    @Test public void testStopUnstartedServer() throws Throwable {
53      HttpServer server = createTestServer();
54      stop(server);
55    }
56  
57    /**
58     * Test that the server is alive once started
59     *
60     * @throws Throwable on failure
61     */
62    @Test
63    public void testStartedServerIsAlive() throws Throwable {
64      HttpServer server = null;
65      server = createTestServer();
66      assertNotLive(server);
67      server.start();
68      assertAlive(server);
69      stop(server);
70    }
71  
72    /**
73     * Assert that the result of {@link HttpServer#toString()} contains the specific text
74     * @param server server to examine
75     * @param text text to search for
76     */
77    private void assertToStringContains(HttpServer server, String text) {
78      String description = server.toString();
79      assertTrue("Did not find \"" + text + "\" in \"" + description + "\"",
80                 description.contains(text));
81    }
82  
83    /**
84     * Test that the server is not alive once stopped
85     *
86     * @throws Throwable on failure
87     */
88    @Test public void testStoppedServerIsNotAlive() throws Throwable {
89      HttpServer server = createAndStartTestServer();
90      assertAlive(server);
91      stop(server);
92      assertNotLive(server);
93    }
94  
95    /**
96     * Test that the server is not alive once stopped
97     *
98     * @throws Throwable on failure
99     */
100   @Test public void testStoppingTwiceServerIsAllowed() throws Throwable {
101     HttpServer server = createAndStartTestServer();
102     assertAlive(server);
103     stop(server);
104     assertNotLive(server);
105     stop(server);
106     assertNotLive(server);
107   }
108 
109   /**
110    * Test that the server is alive once started
111    * 
112    * @throws Throwable
113    *           on failure
114    */
115   @Test
116   public void testWepAppContextAfterServerStop() throws Throwable {
117     HttpServer server = null;
118     String key = "test.attribute.key";
119     String value = "test.attribute.value";
120     server = createTestServer();
121     assertNotLive(server);
122     server.start();
123     server.setAttribute(key, value);
124     assertAlive(server);
125     assertEquals(value, server.getAttribute(key));
126     stop(server);
127     assertNull("Server context should have cleared", server.getAttribute(key));
128   }
129 }