Polishing

(cherry picked from commit 5fee5f3)
This commit is contained in:
Juergen Hoeller
2016-12-01 14:13:23 +01:00
parent 23e91e1ae6
commit 0b76b13d88
22 changed files with 224 additions and 221 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -151,8 +151,7 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory,
*/
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
Assert.state(this.taskExecutor != null,
"Asynchronous execution requires an AsyncTaskExecutor to be set");
Assert.state(this.taskExecutor != null, "Asynchronous execution requires TaskExecutor to be set");
HttpURLConnection connection = openConnection(uri.toURL(), this.proxy);
prepareConnection(connection, httpMethod.name());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,14 +44,14 @@ import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class UriTemplate implements Serializable {
private final String uriTemplate;
private final UriComponents uriComponents;
private final List<String> variableNames;
private final Pattern matchPattern;
private final String uriTemplate;
/**
* Construct a new {@code UriTemplate} with the given URI String.
@@ -105,7 +105,7 @@ public class UriTemplate implements Serializable {
* <p>Example:
* <pre class="code">
* UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
* System.out.println(template.expand("Rest & Relax", "42));
* System.out.println(template.expand("Rest & Relax", 42));
* </pre>
* will print: <blockquote>{@code http://example.com/hotels/Rest%20%26%20Relax/bookings/42}</blockquote>
* @param uriVariableValues the array of URI variables
@@ -173,7 +173,6 @@ public class UriTemplate implements Serializable {
private final Pattern pattern;
private TemplateInfo(List<String> vars, Pattern pattern) {
this.variableNames = vars;
this.pattern = pattern;
@@ -187,7 +186,7 @@ public class UriTemplate implements Serializable {
return this.pattern;
}
private static TemplateInfo parse(String uriTemplate) {
public static TemplateInfo parse(String uriTemplate) {
int level = 0;
List<String> variableNames = new ArrayList<String>();
StringBuilder pattern = new StringBuilder();
@@ -216,8 +215,7 @@ public class UriTemplate implements Serializable {
else {
if (idx + 1 == variable.length()) {
throw new IllegalArgumentException(
"No custom regular expression specified after ':' " +
"in \"" + variable + "\"");
"No custom regular expression specified after ':' in \"" + variable + "\"");
}
String regex = variable.substring(idx + 1, variable.length());
pattern.append('(');
@@ -238,7 +236,7 @@ public class UriTemplate implements Serializable {
}
private static String quote(StringBuilder builder) {
return builder.length() != 0 ? Pattern.quote(builder.toString()) : "";
return (builder.length() > 0 ? Pattern.quote(builder.toString()) : "");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,13 +33,11 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -56,11 +54,12 @@ public class AbstractJettyServerTestCase {
protected static final String helloWorld = "H\u00e9llo W\u00f6rld";
protected static final MediaType textContentType = new MediaType("text", "plain",
Collections.singletonMap("charset", "UTF-8"));
protected static final MediaType textContentType =
new MediaType("text", "plain", Collections.singletonMap("charset", "UTF-8"));
protected static final MediaType jsonContentType =
new MediaType("application", "json", Collections.singletonMap("charset", "UTF-8"));
protected static final MediaType jsonContentType = new MediaType("application",
"json", Collections.singletonMap("charset", "utf-8"));
private static Server jettyServer;
@@ -71,7 +70,6 @@ public class AbstractJettyServerTestCase {
@BeforeClass
public static void startJettyServer() throws Exception {
// Let server pick its own random, available port.
jettyServer = new Server(0);
@@ -114,39 +112,41 @@ public class AbstractJettyServerTestCase {
}
}
/** Servlet that sets the given status code. */
@SuppressWarnings("serial")
private static class StatusCodeServlet extends GenericServlet {
private final int sc;
private StatusCodeServlet(int sc) {
public StatusCodeServlet(int sc) {
this.sc = sc;
}
@Override
public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException {
public void service(ServletRequest request, ServletResponse response) throws IOException {
((HttpServletResponse) response).setStatus(sc);
}
}
/** Servlet that returns an error message for a given status code. */
@SuppressWarnings("serial")
private static class ErrorServlet extends GenericServlet {
private final int sc;
private ErrorServlet(int sc) {
public ErrorServlet(int sc) {
this.sc = sc;
}
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
public void service(ServletRequest request, ServletResponse response) throws IOException {
((HttpServletResponse) response).sendError(sc);
}
}
@SuppressWarnings("serial")
private static class GetServlet extends HttpServlet {
@@ -154,14 +154,13 @@ public class AbstractJettyServerTestCase {
private final MediaType contentType;
private GetServlet(byte[] buf, MediaType contentType) {
public GetServlet(byte[] buf, MediaType contentType) {
this.buf = buf;
this.contentType = contentType;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (contentType != null) {
response.setContentType(contentType.toString());
}
@@ -170,10 +169,11 @@ public class AbstractJettyServerTestCase {
}
}
@SuppressWarnings("serial")
private static class PostServlet extends HttpServlet {
private final String s;
private final String content;
private final String location;
@@ -181,20 +181,19 @@ public class AbstractJettyServerTestCase {
private final MediaType contentType;
private PostServlet(String s, String location, byte[] buf, MediaType contentType) {
this.s = s;
public PostServlet(String content, String location, byte[] buf, MediaType contentType) {
this.content = content;
this.location = location;
this.buf = buf;
this.contentType = contentType;
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
assertTrue("Invalid request content-length", request.getContentLength() > 0);
assertNotNull("No content-type", request.getContentType());
String body = FileCopyUtils.copyToString(request.getReader());
assertEquals("Invalid request body", s, body);
assertEquals("Invalid request body", content, body);
response.setStatus(HttpServletResponse.SC_CREATED);
response.setHeader("Location", baseUrl + location);
response.setContentLength(buf.length);
@@ -203,6 +202,7 @@ public class AbstractJettyServerTestCase {
}
}
@SuppressWarnings("serial")
private static class JsonPostServlet extends HttpServlet {
@@ -210,14 +210,13 @@ public class AbstractJettyServerTestCase {
private final MediaType contentType;
private JsonPostServlet(String location, MediaType contentType) {
public JsonPostServlet(String location, MediaType contentType) {
this.location = location;
this.contentType = contentType;
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
assertTrue("Invalid request content-length", request.getContentLength() > 0);
assertNotNull("No content-type", request.getContentType());
String body = FileCopyUtils.copyToString(request.getReader());
@@ -230,18 +229,18 @@ public class AbstractJettyServerTestCase {
}
}
@SuppressWarnings("serial")
private static class PutServlet extends HttpServlet {
private final String s;
private PutServlet(String s, byte[] buf, MediaType contentType) {
public PutServlet(String s, byte[] buf, MediaType contentType) {
this.s = s;
}
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException {
assertTrue("Invalid request content-length", request.getContentLength() > 0);
assertNotNull("No content-type", request.getContentType());
String body = FileCopyUtils.copyToString(request.getReader());
@@ -250,17 +249,19 @@ public class AbstractJettyServerTestCase {
}
}
@SuppressWarnings("serial")
private static class UriServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("utf-8");
resp.getWriter().write(req.getRequestURI());
}
}
@SuppressWarnings("serial")
private static class MultipartServlet extends HttpServlet {
@@ -300,13 +301,13 @@ public class AbstractJettyServerTestCase {
}
}
@SuppressWarnings("serial")
private static class FormServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
assertEquals(MediaType.APPLICATION_FORM_URLENCODED_VALUE,
req.getContentType());
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
assertEquals(MediaType.APPLICATION_FORM_URLENCODED_VALUE, req.getContentType());
Map<String, String[]> parameters = req.getParameterMap();
assertEquals(2, parameters.size());
@@ -322,15 +323,14 @@ public class AbstractJettyServerTestCase {
}
}
@SuppressWarnings("serial")
private static class DeleteServlet extends HttpServlet {
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setStatus(200);
}
}
}

View File

@@ -114,7 +114,6 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
assertNull("Invalid content", entity.getBody());
}
@Test
public void getNoContentTypeHeader() throws Exception {
Future<ResponseEntity<byte[]>> futureEntity = template.getForEntity(baseUrl + "/get/nocontenttype", byte[].class);
@@ -122,7 +121,6 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
assertArrayEquals("Invalid content", helloWorld.getBytes("UTF-8"), responseEntity.getBody());
}
@Test
public void getNoContent() throws Exception {
Future<ResponseEntity<String>> responseFuture = template.getForEntity(baseUrl + "/status/nocontent", String.class);