Browse Source

Polishing

(cherry picked from commit e828be9)
pull/1155/head
Juergen Hoeller 9 years ago
parent
commit
5b0722ed28
  1. 31
      spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java
  2. 11
      spring-messaging/src/test/java/org/springframework/messaging/core/GenericMessagingTemplateTests.java
  3. 16
      spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java
  4. 9
      spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java
  5. 17
      spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java
  6. 12
      spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
  7. 25
      spring-web/src/test/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequestTests.java
  8. 19
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartIntegrationTests.java

31
spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -199,6 +199,20 @@ public class MappingJackson2MessageConverterTests {
} }
@JsonView(MyJacksonView1.class)
public JacksonViewBean jsonViewResponse() {
JacksonViewBean bean = new JacksonViewBean();
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("with");
return bean;
}
public void jsonViewPayload(@JsonView(MyJacksonView2.class) JacksonViewBean payload) {
}
public static class MyBean { public static class MyBean {
private String string; private String string;
@ -262,9 +276,12 @@ public class MappingJackson2MessageConverterTests {
} }
} }
public interface MyJacksonView1 {}; public interface MyJacksonView1 {};
public interface MyJacksonView2 {}; public interface MyJacksonView2 {};
public static class JacksonViewBean { public static class JacksonViewBean {
@JsonView(MyJacksonView1.class) @JsonView(MyJacksonView1.class)
@ -300,16 +317,4 @@ public class MappingJackson2MessageConverterTests {
} }
} }
@JsonView(MyJacksonView1.class)
public JacksonViewBean jsonViewResponse() {
JacksonViewBean bean = new JacksonViewBean();
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("with");
return bean;
}
public void jsonViewPayload(@JsonView(MyJacksonView2.class) JacksonViewBean payload) {
}
} }

11
spring-messaging/src/test/java/org/springframework/messaging/core/GenericMessagingTemplateTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -66,7 +66,6 @@ public class GenericMessagingTemplateTests {
@Test @Test
public void sendAndReceive() { public void sendAndReceive() {
SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor); SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
channel.subscribe(new MessageHandler() { channel.subscribe(new MessageHandler() {
@Override @Override
@ -82,7 +81,6 @@ public class GenericMessagingTemplateTests {
@Test @Test
public void sendAndReceiveTimeout() throws InterruptedException { public void sendAndReceiveTimeout() throws InterruptedException {
final AtomicReference<Throwable> failure = new AtomicReference<Throwable>(); final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
@ -118,8 +116,9 @@ public class GenericMessagingTemplateTests {
assertNull(this.template.convertSendAndReceive(channel, "request", String.class)); assertNull(this.template.convertSendAndReceive(channel, "request", String.class));
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
if (failure.get() != null) { Throwable ex = failure.get();
throw new AssertionError(failure.get()); if (ex != null) {
throw new AssertionError(ex);
} }
} }
@ -138,6 +137,7 @@ public class GenericMessagingTemplateTests {
assertFalse(accessor.isMutable()); assertFalse(accessor.isMutable());
} }
private class TestDestinationResolver implements DestinationResolver<MessageChannel> { private class TestDestinationResolver implements DestinationResolver<MessageChannel> {
@Override @Override
@ -145,4 +145,5 @@ public class GenericMessagingTemplateTests {
return messageChannel; return messageChannel;
} }
} }
} }

16
spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java

@ -22,6 +22,8 @@ import java.text.ParseException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -29,14 +31,9 @@ import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.jayway.jsonpath.InvalidPathException; import static org.hamcrest.MatcherAssert.*;
import com.jayway.jsonpath.JsonPath; import static org.hamcrest.core.IsInstanceOf.*;
import static org.springframework.test.util.AssertionErrors.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import static org.springframework.test.util.AssertionErrors.fail;
/** /**
* A helper class for applying assertions via JSON path expressions. * A helper class for applying assertions via JSON path expressions.
@ -257,9 +254,6 @@ public class JsonPathExpectationsHelper {
catch (InvalidPathException ex) { catch (InvalidPathException ex) {
throw new AssertionError(message + ex.getMessage()); throw new AssertionError(message + ex.getMessage());
} }
catch (ArrayIndexOutOfBoundsException ex) {
throw new AssertionError(message + ex.getMessage());
}
catch (IndexOutOfBoundsException ex) { catch (IndexOutOfBoundsException ex) {
throw new AssertionError(message + ex.getMessage()); throw new AssertionError(message + ex.getMessage());
} }

9
spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -123,28 +123,21 @@ public class ObjectToStringHttpMessageConverterTests {
@Test @Test
public void read() throws IOException { public void read() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType(MediaType.TEXT_PLAIN_VALUE); request.setContentType(MediaType.TEXT_PLAIN_VALUE);
Short shortValue = Short.valueOf((short) 781); Short shortValue = Short.valueOf((short) 781);
request.setContent(shortValue.toString().getBytes( request.setContent(shortValue.toString().getBytes(
StringHttpMessageConverter.DEFAULT_CHARSET)); StringHttpMessageConverter.DEFAULT_CHARSET));
assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request))); assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request)));
Float floatValue = Float.valueOf(123); Float floatValue = Float.valueOf(123);
request.setCharacterEncoding("UTF-16"); request.setCharacterEncoding("UTF-16");
request.setContent(floatValue.toString().getBytes("UTF-16")); request.setContent(floatValue.toString().getBytes("UTF-16"));
assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request))); assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request)));
Long longValue = Long.valueOf(55819182821331L); Long longValue = Long.valueOf(55819182821331L);
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
request.setContent(longValue.toString().getBytes("UTF-8")); request.setContent(longValue.toString().getBytes("UTF-8"));
assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request))); assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request)));
} }

17
spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,14 +49,14 @@ import static org.junit.Assert.*;
*/ */
public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCase { public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCase {
private final AsyncRestTemplate template = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory()); private final AsyncRestTemplate template = new AsyncRestTemplate(
new HttpComponentsAsyncClientHttpRequestFactory());
@Test @Test
public void getEntity() throws Exception { public void getEntity() throws Exception {
Future<ResponseEntity<String>> futureEntity = Future<ResponseEntity<String>> future = template.getForEntity(baseUrl + "/{method}", String.class, "get");
template.getForEntity(baseUrl + "/{method}", String.class, "get"); ResponseEntity<String> entity = future.get();
ResponseEntity<String> entity = futureEntity.get();
assertEquals("Invalid content", helloWorld, entity.getBody()); assertEquals("Invalid content", helloWorld, entity.getBody());
assertFalse("No headers", entity.getHeaders().isEmpty()); assertFalse("No headers", entity.getHeaders().isEmpty());
assertEquals("Invalid content-type", textContentType, entity.getHeaders().getContentType()); assertEquals("Invalid content-type", textContentType, entity.getHeaders().getContentType());
@ -65,10 +65,9 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
@Test @Test
public void multipleFutureGets() throws Exception { public void multipleFutureGets() throws Exception {
Future<ResponseEntity<String>> futureEntity = Future<ResponseEntity<String>> future = template.getForEntity(baseUrl + "/{method}", String.class, "get");
template.getForEntity(baseUrl + "/{method}", String.class, "get"); future.get();
futureEntity.get(); future.get();
futureEntity.get();
} }
@Test @Test

12
spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.nio.charset.Charset;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Set; import java.util.Set;
import com.fasterxml.jackson.annotation.JsonView;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
@ -38,8 +39,6 @@ import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import com.fasterxml.jackson.annotation.JsonView;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@ -235,17 +234,18 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
assertFalse(s.contains("\"without\":\"without\"")); assertFalse(s.contains("\"without\":\"without\""));
} }
// SPR-12123 @Test // SPR-12123
@Test
public void serverPort() { public void serverPort() {
String s = template.getForObject("http://localhost:{port}/get", String.class, port); String s = template.getForObject("http://localhost:{port}/get", String.class, port);
assertEquals("Invalid content", helloWorld, s); assertEquals("Invalid content", helloWorld, s);
} }
public interface MyJacksonView1 {}; public interface MyJacksonView1 {};
public interface MyJacksonView2 {}; public interface MyJacksonView2 {};
public static class MySampleBean { public static class MySampleBean {
@JsonView(MyJacksonView1.class) @JsonView(MyJacksonView1.class)

25
spring-web/src/test/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequestTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.web.multipart.support;
import java.net.URI; import java.net.URI;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletRequestWrapper;
@ -33,9 +32,7 @@ import org.springframework.mock.web.test.MockMultipartHttpServletRequest;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/** /**
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@ -89,9 +86,7 @@ public class RequestPartServletServerHttpRequestTests {
assertArrayEquals(bytes, result); assertArrayEquals(bytes, result);
} }
// SPR-13317 @Test // SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception { public void getBodyWithWrappedRequest() throws Exception {
byte[] bytes = "content".getBytes("UTF-8"); byte[] bytes = "content".getBytes("UTF-8");
MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes); MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
@ -103,12 +98,9 @@ public class RequestPartServletServerHttpRequestTests {
assertArrayEquals(bytes, result); assertArrayEquals(bytes, result);
} }
// SPR-13096 @Test // SPR-13096
@Test
public void getBodyViaRequestParameter() throws Exception { public void getBodyViaRequestParameter() throws Exception {
MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() { MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {
@Override @Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) { public HttpHeaders getMultipartHeaders(String paramOrFileName) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -116,10 +108,10 @@ public class RequestPartServletServerHttpRequestTests {
return headers; return headers;
} }
}; };
byte[] bytes = {(byte) 0xC4}; byte[] bytes = {(byte) 0xC4};
mockRequest.setParameter("part", new String(bytes, Charset.forName("iso-8859-1"))); mockRequest.setParameter("part", new String(bytes, Charset.forName("ISO-8859-1")));
ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part"); ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertArrayEquals(bytes, result); assertArrayEquals(bytes, result);
} }
@ -127,7 +119,6 @@ public class RequestPartServletServerHttpRequestTests {
@Test @Test
public void getBodyViaRequestParameterWithRequestEncoding() throws Exception { public void getBodyViaRequestParameterWithRequestEncoding() throws Exception {
MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() { MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {
@Override @Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) { public HttpHeaders getMultipartHeaders(String paramOrFileName) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -135,11 +126,11 @@ public class RequestPartServletServerHttpRequestTests {
return headers; return headers;
} }
}; };
byte[] bytes = {(byte) 0xC4}; byte[] bytes = {(byte) 0xC4};
mockRequest.setParameter("part", new String(bytes, Charset.forName("iso-8859-1"))); mockRequest.setParameter("part", new String(bytes, Charset.forName("ISO-8859-1")));
mockRequest.setCharacterEncoding("iso-8859-1"); mockRequest.setCharacterEncoding("iso-8859-1");
ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part"); ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertArrayEquals(bytes, result); assertArrayEquals(bytes, result);
} }

19
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartIntegrationTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,7 +30,6 @@ import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -116,6 +115,13 @@ public class RequestPartIntegrationTests {
baseUrl = "http://localhost:" + connector.getLocalPort(); baseUrl = "http://localhost:" + connector.getLocalPort();
} }
@AfterClass
public static void stopServer() throws Exception {
if (server != null) {
server.stop();
}
}
@Before @Before
public void setUp() { public void setUp() {
ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter(); ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
@ -134,13 +140,6 @@ public class RequestPartIntegrationTests {
restTemplate.setMessageConverters(Collections.singletonList(converter)); restTemplate.setMessageConverters(Collections.singletonList(converter));
} }
@AfterClass
public static void stopServer() throws Exception {
if (server != null) {
server.stop();
}
}
@Test @Test
public void commonsMultipartResolver() throws Exception { public void commonsMultipartResolver() throws Exception {
@ -172,7 +171,7 @@ public class RequestPartIntegrationTests {
RequestEntity<byte[]> requestEntity = RequestEntity<byte[]> requestEntity =
RequestEntity.post(new URI(baseUrl + "/standard-resolver/spr13319")) RequestEntity.post(new URI(baseUrl + "/standard-resolver/spr13319"))
.contentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params)) .contentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params))
.body(content.getBytes(Charset.forName("us-ascii"))); .body(content.getBytes(Charset.forName("US-ASCII")));
ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter(); ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.MULTIPART_FORM_DATA)); converter.setSupportedMediaTypes(Collections.singletonList(MediaType.MULTIPART_FORM_DATA));

Loading…
Cancel
Save