HBASE-11404 TestLogLevel should stop the server at the end

This commit is contained in:
Jimmy Xiang 2014-06-23 09:25:22 -07:00
parent 3020842d5c
commit 54a5375710
1 changed files with 45 additions and 32 deletions

View File

@ -17,22 +17,27 @@
*/ */
package org.apache.hadoop.hbase.http.log; package org.apache.hadoop.hbase.http.log;
import static org.junit.Assert.assertTrue;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import org.apache.hadoop.hbase.SmallTests;
import org.apache.hadoop.hbase.http.HttpServer; import org.apache.hadoop.hbase.http.HttpServer;
import org.apache.hadoop.hbase.http.log.LogLevel; import org.apache.hadoop.hbase.http.log.LogLevel;
import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.net.NetUtils;
import junit.framework.TestCase;
import org.apache.commons.logging.*; import org.apache.commons.logging.*;
import org.apache.commons.logging.impl.*; import org.apache.commons.logging.impl.*;
import org.apache.log4j.*; import org.apache.log4j.*;
import org.junit.Test;
import org.junit.experimental.categories.Category;
public class TestLogLevel extends TestCase { @Category(SmallTests.class)
public class TestLogLevel {
static final PrintStream out = System.out; static final PrintStream out = System.out;
@Test (timeout=60000)
@SuppressWarnings("deprecation")
public void testDynamicLogLevel() throws Exception { public void testDynamicLogLevel() throws Exception {
String logName = TestLogLevel.class.getName(); String logName = TestLogLevel.class.getName();
Log testlog = LogFactory.getLog(logName); Log testlog = LogFactory.getLog(logName);
@ -45,38 +50,46 @@ public class TestLogLevel extends TestCase {
log.error("log.error1"); log.error("log.error1");
assertTrue(!Level.ERROR.equals(log.getEffectiveLevel())); assertTrue(!Level.ERROR.equals(log.getEffectiveLevel()));
HttpServer server = new HttpServer.Builder().setName("..") HttpServer server = null;
.addEndpoint(new URI("http://localhost:0")).setFindPort(true) try {
.build(); server = new HttpServer.Builder().setName("..")
.addEndpoint(new URI("http://localhost:0")).setFindPort(true)
.build();
server.start(); server.start();
String authority = NetUtils.getHostPortString(server String authority = NetUtils.getHostPortString(server
.getConnectorAddress(0)); .getConnectorAddress(0));
//servlet //servlet
URL url = new URL("http://" + authority + "/logLevel?log=" + logName URL url = new URL("http://" + authority + "/logLevel?log=" + logName
+ "&level=" + Level.ERROR); + "&level=" + Level.ERROR);
out.println("*** Connecting to " + url); out.println("*** Connecting to " + url);
URLConnection connection = url.openConnection(); HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect(); connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader( BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream())); connection.getInputStream()));
for(String line; (line = in.readLine()) != null; out.println(line)); for(String line; (line = in.readLine()) != null; out.println(line));
in.close(); in.close();
connection.disconnect();
log.debug("log.debug2"); log.debug("log.debug2");
log.info("log.info2"); log.info("log.info2");
log.error("log.error2"); log.error("log.error2");
assertTrue(Level.ERROR.equals(log.getEffectiveLevel())); assertTrue(Level.ERROR.equals(log.getEffectiveLevel()));
//command line //command line
String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()}; String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()};
LogLevel.main(args); LogLevel.main(args);
log.debug("log.debug3"); log.debug("log.debug3");
log.info("log.info3"); log.info("log.info3");
log.error("log.error3"); log.error("log.error3");
assertTrue(Level.DEBUG.equals(log.getEffectiveLevel())); assertTrue(Level.DEBUG.equals(log.getEffectiveLevel()));
} finally {
if (server != null) {
server.stop();
}
}
} }
else { else {
out.println(testlog.getClass() + " not tested."); out.println(testlog.getClass() + " not tested.");