switched setup annotation to operate on Class to avoid InterruptedExceptions; accomodated OpenJDK behavior change wrt Expect: 100-continue

This commit is contained in:
Adrian Cole 2012-12-01 18:40:07 -08:00
parent dc2650f089
commit 1001021ecd
1 changed files with 79 additions and 86 deletions

View File

@ -60,8 +60,8 @@ import org.jclouds.ContextBuilder;
import org.jclouds.io.InputSuppliers; import org.jclouds.io.InputSuppliers;
import org.jclouds.providers.AnonymousProviderMetadata; import org.jclouds.providers.AnonymousProviderMetadata;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional; import org.testng.annotations.Optional;
import org.testng.annotations.Parameters; import org.testng.annotations.Parameters;
@ -90,7 +90,7 @@ public abstract class BaseJettyTest {
protected String md5; protected String md5;
static final Pattern actionPattern = Pattern.compile("/objects/(.*)/action/([a-z]*);?(.*)"); static final Pattern actionPattern = Pattern.compile("/objects/(.*)/action/([a-z]*);?(.*)");
@BeforeTest @BeforeClass
@Parameters({ "test-jetty-port" }) @Parameters({ "test-jetty-port" })
public void setUpJetty(@Optional("8123") final int testPort) throws Exception { public void setUpJetty(@Optional("8123") final int testPort) throws Exception {
this.testPort = testPort; this.testPort = testPort;
@ -102,62 +102,61 @@ public abstract class BaseJettyTest {
Handler server1Handler = new AbstractHandler() { Handler server1Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { throws IOException, ServletException {
InputStream body = request.getInputStream(); if (failIfNoContentLength(request, response)) {
try { return;
if (failIfNoContentLength(request, response)) { } else if (target.indexOf("sleep") > 0) {
return; sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
} else if (target.indexOf("sleep") > 0) { response.setContentType("text/xml");
sleepUninterruptibly(100, TimeUnit.MILLISECONDS); response.setStatus(SC_OK);
response.setContentType("text/xml"); } else if (target.indexOf("redirect") > 0) {
response.setStatus(SC_OK); // in OpenJDK 7.0, expect continue handling is enforced, so we
} else if (target.indexOf("redirect") > 0) { // have to consume the stream.
response.sendRedirect("https://localhost:" + (testPort + 1) + "/"); // http://hg.openjdk.java.net/jdk7/tl/jdk/rev/045aeb76b0ff
} else if (target.indexOf("101constitutions") > 0) { // getInputStream address the expect-continue, per jetty docs
response.setContentType("text/plain"); // http://wiki.eclipse.org/Jetty/Feature/1xx_Responses#100_Continue
response.setHeader("Content-MD5", md5); toStringAndClose(request.getInputStream());
response.setStatus(SC_OK); response.sendRedirect("https://localhost:" + (testPort + 1) + "/");
copy(oneHundredOneConstitutions, response.getOutputStream()); } else if (target.indexOf("101constitutions") > 0) {
} else if (request.getMethod().equals("PUT")) { response.setContentType("text/plain");
if (request.getContentLength() > 0) { response.setHeader("Content-MD5", md5);
response.setStatus(SC_OK); response.setStatus(SC_OK);
response.getWriter().println(toStringAndClose(body) + "PUT"); copy(oneHundredOneConstitutions, response.getOutputStream());
} else { } else if (request.getMethod().equals("PUT")) {
response.sendError(500, "no content"); if (request.getContentLength() > 0) {
}
} else if (request.getMethod().equals("POST")) {
// don't redirect large objects
if (request.getContentLength() < 10240 && redirectEveryTwentyRequests(request, response))
return;
if (failEveryTenRequests(request, response))
return;
if (request.getContentLength() > 0) {
handlePost(request, response);
} else {
handleAction(request, response);
}
} else if (request.getHeader("range") != null) {
response.sendError(404, "no content");
} else if (request.getHeader("test") != null) {
response.setContentType("text/plain");
response.setStatus(SC_OK);
response.getWriter().println("test");
} else if (request.getMethod().equals("HEAD")) {
// by HTML specification, HEAD response MUST NOT include a
// body
response.setContentType("text/xml");
response.setStatus(SC_OK); response.setStatus(SC_OK);
response.getWriter().println(toStringAndClose(request.getInputStream()) + "PUT");
} else { } else {
if (failEveryTenRequests(request, response)) response.sendError(500, "no content");
return;
response.setContentType("text/xml");
response.setStatus(SC_OK);
response.getWriter().println(XML);
} }
((Request) request).setHandled(true); } else if (request.getMethod().equals("POST")) {
} catch (IOException e) { // don't redirect large objects
closeQuietly(body); if (request.getContentLength() < 10240 && redirectEveryTwentyRequests(request, response))
response.sendError(500, getStackTraceAsString(e)); return;
if (failEveryTenRequests(request, response))
return;
if (request.getContentLength() > 0) {
handlePost(request, response);
} else {
handleAction(request, response);
}
} else if (request.getHeader("range") != null) {
response.sendError(404, "no content");
} else if (request.getHeader("test") != null) {
response.setContentType("text/plain");
response.setStatus(SC_OK);
response.getWriter().println("test");
} else if (request.getMethod().equals("HEAD")) {
// by HTML specification, HEAD response MUST NOT include a body
response.setContentType("text/xml");
response.setStatus(SC_OK);
} else {
if (failEveryTenRequests(request, response))
return;
response.setContentType("text/xml");
response.setStatus(SC_OK);
response.getWriter().println(XML);
} }
Request.class.cast(request).setHandled(true);
} }
}; };
@ -201,6 +200,7 @@ public abstract class BaseJettyTest {
response.setStatus(SC_OK); response.setStatus(SC_OK);
response.getWriter().println(responseString); response.getWriter().println(responseString);
} }
Request.class.cast(request).setHandled(true);
} catch (IOException e) { } catch (IOException e) {
closeQuietly(body); closeQuietly(body);
response.sendError(500, getStackTraceAsString(e)); response.sendError(500, getStackTraceAsString(e));
@ -211,36 +211,28 @@ public abstract class BaseJettyTest {
Handler server2Handler = new AbstractHandler() { Handler server2Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { throws IOException, ServletException {
InputStream body = request.getInputStream(); if (request.getMethod().equals("PUT")) {
try { if (request.getContentLength() > 0) {
if (request.getMethod().equals("PUT")) {
String text = toStringAndClose(body);
body = null;
if (request.getContentLength() > 0) {
response.setStatus(SC_OK);
response.getWriter().println(text + "PUTREDIRECT");
}
} else if (request.getMethod().equals("POST")) {
if (request.getContentLength() > 0) {
handlePost(request, response);
} else {
handleAction(request, response);
}
} else if (request.getMethod().equals("HEAD")) {
// by HTML specification, HEAD response MUST NOT include a
// body
response.setContentType("text/xml");
response.setStatus(SC_OK); response.setStatus(SC_OK);
} else { String text = toStringAndClose(request.getInputStream());
response.setContentType("text/xml"); response.getWriter().println(text + "PUTREDIRECT");
response.setStatus(SC_OK);
response.getWriter().println(XML2);
} }
((Request) request).setHandled(true); } else if (request.getMethod().equals("POST")) {
} catch (IOException e) { if (request.getContentLength() > 0) {
closeQuietly(body); handlePost(request, response);
response.sendError(500, getStackTraceAsString(e)); } else {
handleAction(request, response);
}
} else if (request.getMethod().equals("HEAD")) {
// by HTML specification, HEAD response MUST NOT include a body
response.setContentType("text/xml");
response.setStatus(SC_OK);
} else {
response.setContentType("text/xml");
response.setStatus(SC_OK);
response.getWriter().println(XML2);
} }
Request.class.cast(request).setHandled(true);
} }
}; };
@ -284,7 +276,7 @@ public abstract class BaseJettyTest {
.modules(ImmutableSet.<Module> copyOf(connectionModules)).overrides(properties); .modules(ImmutableSet.<Module> copyOf(connectionModules)).overrides(properties);
} }
@AfterTest @AfterClass
public void tearDownJetty() throws Exception { public void tearDownJetty() throws Exception {
closeQuietly(context); closeQuietly(context);
if (server2 != null) if (server2 != null)
@ -307,7 +299,7 @@ public abstract class BaseJettyTest {
protected boolean failEveryTenRequests(HttpServletRequest request, HttpServletResponse response) throws IOException { protected boolean failEveryTenRequests(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (cycle.incrementAndGet() % 10 == 0) { if (cycle.incrementAndGet() % 10 == 0) {
response.sendError(500, "unlucky 10"); response.sendError(500, "unlucky 10");
((Request) request).setHandled(true); Request.class.cast(request).setHandled(true);
return true; return true;
} }
return false; return false;
@ -317,7 +309,7 @@ public abstract class BaseJettyTest {
throws IOException { throws IOException {
if (cycle.incrementAndGet() % 20 == 0) { if (cycle.incrementAndGet() % 20 == 0) {
response.sendRedirect("http://localhost:" + (testPort + 1) + "/"); response.sendRedirect("http://localhost:" + (testPort + 1) + "/");
((Request) request).setHandled(true); Request.class.cast(request).setHandled(true);
return true; return true;
} }
return false; return false;
@ -337,7 +329,7 @@ public abstract class BaseJettyTest {
response.getWriter().println("no content length!"); response.getWriter().println("no content length!");
response.getWriter().println(realHeaders.toString()); response.getWriter().println(realHeaders.toString());
response.sendError(500, "no content length!"); response.sendError(500, "no content length!");
((Request) request).setHandled(true); Request.class.cast(request).setHandled(true);
return true; return true;
} }
return false; return false;
@ -359,4 +351,5 @@ public abstract class BaseJettyTest {
response.sendError(500, "no content"); response.sendError(500, "no content");
} }
} }
} }