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(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     * Test that the server is alive once started
61     *
62     * @throws Throwable on failure
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     * Assert that the result of {@link HttpServer#toString()} contains the specific text
76     * @param server server to examine
77     * @param text text to search for
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     * Test that the server is not alive once stopped
87     *
88     * @throws Throwable on failure
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     * Test that the server is not alive once stopped
100    *
101    * @throws Throwable on failure
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    * Test that the server is alive once started
115    *
116    * @throws Throwable
117    *           on failure
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 }