tests: convert more jetty/example tests to junit4

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@927644 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-03-25 23:55:14 +00:00
parent cec08f1741
commit 604c7c220e
3 changed files with 41 additions and 47 deletions

View File

@ -26,20 +26,26 @@ import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.util.DateUtil;
import org.junit.BeforeClass;
import org.junit.Test;
import static junit.framework.Assert.*;
/**
* A test case for the several HTTP cache headers emitted by Solr
*/
public class CacheHeaderTest extends CacheHeaderTestBase {
@Override
public String getSolrConfigFilename() {
return "solrconfig.xml";
@BeforeClass
public static void beforeTest() throws Exception {
createJetty("solr/", null, null);
}
protected static final String CHARSET = "UTF-8";
protected static final String CONTENTS = "id\n100\n101\n102";
@Test
public void testCacheVetoHandler() throws Exception {
File f=makeFile(CONTENTS);
HttpMethodBase m=getUpdateMethod("GET");
@ -49,6 +55,7 @@ public class CacheHeaderTest extends CacheHeaderTestBase {
checkVetoHeaders(m, true);
}
@Test
public void testCacheVetoException() throws Exception {
HttpMethodBase m = getSelectMethod("GET");
// We force an exception from Solr. This should emit "no-cache" HTTP headers
@ -59,6 +66,7 @@ public class CacheHeaderTest extends CacheHeaderTestBase {
checkVetoHeaders(m, false);
}
protected void checkVetoHeaders(HttpMethodBase m, boolean checkExpires) throws Exception {
Header head = m.getResponseHeader("Cache-Control");
assertNotNull("We got no Cache-Control header", head);

View File

@ -23,47 +23,16 @@ import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.solr.client.solrj.SolrExampleTestBase;
import org.apache.solr.client.solrj.SolrJettyTestBase;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.junit.Test;
public abstract class CacheHeaderTestBase extends SolrExampleTestBase {
@Override public String getSolrHome() { return "solr/"; }
abstract public String getSolrConfigFilename();
public String getSolrConfigFile() { return getSolrHome()+"conf/"+getSolrConfigFilename(); }
CommonsHttpSolrServer server;
JettySolrRunner jetty;
int port = 0;
static final String context = "/example";
@Override
public void setUp() throws Exception {
super.setUp();
jetty = new JettySolrRunner(context, 0, getSolrConfigFilename());
jetty.start();
port = jetty.getLocalPort();
server = this.createNewSolrServer();
}
@Override
public void tearDown() throws Exception {
super.tearDown();
jetty.stop(); // stop the server
}
@Override
protected SolrServer getSolrServer() {
return server;
}
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public abstract class CacheHeaderTestBase extends SolrJettyTestBase {
@Override
protected CommonsHttpSolrServer createNewSolrServer() {
try {
@ -80,13 +49,14 @@ public abstract class CacheHeaderTestBase extends SolrExampleTestBase {
}
protected HttpMethodBase getSelectMethod(String method) {
CommonsHttpSolrServer httpserver = (CommonsHttpSolrServer)getSolrServer();
HttpMethodBase m = null;
if ("GET".equals(method)) {
m = new GetMethod(server.getBaseURL() + "/select");
m = new GetMethod(httpserver.getBaseURL() + "/select");
} else if ("HEAD".equals(method)) {
m = new HeadMethod(server.getBaseURL() + "/select");
m = new HeadMethod(httpserver.getBaseURL() + "/select");
} else if ("POST".equals(method)) {
m = new PostMethod(server.getBaseURL() + "/select");
m = new PostMethod(httpserver.getBaseURL() + "/select");
}
m.setQueryString(new NameValuePair[] { new NameValuePair("q", "solr"),
new NameValuePair("qt", "standard") });
@ -94,21 +64,23 @@ public abstract class CacheHeaderTestBase extends SolrExampleTestBase {
}
protected HttpMethodBase getUpdateMethod(String method) {
CommonsHttpSolrServer httpserver = (CommonsHttpSolrServer)getSolrServer();
HttpMethodBase m = null;
if ("GET".equals(method)) {
m=new GetMethod(server.getBaseURL()+"/update/csv");
m=new GetMethod(httpserver.getBaseURL()+"/update/csv");
} else if ("POST".equals(method)) {
m=new PostMethod(server.getBaseURL()+"/update/csv");
m=new PostMethod(httpserver.getBaseURL()+"/update/csv");
} else if ("HEAD".equals(method)) {
m=new HeadMethod(server.getBaseURL()+"/update/csv");
m=new HeadMethod(httpserver.getBaseURL()+"/update/csv");
}
return m;
}
protected HttpClient getClient() {
return server.getHttpClient();
CommonsHttpSolrServer httpserver = (CommonsHttpSolrServer)getSolrServer();
return httpserver.getHttpClient();
}
protected void checkResponseBody(String method, HttpMethodBase resp)
@ -140,16 +112,19 @@ public abstract class CacheHeaderTestBase extends SolrExampleTestBase {
}
// The tests
@Test
public void testLastModified() throws Exception {
doLastModified("GET");
doLastModified("HEAD");
}
@Test
public void testEtag() throws Exception {
doETag("GET");
doETag("HEAD");
}
@Test
public void testCacheControl() throws Exception {
doCacheControl("GET");
doCacheControl("HEAD");

View File

@ -21,24 +21,35 @@ import java.util.Date;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.util.DateUtil;
import org.junit.BeforeClass;
import org.junit.Test;
import static junit.framework.Assert.*;
/**
* A test case for the several HTTP cache headers emitted by Solr
*/
public class NoCacheHeaderTest extends CacheHeaderTestBase {
@Override public String getSolrConfigFilename() { return "solrconfig-nocache.xml"; }
@BeforeClass
public static void beforeTest() throws Exception {
createJetty("solr/", "solr/conf/solrconfig-nocache.xml", null);
}
// The tests
@Test
public void testLastModified() throws Exception {
doLastModified("GET");
doLastModified("HEAD");
}
@Test
public void testEtag() throws Exception {
doETag("GET");
doETag("HEAD");
}
@Test
public void testCacheControl() throws Exception {
doCacheControl("GET");
doCacheControl("HEAD");