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.http; 19 20 import org.apache.log4j.Logger; 21 import org.junit.Test; 22 23 public class TestHttpServerLifecycle extends HttpServerFunctionalTest { 24 25 /** 26 * Check that a server is alive by probing the {@link HttpServer2#isAlive()} method 27 * and the text of its toString() description 28 * @param server server 29 */ assertAlive(HttpServer2 server)30 private void assertAlive(HttpServer2 server) { 31 assertTrue("Server is not alive", server.isAlive()); 32 assertToStringContains(server, HttpServer2.STATE_DESCRIPTION_ALIVE); 33 } 34 assertNotLive(HttpServer2 server)35 private void assertNotLive(HttpServer2 server) { 36 assertTrue("Server should not be live", !server.isAlive()); 37 assertToStringContains(server, HttpServer2.STATE_DESCRIPTION_NOT_LIVE); 38 } 39 40 /** 41 * Test that the server is alive once started 42 * 43 * @throws Throwable on failure 44 */ testCreatedServerIsNotAlive()45 @Test public void testCreatedServerIsNotAlive() throws Throwable { 46 HttpServer2 server = createTestServer(); 47 assertNotLive(server); 48 } 49 testStopUnstartedServer()50 @Test public void testStopUnstartedServer() throws Throwable { 51 HttpServer2 server = createTestServer(); 52 stop(server); 53 } 54 55 /** 56 * Test that the server is alive once started 57 * 58 * @throws Throwable on failure 59 */ 60 @Test testStartedServerIsAlive()61 public void testStartedServerIsAlive() throws Throwable { 62 HttpServer2 server = null; 63 server = createTestServer(); 64 assertNotLive(server); 65 server.start(); 66 assertAlive(server); 67 stop(server); 68 } 69 70 /** 71 * Test that the server with request logging enabled 72 * 73 * @throws Throwable on failure 74 */ 75 @Test testStartedServerWithRequestLog()76 public void testStartedServerWithRequestLog() throws Throwable { 77 HttpRequestLogAppender requestLogAppender = new HttpRequestLogAppender(); 78 requestLogAppender.setName("httprequestlog"); 79 requestLogAppender.setFilename(System.getProperty("test.build.data", "/tmp/") 80 + "jetty-name-yyyy_mm_dd.log"); 81 Logger.getLogger(HttpServer2.class.getName() + ".test").addAppender(requestLogAppender); 82 HttpServer2 server = null; 83 server = createTestServer(); 84 assertNotLive(server); 85 server.start(); 86 assertAlive(server); 87 stop(server); 88 Logger.getLogger(HttpServer2.class.getName() + ".test").removeAppender(requestLogAppender); 89 } 90 91 /** 92 * Assert that the result of {@link HttpServer2#toString()} contains the specific text 93 * @param server server to examine 94 * @param text text to search for 95 */ assertToStringContains(HttpServer2 server, String text)96 private void assertToStringContains(HttpServer2 server, String text) { 97 String description = server.toString(); 98 assertTrue("Did not find \"" + text + "\" in \"" + description + "\"", 99 description.contains(text)); 100 } 101 102 /** 103 * Test that the server is not alive once stopped 104 * 105 * @throws Throwable on failure 106 */ testStoppedServerIsNotAlive()107 @Test public void testStoppedServerIsNotAlive() throws Throwable { 108 HttpServer2 server = createAndStartTestServer(); 109 assertAlive(server); 110 stop(server); 111 assertNotLive(server); 112 } 113 114 /** 115 * Test that the server is not alive once stopped 116 * 117 * @throws Throwable on failure 118 */ testStoppingTwiceServerIsAllowed()119 @Test public void testStoppingTwiceServerIsAllowed() throws Throwable { 120 HttpServer2 server = createAndStartTestServer(); 121 assertAlive(server); 122 stop(server); 123 assertNotLive(server); 124 stop(server); 125 assertNotLive(server); 126 } 127 128 /** 129 * Test that the server is alive once started 130 * 131 * @throws Throwable 132 * on failure 133 */ 134 @Test testWepAppContextAfterServerStop()135 public void testWepAppContextAfterServerStop() throws Throwable { 136 HttpServer2 server = null; 137 String key = "test.attribute.key"; 138 String value = "test.attribute.value"; 139 server = createTestServer(); 140 assertNotLive(server); 141 server.start(); 142 server.setAttribute(key, value); 143 assertAlive(server); 144 assertEquals(value, server.getAttribute(key)); 145 stop(server); 146 assertNull("Server context should have cleared", server.getAttribute(key)); 147 } 148 } 149