Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target
/build
/.classpath
/.settings
/.project
176 changes: 176 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gruter</groupId>
<artifactId>gruter-common</artifactId>
<version>0.9.0</version>

<properties>
<jetty.version>6.1.24</jetty.version>
<slf4j.version>1.6.0</slf4j.version>
<log4j.version>1.2.14</log4j.version>
<hadoop.version>0.21.0</hadoop.version>
<zookeeper.version>3.3.1</zookeeper.version>
<thrift.version>0.6.0</thrift.version>
<junit.version>4.8</junit.version>
</properties>

<developers>
<developer>
<name>babokim</name>
<organization>gruter</organization>
</developer>
</developers>

<build>
<directory>build</directory>
<sourceDirectory>src/java</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<resources>
<resource>
<directory>src/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- /test -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>1.4.1</version>
<exclusions>
<exclusion>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>thrift</artifactId>
<version>${thrift.version}</version>
<scope>system</scope>
<systemPath>${basedir}/lib/libthrift-${thrift.version}.jar</systemPath>
</dependency>
<!-- hadoop -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
<optional>true</optional>
<scope>system</scope>
<systemPath>${basedir}/lib/hadoop-common-${hadoop.version}.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
<optional>true</optional>
<scope>system</scope>
<systemPath>${basedir}/lib/hadoop-hdfs-${hadoop.version}.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapred</artifactId>
<version>${hadoop.version}</version>
<optional>true</optional>
<scope>system</scope>
<systemPath>${basedir}/lib/hadoop-mapred-${hadoop.version}.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapred-tools</artifactId>
<version>${hadoop.version}</version>
<optional>true</optional>
<scope>system</scope>
<systemPath>${basedir}/lib/hadoop-mapred-tools-${hadoop.version}.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
212 changes: 212 additions & 0 deletions src/test/com/gruter/common/http/CommonHttpServerTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package com.gruter.common.http;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* @author Daegeun Kim
*/
public abstract class CommonHttpServerTestCase implements FilterContainer {
private static final String DEFAULT_WEB_CONTEXT_PATH = "default_context_path";
private static final String DEFAULT_HOSTNAME = "127.0.0.1";
private static final int DEFAULT_PORT = 8100;

private String webContextPath;
protected String hostname;
protected int port;
protected CommonHttpServer server;
private boolean running;

protected CommonHttpServerTestCase() {
this(DEFAULT_WEB_CONTEXT_PATH, DEFAULT_HOSTNAME, DEFAULT_PORT);
}

protected CommonHttpServerTestCase(String webContextPath) {
this(webContextPath, DEFAULT_HOSTNAME, DEFAULT_PORT);
}

protected CommonHttpServerTestCase(String webContextPath, String hostname, int port) {
this.webContextPath = webContextPath;
this.hostname = hostname;
this.port = port;
}

protected void initServer() throws IOException {
if (server == null) {
server = new CommonHttpServer(getWebContextPath(), getHostname(), getPort(), false);
}
}

/**
* 서블릿 추가
* @param name
* @param pathSpec
* @param clazz
*/
public void addServlet(String name, String pathSpec, Class<? extends HttpServlet> clazz) {
server.addServlet(name, pathSpec, clazz);
}

/* (non-Javadoc)
* @see com.gruter.common.http.FilterContainer#addFilter(java.lang.String, java.lang.String, java.util.Map)
*/
public void addFilter(String name, String classname, Map<String, String> parameters) {
server.addFilter(name, classname, parameters);
}

/* (non-Javadoc)
* @see com.gruter.common.http.FilterContainer#addGlobalFilter(java.lang.String, java.lang.String, java.util.Map)
*/
public void addGlobalFilter(String name, String classname, Map<String, String> parameters) {
server.addGlobalFilter(name, classname, parameters);
}

/**
* 지정한 hostname 과 port 를 기반으로 서버를 구동한다.
* @throws IOException
*/
protected void runServer() throws IOException {
if (running == false && server == null) {
initServer();
}
if (running == false) {
server.start();
running = true;
}
}

/**
* 구동된 서버를 중지시킨다.
* @throws Exception
*/
protected void stopServer() throws Exception {
if (server == null) {
throw new Exception("server is not running");
}
server.stop();
}

@SuppressWarnings("unchecked")
protected <P extends Page> P requestToServer(String method, String path) throws Exception {
if (isRunning() == false) {
runServer();
}

DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest request = invokeMethod(method, getPageURI(path));
Page page = new Page(new WebResponse(client.execute(request)));
return (P) page;
/*
// HTMLUnit 으로 작성했다가 필요한 Library 가 워낙 많아서 주석 처리하고 httpcomponent 4.x 으로 변경하고 하위에 inner class 만들었습니다.
// maven 을 사용하면 test 용 library 는 제외시키기 편한데 ant 는 건드려야 할 것이 많아서 급 수정
WebClient client = new WebClient();
WebRequest request = new WebRequest(new URL(getPageURI(path)), HttpMethod.valueOf(method.toUpperCase()));
try {
return (P) client.getPage(request);
} catch (FailingHttpStatusCodeException e) {
Page page = new HtmlPage(request.getUrl(), e.getResponse(), client.getCurrentWindow());
return (P) page;
} catch (MalformedURLException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
client.closeAllWindows();
}
*/
}

private HttpUriRequest invokeMethod(String method, String page) {
Map<String, Class<? extends HttpUriRequest>> methods = new HashMap<String, Class<? extends HttpUriRequest>>();
methods.put("GET", HttpGet.class);
methods.put("PUT", HttpPut.class);
methods.put("POST", HttpPost.class);
methods.put("OPTIONS", HttpOptions.class);
methods.put("HEAD", HttpHead.class);
methods.put("TRACE", HttpTrace.class);
methods.put("DELETE", HttpDelete.class);
try {
return methods.get(method.toUpperCase()).getConstructor(String.class).newInstance(page);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private boolean isRunning() {
return running;
}

/**
* 테스트에 사용할 URL 문자열을 만든다.
* @param path
* @return
*/
protected String getPageURI(String path) {
return new StringBuilder("http://").append(getAuthority()).append(path).toString();
}

protected String getAuthority() {
return getPort() == 80 ? getHostname() : getHostname() + ":" + getPort();
}

protected String getWebContextPath() {
return webContextPath;
}

protected String getHostname() {
return hostname;
}

protected int getPort() {
return port;
}


/**
* HtmlUnit 의 Page class 를 코드변경없이 사용하고자 동일한 이름의 클래스로 필요한 것만 작성
*/
protected static class Page {
WebResponse response;

Page(WebResponse response) {
this.response = response;
}

public WebResponse getWebResponse() {
return response;
}
}

/**
* HtmlUnit 의 WebResponse class 를 코드변경없이 사용하고자 동일한 이름의 클래스로 필요한 것만 작성
*/
protected static class WebResponse {
HttpResponse response;

WebResponse(HttpResponse response) {
this.response = response;
}

public String getResponseHeaderValue(String name) {
return response.getFirstHeader(name).getValue();
}

public int getStatusCode() {
return response.getStatusLine().getStatusCode();
}
}
}
Loading