From 75fe61843ae10fdfbd223d4f2b72d436a68776a2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 23 Jun 2015 15:01:24 +0200 Subject: [PATCH] Fix broken (Async)RestTemplate integration tests Issue: SPR-13157 --- .../client/AbstractJettyServerTestCase.java | 29 ++++++++----------- .../AsyncRestTemplateIntegrationTests.java | 11 ++----- .../client/RestTemplateIntegrationTests.java | 10 ++----- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java b/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java index b56c43960da..f633dec43ea 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java +++ b/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -50,27 +50,22 @@ import static org.junit.Assert.*; */ public class AbstractJettyServerTestCase { - protected static String helloWorld = "H\u00e9llo W\u00f6rld"; + protected static final String helloWorld = "H\u00e9llo W\u00f6rld"; - protected static int port; - protected static String baseUrl; + protected static final int port = SocketUtils.findAvailableTcpPort(); - protected static MediaType textContentType; - protected static MediaType jsonContentType; + protected static final String baseUrl = "http://localhost:" + port; - private static Server jettyServer; + 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")); + + private static final Server jettyServer = new Server(port); @BeforeClass public static void startJettyServer() throws Exception { - port = SocketUtils.findAvailableTcpPort(); - jettyServer = new Server(port); - baseUrl = "http://localhost:" + port; ServletContextHandler handler = new ServletContextHandler(); - byte[] bytes = helloWorld.getBytes("UTF-8"); - textContentType = new MediaType("text", "plain", Collections - .singletonMap("charset", "UTF-8")); - jsonContentType = new MediaType("application", "json", Collections - .singletonMap("charset", "UTF-8")); + byte[] bytes = helloWorld.getBytes("utf-8"); handler.addServlet(new ServletHolder(new GetServlet(bytes, textContentType)), "/get"); handler.addServlet(new ServletHolder(new GetServlet(new byte[0], textContentType)), "/get/nothing"); handler.addServlet(new ServletHolder(new GetServlet(bytes, null)), "/get/nocontenttype"); @@ -212,7 +207,7 @@ public class AbstractJettyServerTestCase { response.setStatus(HttpServletResponse.SC_CREATED); response.setHeader("Location", location); response.setContentType(contentType.toString()); - byte[] bytes = body.getBytes("UTF-8"); + byte[] bytes = body.getBytes("utf-8"); response.setContentLength(bytes.length);; FileCopyUtils.copy(bytes, response.getOutputStream()); } @@ -244,7 +239,7 @@ public class AbstractJettyServerTestCase { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); - resp.setCharacterEncoding("UTF-8"); + resp.setCharacterEncoding("utf-8"); resp.getWriter().write(req.getRequestURI()); } } diff --git a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java index 9a45ff0276d..6210cec1f8d 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -25,7 +25,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import org.junit.Before; import org.junit.Test; import org.springframework.core.io.ClassPathResource; @@ -50,13 +49,7 @@ import static org.junit.Assert.*; */ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCase { - private AsyncRestTemplate template; - - - @Before - public void createTemplate() { - template = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory()); - } + private final AsyncRestTemplate template = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory()); @Test diff --git a/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java b/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java index 393b0d20c91..149340b6f9e 100644 --- a/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java @@ -23,8 +23,6 @@ import java.nio.charset.Charset; import java.util.EnumSet; import java.util.Set; -import com.fasterxml.jackson.annotation.JsonView; -import org.junit.Before; import org.junit.Test; import org.springframework.core.io.ClassPathResource; @@ -40,6 +38,8 @@ import org.springframework.http.converter.json.MappingJacksonValue; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import com.fasterxml.jackson.annotation.JsonView; + import static org.junit.Assert.*; /** @@ -47,12 +47,8 @@ import static org.junit.Assert.*; */ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase { - private RestTemplate template; + private final RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory()); - @Before - public void createTemplate() { - template = new RestTemplate(new HttpComponentsClientHttpRequestFactory()); - } @Test public void getString() {