add StatViewServlet Test Case.

This commit is contained in:
Zhanming Qi
2012-08-11 17:25:41 +08:00
parent a21ad113c3
commit 7f63e9bffb
2 changed files with 167 additions and 0 deletions
@@ -0,0 +1,116 @@
package com.alibaba.druid.bvt.support.http;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.net.ServerSocket;
import java.util.Map;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.alibaba.druid.VERSION;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;
/**
* Test StatViewServlet' json test.
*
* @author Zhanming
*/
public class StatViewServletTest {
private final static String contextPath = "/";
private final static String servletPatternPrefix = "/druid";
private Server server;
private int port;
@BeforeClass
public static void setUpBeforeClass() throws Exception {}
@AfterClass
public static void tearDownAfterClass() throws Exception {}
@Before
public void setUp() throws Exception {
// get a port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
server.addConnector(connector);
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setWelcomeFiles(new String[] { "index.html" });
resourceHandler.setResourceBase(".");
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setContextPath(contextPath);
// http://localhost:8080/druid/*
contextHandler.addServlet(new ServletHolder(new StatViewServlet()), servletPatternPrefix + "/*");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resourceHandler, contextHandler, new DefaultHandler() });
server.setHandler(handlers);
server.start();
// in test case, should not use join method.
// server.join();
}
@After
public void tearDown() throws Exception {
server.stop();
}
@Test
public void test_basic() throws Exception {
WebConversation wc = new WebConversation();
String url = "http://localhost:" + port + contextPath + servletPatternPrefix + "/basic.json";
WebResponse wr = wc.getResponse(url);
String jsonString = wr.getText();
JSONObject json = JSON.parseObject(jsonString);
int resultCode = json.getInteger("ResultCode");
assertThat(1, equalTo(resultCode));
Map<String, Object> dataMap = json.getJSONObject("Content");
assertThat(VERSION.getVersionNumber(), equalTo(dataMap.get("Version")));
}
//
// public void test_sql() throws Exception {
//
// }
/**
* Config an embedding Jetty Server with XML file. It's the same as setUp().
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Resource statViewServerXml = Resource.newSystemResource("jetty/StatViewServer.xml");
XmlConfiguration configuration = new XmlConfiguration(statViewServerXml.getInputStream());
Server server = (Server) configuration.configure();
server.start();
server.join();
}
}
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="StatViewServer" class="org.eclipse.jetty.server.Server">
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<Set name="port">8080</Set>
</New>
</Arg>
</Call>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerList">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<!-- <Set name="directoriesListed">true</Set> -->
<Set name="welcomeFiles">
<Array type="String">
<Item>index.html</Item>
</Array>
</Set>
<Set name="resourceBase">.</Set>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.servlet.ServletContextHandler">
<Set name="contextPath">/</Set>
<Call name="addServlet">
<Arg>
<New class="org.eclipse.jetty.servlet.ServletHolder">
<Arg>
<New class="com.alibaba.druid.support.http.StatViewServlet"></New>
</Arg>
</New>
</Arg>
<Arg>/druid/*</Arg>
</Call>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.server.handler.DefaultHandler">
</New>
</Item>
</Array>
</Set>
</New>
</Set>
</Configure>