001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http;
019
020import org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.testclassification.MiscTests;
022import org.apache.hadoop.hbase.testclassification.SmallTests;
023import org.junit.ClassRule;
024import org.junit.Ignore;
025import org.junit.Test;
026import org.junit.experimental.categories.Category;
027
028@Category({MiscTests.class, SmallTests.class})
029public class TestHttpServerLifecycle extends HttpServerFunctionalTest {
030
031  @ClassRule
032  public static final HBaseClassTestRule CLASS_RULE =
033      HBaseClassTestRule.forClass(TestHttpServerLifecycle.class);
034
035  /**
036   * Check that a server is alive by probing the {@link HttpServer#isAlive()} method
037   * and the text of its toString() description
038   * @param server server
039   */
040  private void assertAlive(HttpServer server) {
041    assertTrue("Server is not alive", server.isAlive());
042    assertToStringContains(server, HttpServer.STATE_DESCRIPTION_ALIVE);
043  }
044
045  private void assertNotLive(HttpServer server) {
046    assertTrue("Server should not be live", !server.isAlive());
047    assertToStringContains(server, HttpServer.STATE_DESCRIPTION_NOT_LIVE);
048  }
049
050  /**
051   * Test that the server is alive once started
052   *
053   * @throws Throwable on failure
054   */
055  @Ignore ("Hangs on occasion; see HBASE-14430") @Test
056  public void testCreatedServerIsNotAlive() throws Throwable {
057    HttpServer server = createTestServer();
058    assertNotLive(server);
059  }
060
061  @Ignore ("Hangs on occasion; see HBASE-14430") @Test
062  public void testStopUnstartedServer() throws Throwable {
063    HttpServer server = createTestServer();
064    stop(server);
065  }
066
067  /**
068   * Test that the server is alive once started
069   *
070   * @throws Throwable on failure
071   */
072  @Ignore ("Hangs on occasion; see HBASE-14430") @Test
073  public void testStartedServerIsAlive() throws Throwable {
074    HttpServer server = null;
075    server = createTestServer();
076    assertNotLive(server);
077    server.start();
078    assertAlive(server);
079    stop(server);
080  }
081
082  /**
083   * Assert that the result of {@link HttpServer#toString()} contains the specific text
084   * @param server server to examine
085   * @param text text to search for
086   */
087  private void assertToStringContains(HttpServer server, String text) {
088    String description = server.toString();
089    assertTrue("Did not find \"" + text + "\" in \"" + description + "\"",
090               description.contains(text));
091  }
092
093  /**
094   * Test that the server is not alive once stopped
095   *
096   * @throws Throwable on failure
097   */
098  @Ignore ("Hangs on occasion; see HBASE-14430") @Test
099  public void testStoppedServerIsNotAlive() throws Throwable {
100    HttpServer server = createAndStartTestServer();
101    assertAlive(server);
102    stop(server);
103    assertNotLive(server);
104  }
105
106  /**
107   * Test that the server is not alive once stopped
108   *
109   * @throws Throwable on failure
110   */
111  @Ignore ("Hangs on occasion; see HBASE-14430") @Test
112  public void testStoppingTwiceServerIsAllowed() throws Throwable {
113    HttpServer server = createAndStartTestServer();
114    assertAlive(server);
115    stop(server);
116    assertNotLive(server);
117    stop(server);
118    assertNotLive(server);
119  }
120
121  /**
122   * Test that the server is alive once started
123   *
124   * @throws Throwable
125   *           on failure
126   */
127  @Ignore ("Hangs on occasion; see HBASE-14430") @Test
128  public void testWepAppContextAfterServerStop() throws Throwable {
129    HttpServer server = null;
130    String key = "test.attribute.key";
131    String value = "test.attribute.value";
132    server = createTestServer();
133    assertNotLive(server);
134    server.start();
135    server.setAttribute(key, value);
136    assertAlive(server);
137    assertEquals(value, server.getAttribute(key));
138    stop(server);
139    assertNull("Server context should have cleared", server.getAttribute(key));
140  }
141}