Revise encoding steps towards use of JDK Charset and StandardCharsets
Issue: SPR-14492
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.http;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
@@ -32,7 +33,7 @@ import java.util.TimeZone;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -76,7 +77,7 @@ public class HttpHeadersTests {
|
||||
|
||||
@Test
|
||||
public void acceptCharsets() {
|
||||
Charset charset1 = Charset.forName("UTF-8");
|
||||
Charset charset1 = StandardCharsets.UTF_8;
|
||||
Charset charset2 = Charset.forName("ISO-8859-1");
|
||||
List<Charset> charsets = new ArrayList<>(2);
|
||||
charsets.add(charset1);
|
||||
@@ -89,7 +90,7 @@ public class HttpHeadersTests {
|
||||
@Test
|
||||
public void acceptCharsetWildcard() {
|
||||
headers.set("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
|
||||
assertEquals("Invalid Accept header", Arrays.asList(Charset.forName("ISO-8859-1"), Charset.forName("UTF-8")),
|
||||
assertEquals("Invalid Accept header", Arrays.asList(Charset.forName("ISO-8859-1"), StandardCharsets.UTF_8),
|
||||
headers.getAcceptCharset());
|
||||
}
|
||||
|
||||
@@ -111,7 +112,7 @@ public class HttpHeadersTests {
|
||||
|
||||
@Test
|
||||
public void contentType() {
|
||||
MediaType contentType = new MediaType("text", "html", Charset.forName("UTF-8"));
|
||||
MediaType contentType = new MediaType("text", "html", StandardCharsets.UTF_8);
|
||||
headers.setContentType(contentType);
|
||||
assertEquals("Invalid Content-Type header", contentType, headers.getContentType());
|
||||
assertEquals("Invalid Content-Type header", "text/html;charset=UTF-8", headers.getFirst("Content-Type"));
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -112,7 +112,7 @@ public class HttpRangeTests {
|
||||
|
||||
@Test
|
||||
public void toResourceRegion() {
|
||||
byte[] bytes = "Spring Framework".getBytes(Charset.forName("UTF-8"));
|
||||
byte[] bytes = "Spring Framework".getBytes(StandardCharsets.UTF_8);
|
||||
ByteArrayResource resource = new ByteArrayResource(bytes);
|
||||
HttpRange range = HttpRange.createByteRange(0, 5);
|
||||
ResourceRegion region = range.toResourceRegion(resource);
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -94,7 +95,6 @@ public class RequestEntityTests {
|
||||
@Test
|
||||
public void headers() throws URISyntaxException {
|
||||
MediaType accept = MediaType.TEXT_PLAIN;
|
||||
Charset charset = Charset.forName("UTF-8");
|
||||
long ifModifiedSince = 12345L;
|
||||
String ifNoneMatch = "\"foo\"";
|
||||
long contentLength = 67890;
|
||||
@@ -102,7 +102,7 @@ public class RequestEntityTests {
|
||||
|
||||
RequestEntity<Void> responseEntity = RequestEntity.post(new URI("http://example.com")).
|
||||
accept(accept).
|
||||
acceptCharset(charset).
|
||||
acceptCharset(StandardCharsets.UTF_8).
|
||||
ifModifiedSince(ifModifiedSince).
|
||||
ifNoneMatch(ifNoneMatch).
|
||||
contentLength(contentLength).
|
||||
|
||||
@@ -16,68 +16,66 @@
|
||||
|
||||
package org.springframework.http.client;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class SimpleClientHttpResponseTests {
|
||||
|
||||
private final Charset UTF8 = Charset.forName("UTF-8");
|
||||
|
||||
private SimpleClientHttpResponse response;
|
||||
|
||||
private HttpURLConnection connection;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.connection = mock(HttpURLConnection.class);
|
||||
this.response = new SimpleClientHttpResponse(this.connection);
|
||||
}
|
||||
|
||||
// SPR-14040
|
||||
@Test
|
||||
|
||||
@Test // SPR-14040
|
||||
public void shouldNotCloseConnectionWhenResponseClosed() throws Exception {
|
||||
TestByteArrayInputStream is = new TestByteArrayInputStream("Spring".getBytes(UTF8));
|
||||
TestByteArrayInputStream is = new TestByteArrayInputStream("Spring".getBytes(StandardCharsets.UTF_8));
|
||||
given(this.connection.getErrorStream()).willReturn(null);
|
||||
given(this.connection.getInputStream()).willReturn(is);
|
||||
|
||||
InputStream responseStream = this.response.getBody();
|
||||
assertThat(StreamUtils.copyToString(responseStream, UTF8), is("Spring"));
|
||||
assertThat(StreamUtils.copyToString(responseStream, StandardCharsets.UTF_8), is("Spring"));
|
||||
|
||||
this.response.close();
|
||||
assertTrue(is.isClosed());
|
||||
verify(this.connection, never()).disconnect();
|
||||
}
|
||||
|
||||
// SPR-14040
|
||||
@Test
|
||||
@Test // SPR-14040
|
||||
public void shouldDrainStreamWhenResponseClosed() throws Exception {
|
||||
byte[] buf = new byte[6];
|
||||
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
|
||||
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8));
|
||||
given(this.connection.getErrorStream()).willReturn(null);
|
||||
given(this.connection.getInputStream()).willReturn(is);
|
||||
|
||||
InputStream responseStream = this.response.getBody();
|
||||
responseStream.read(buf);
|
||||
assertThat(new String(buf, UTF8), is("Spring"));
|
||||
assertThat(new String(buf, StandardCharsets.UTF_8), is("Spring"));
|
||||
assertThat(is.available(), is(6));
|
||||
|
||||
this.response.close();
|
||||
@@ -86,16 +84,15 @@ public class SimpleClientHttpResponseTests {
|
||||
verify(this.connection, never()).disconnect();
|
||||
}
|
||||
|
||||
// SPR-14040
|
||||
@Test
|
||||
@Test // SPR-14040
|
||||
public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
|
||||
byte[] buf = new byte[6];
|
||||
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
|
||||
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8));
|
||||
given(this.connection.getErrorStream()).willReturn(is);
|
||||
|
||||
InputStream responseStream = this.response.getBody();
|
||||
responseStream.read(buf);
|
||||
assertThat(new String(buf, UTF8), is("Spring"));
|
||||
assertThat(new String(buf, StandardCharsets.UTF_8), is("Spring"));
|
||||
assertThat(is.available(), is(6));
|
||||
|
||||
this.response.close();
|
||||
|
||||
@@ -21,8 +21,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
@@ -31,8 +31,6 @@ import org.apache.commons.fileupload.FileItemFactory;
|
||||
import org.apache.commons.fileupload.FileUpload;
|
||||
import org.apache.commons.fileupload.RequestContext;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -47,14 +45,10 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.never;
|
||||
import static org.mockito.BDDMockito.verify;
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
import static org.hamcrest.CoreMatchers.startsWith;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -62,16 +56,7 @@ import static org.mockito.BDDMockito.verify;
|
||||
*/
|
||||
public class FormHttpMessageConverterTests {
|
||||
|
||||
public static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
|
||||
private FormHttpMessageConverter converter;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.converter = new AllEncompassingFormHttpMessageConverter();
|
||||
}
|
||||
private final FormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -89,7 +74,7 @@ public class FormHttpMessageConverterTests {
|
||||
assertTrue(this.converter.canWrite(MultiValueMap.class,
|
||||
new MediaType("multipart", "form-data")));
|
||||
assertTrue(this.converter.canWrite(MultiValueMap.class,
|
||||
new MediaType("multipart", "form-data", Charset.forName("UTF-8"))));
|
||||
new MediaType("multipart", "form-data", StandardCharsets.UTF_8)));
|
||||
assertTrue(this.converter.canWrite(MultiValueMap.class, MediaType.ALL));
|
||||
}
|
||||
|
||||
@@ -121,7 +106,7 @@ public class FormHttpMessageConverterTests {
|
||||
this.converter.write(body, MediaType.APPLICATION_FORM_URLENCODED, outputMessage);
|
||||
|
||||
assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3",
|
||||
outputMessage.getBodyAsString(UTF_8));
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "x-www-form-urlencoded"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
|
||||
@@ -155,8 +140,8 @@ public class FormHttpMessageConverterTests {
|
||||
parts.add("xml", entity);
|
||||
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setMultipartCharset(UTF_8);
|
||||
this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage);
|
||||
this.converter.setMultipartCharset(StandardCharsets.UTF_8);
|
||||
this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
|
||||
|
||||
final MediaType contentType = outputMessage.getHeaders().getContentType();
|
||||
assertNotNull("No boundary found", contentType.getParameter("boundary"));
|
||||
@@ -218,8 +203,8 @@ public class FormHttpMessageConverterTests {
|
||||
parts.add("part2", entity);
|
||||
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setMultipartCharset(UTF_8);
|
||||
this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage);
|
||||
this.converter.setMultipartCharset(StandardCharsets.UTF_8);
|
||||
this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
|
||||
|
||||
final MediaType contentType = outputMessage.getHeaders().getContentType();
|
||||
assertNotNull("No boundary found", contentType.getParameter("boundary"));
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.http.converter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -82,7 +82,7 @@ public class ResourceRegionHttpMessageConverterTests {
|
||||
assertThat(headers.getContentLength(), is(6L));
|
||||
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1));
|
||||
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 0-5/39"));
|
||||
assertThat(outputMessage.getBodyAsString(Charset.forName("UTF-8")), is("Spring"));
|
||||
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,7 +97,7 @@ public class ResourceRegionHttpMessageConverterTests {
|
||||
assertThat(headers.getContentLength(), is(32L));
|
||||
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1));
|
||||
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 7-38/39"));
|
||||
assertThat(outputMessage.getBodyAsString(Charset.forName("UTF-8")), is("Framework test resource content."));
|
||||
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Framework test resource content."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -115,7 +115,7 @@ public class ResourceRegionHttpMessageConverterTests {
|
||||
HttpHeaders headers = outputMessage.getHeaders();
|
||||
assertThat(headers.getContentType().toString(), Matchers.startsWith("multipart/byteranges;boundary="));
|
||||
String boundary = "--" + headers.getContentType().toString().substring(30);
|
||||
String content = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String content = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
|
||||
|
||||
assertThat(ranges[0], is(boundary));
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.http.converter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -35,10 +36,7 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class StringHttpMessageConverterTests {
|
||||
|
||||
public static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", UTF_8);
|
||||
|
||||
public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
|
||||
|
||||
private StringHttpMessageConverter converter;
|
||||
|
||||
@@ -66,7 +64,7 @@ public class StringHttpMessageConverterTests {
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
String body = "Hello World";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(UTF_8));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(TEXT_PLAIN_UTF_8);
|
||||
String result = this.converter.read(String.class, inputMessage);
|
||||
|
||||
@@ -92,15 +90,13 @@ public class StringHttpMessageConverterTests {
|
||||
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
|
||||
|
||||
HttpHeaders headers = this.outputMessage.getHeaders();
|
||||
assertEquals(body, this.outputMessage.getBodyAsString(UTF_8));
|
||||
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
|
||||
assertEquals(body.getBytes(UTF_8).length, headers.getContentLength());
|
||||
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
|
||||
assertFalse(headers.getAcceptCharset().isEmpty());
|
||||
}
|
||||
|
||||
// SPR-8867
|
||||
|
||||
@Test
|
||||
@Test // SPR-8867
|
||||
public void writeOverrideRequestedContentType() throws IOException {
|
||||
String body = "H\u00e9llo W\u00f6rld";
|
||||
MediaType requestedContentType = new MediaType("text", "html");
|
||||
@@ -109,9 +105,9 @@ public class StringHttpMessageConverterTests {
|
||||
headers.setContentType(TEXT_PLAIN_UTF_8);
|
||||
this.converter.write(body, requestedContentType, this.outputMessage);
|
||||
|
||||
assertEquals(body, this.outputMessage.getBodyAsString(UTF_8));
|
||||
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
|
||||
assertEquals(body.getBytes(UTF_8).length, headers.getContentLength());
|
||||
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
|
||||
assertFalse(headers.getAcceptCharset().isEmpty());
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.converter.feed;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -44,32 +45,31 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
|
||||
private AtomFeedHttpMessageConverter converter;
|
||||
|
||||
private Charset utf8;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
utf8 = Charset.forName("UTF-8");
|
||||
converter = new AtomFeedHttpMessageConverter();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
assertTrue(converter.canRead(Feed.class, new MediaType("application", "atom+xml")));
|
||||
assertTrue(converter.canRead(Feed.class, new MediaType("application", "atom+xml", utf8)));
|
||||
assertTrue(converter.canRead(Feed.class, new MediaType("application", "atom+xml", StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
assertTrue(converter.canWrite(Feed.class, new MediaType("application", "atom+xml")));
|
||||
assertTrue(converter.canWrite(Feed.class, new MediaType("application", "atom+xml", Charset.forName("UTF-8"))));
|
||||
assertTrue(converter.canWrite(Feed.class, new MediaType("application", "atom+xml", StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
InputStream is = getClass().getResourceAsStream("atom.xml");
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "atom+xml", utf8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "atom+xml", StandardCharsets.UTF_8));
|
||||
Feed result = converter.read(Feed.class, inputMessage);
|
||||
assertEquals("title", result.getTitle());
|
||||
assertEquals("subtitle", result.getSubtitle().getValue());
|
||||
@@ -106,12 +106,12 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(feed, null, outputMessage);
|
||||
|
||||
assertEquals("Invalid content-type", new MediaType("application", "atom+xml", utf8),
|
||||
assertEquals("Invalid content-type", new MediaType("application", "atom+xml", StandardCharsets.UTF_8),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" +
|
||||
"<entry><id>id1</id><title>title1</title></entry>" +
|
||||
"<entry><id>id2</id><title>title2</title></entry></feed>";
|
||||
assertXMLEqual(expected, outputMessage.getBodyAsString(utf8));
|
||||
assertXMLEqual(expected, outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.converter.feed;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -44,32 +45,31 @@ public class RssChannelHttpMessageConverterTests {
|
||||
|
||||
private RssChannelHttpMessageConverter converter;
|
||||
|
||||
private Charset utf8;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
utf8 = Charset.forName("UTF-8");
|
||||
converter = new RssChannelHttpMessageConverter();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
assertTrue(converter.canRead(Channel.class, new MediaType("application", "rss+xml")));
|
||||
assertTrue(converter.canRead(Channel.class, new MediaType("application", "rss+xml", utf8)));
|
||||
assertTrue(converter.canRead(Channel.class, new MediaType("application", "rss+xml", StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
assertTrue(converter.canWrite(Channel.class, new MediaType("application", "rss+xml")));
|
||||
assertTrue(converter.canWrite(Channel.class, new MediaType("application", "rss+xml", Charset.forName("UTF-8"))));
|
||||
assertTrue(converter.canWrite(Channel.class, new MediaType("application", "rss+xml", StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
InputStream is = getClass().getResourceAsStream("rss.xml");
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "rss+xml", utf8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "rss+xml", StandardCharsets.UTF_8));
|
||||
Channel result = converter.read(Channel.class, inputMessage);
|
||||
assertEquals("title", result.getTitle());
|
||||
assertEquals("http://example.com", result.getLink());
|
||||
@@ -106,14 +106,14 @@ public class RssChannelHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(channel, null, outputMessage);
|
||||
|
||||
assertEquals("Invalid content-type", new MediaType("application", "rss+xml", utf8),
|
||||
assertEquals("Invalid content-type", new MediaType("application", "rss+xml", StandardCharsets.UTF_8),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
String expected = "<rss version=\"2.0\">" +
|
||||
"<channel><title>title</title><link>http://example.com</link><description>description</description>" +
|
||||
"<item><title>title1</title></item>" +
|
||||
"<item><title>title2</title></item>" +
|
||||
"</channel></rss>";
|
||||
assertXMLEqual(expected, outputMessage.getBodyAsString(utf8));
|
||||
assertXMLEqual(expected, outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.converter.json;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -42,8 +43,6 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class GsonHttpMessageConverterTests {
|
||||
|
||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||
|
||||
private GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
|
||||
|
||||
|
||||
@@ -118,7 +117,7 @@ public class GsonHttpMessageConverterTests {
|
||||
body.setBool(true);
|
||||
body.setBytes(new byte[]{0x1, 0x2});
|
||||
this.converter.write(body, null, outputMessage);
|
||||
Charset utf8 = Charset.forName("UTF-8");
|
||||
Charset utf8 = StandardCharsets.UTF_8;
|
||||
String result = outputMessage.getBodyAsString(utf8);
|
||||
assertTrue(result.contains("\"string\":\"Foo\""));
|
||||
assertTrue(result.contains("\"number\":42"));
|
||||
@@ -166,8 +165,7 @@ public class GsonHttpMessageConverterTests {
|
||||
};
|
||||
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
|
||||
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(
|
||||
body.getBytes(UTF8));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
|
||||
List<MyBean> results = (List<MyBean>) converter.read(List.class, inputMessage);
|
||||
@@ -189,8 +187,7 @@ public class GsonHttpMessageConverterTests {
|
||||
|
||||
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
|
||||
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(
|
||||
body.getBytes(UTF8));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
|
||||
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
|
||||
@@ -210,7 +207,7 @@ public class GsonHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setPrefixJson(true);
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
assertEquals(")]}', \"foo\"", outputMessage.getBodyAsString(UTF8));
|
||||
assertEquals(")]}', \"foo\"", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -218,7 +215,7 @@ public class GsonHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setJsonPrefix(")))");
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
assertEquals(")))\"foo\"", outputMessage.getBodyAsString(UTF8));
|
||||
assertEquals(")))\"foo\"", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.converter.json;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -118,15 +119,14 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
body.setBool(true);
|
||||
body.setBytes(new byte[]{0x1, 0x2});
|
||||
converter.write(body, null, outputMessage);
|
||||
Charset utf8 = Charset.forName("UTF-8");
|
||||
String result = outputMessage.getBodyAsString(utf8);
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertTrue(result.contains("\"string\":\"Foo\""));
|
||||
assertTrue(result.contains("\"number\":42"));
|
||||
assertTrue(result.contains("fraction\":42.0"));
|
||||
assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
|
||||
assertTrue(result.contains("\"bool\":true"));
|
||||
assertTrue(result.contains("\"bytes\":\"AQI=\""));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
|
||||
assertEquals("Invalid content-type", new MediaType("application", "json", StandardCharsets.UTF_8),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
this.converter.setPrettyPrint(true);
|
||||
this.converter.writeInternal(bean, null, outputMessage);
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
|
||||
assertEquals("{" + NEWLINE_SYSTEM_PROPERTY + " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}", result);
|
||||
}
|
||||
@@ -231,7 +231,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
this.converter.setPrefixJson(true);
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
|
||||
assertEquals(")]}', \"foo\"", outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
assertEquals(")]}', \"foo\"", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -240,7 +240,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
this.converter.setJsonPrefix(")))");
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
|
||||
assertEquals(")))\"foo\"", outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
assertEquals(")))\"foo\"", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -255,7 +255,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
jacksonValue.setSerializationView(MyJacksonView1.class);
|
||||
this.converter.writeInternal(jacksonValue, null, outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertThat(result, containsString("\"withView1\":\"with\""));
|
||||
assertThat(result, not(containsString("\"withView2\":\"with\"")));
|
||||
assertThat(result, not(containsString("\"withoutView\":\"without\"")));
|
||||
@@ -274,7 +274,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
jacksonValue.setFilters(filters);
|
||||
this.converter.writeInternal(jacksonValue, null, outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertThat(result, containsString("\"property1\":\"value\""));
|
||||
assertThat(result, not(containsString("\"property2\":\"value\"")));
|
||||
}
|
||||
@@ -288,7 +288,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.writeInternal(jacksonValue, null, outputMessage);
|
||||
|
||||
assertEquals("/**/callback(\"foo\");", outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
assertEquals("/**/callback(\"foo\");", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -304,7 +304,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
jacksonValue.setJsonpFunction("callback");
|
||||
this.converter.writeInternal(jacksonValue, null, outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertThat(result, startsWith("/**/callback("));
|
||||
assertThat(result, endsWith(");"));
|
||||
assertThat(result, containsString("\"withView1\":\"with\""));
|
||||
@@ -321,7 +321,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
this.converter.writeInternal(bean, MyInterface.class, outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertTrue(result.contains("\"string\":\"Foo\""));
|
||||
assertTrue(result.contains("\"number\":42"));
|
||||
}
|
||||
@@ -343,7 +343,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
this.converter.writeInternal(beans, typeReference.getType(), outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertTrue(result.contains("\"string\":\"Foo\""));
|
||||
assertTrue(result.contains("\"number\":42"));
|
||||
assertTrue(result.contains("\"string\":\"Bar\""));
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
@@ -176,7 +176,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertXMLEqual("Invalid result", "<rootElement><type s=\"Hello World\"/></rootElement>",
|
||||
outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -186,7 +186,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertXMLEqual("Invalid result", "<rootElement><type s=\"Hello World\"/></rootElement>",
|
||||
outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
// SPR-11488
|
||||
@@ -197,7 +197,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
|
||||
myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<myRootElement><element>a|||b</element></myRootElement>",
|
||||
outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -18,7 +18,7 @@ package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
@@ -91,15 +91,14 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
body.setBool(true);
|
||||
body.setBytes(new byte[]{0x1, 0x2});
|
||||
converter.write(body, null, outputMessage);
|
||||
Charset utf8 = Charset.forName("UTF-8");
|
||||
String result = outputMessage.getBodyAsString(utf8);
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertTrue(result.contains("<string>Foo</string>"));
|
||||
assertTrue(result.contains("<number>42</number>"));
|
||||
assertTrue(result.contains("<fraction>42.0</fraction>"));
|
||||
assertTrue(result.contains("<array><array>Foo</array><array>Bar</array></array>"));
|
||||
assertTrue(result.contains("<bool>true</bool>"));
|
||||
assertTrue(result.contains("<bytes>AQI=</bytes>"));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml", utf8),
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml", StandardCharsets.UTF_8),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
}
|
||||
|
||||
@@ -132,7 +131,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
jacksonValue.setSerializationView(MyJacksonView1.class);
|
||||
this.writeInternal(jacksonValue, outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertThat(result, containsString("<withView1>with</withView1>"));
|
||||
assertThat(result, not(containsString("<withView2>with</withView2>")));
|
||||
assertThat(result, not(containsString("<withoutView>without</withoutView>")));
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
@@ -300,7 +301,7 @@ public class SourceHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(domSource, null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||
outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
|
||||
@@ -315,7 +316,7 @@ public class SourceHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(saxSource, null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||
outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
}
|
||||
@@ -328,7 +329,7 @@ public class SourceHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(streamSource, null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||
outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.http.server;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -93,7 +93,7 @@ public class ServletServerHttpRequestTests {
|
||||
assertEquals("Invalid header values returned", 2, headerValues.size());
|
||||
assertTrue("Invalid header values returned", headerValues.contains(headerValue1));
|
||||
assertTrue("Invalid header values returned", headerValues.contains(headerValue2));
|
||||
assertEquals("Invalid Content-Type", new MediaType("text", "plain", Charset.forName("UTF-8")),
|
||||
assertEquals("Invalid Content-Type", new MediaType("text", "plain", StandardCharsets.UTF_8),
|
||||
headers.getContentType());
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.http.server;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -64,7 +64,7 @@ public class ServletServerHttpResponseTests {
|
||||
headers.add(headerName, headerValue1);
|
||||
String headerValue2 = "value2";
|
||||
headers.add(headerName, headerValue2);
|
||||
headers.setContentType(new MediaType("text", "plain", Charset.forName("UTF-8")));
|
||||
headers.setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));
|
||||
|
||||
response.close();
|
||||
assertTrue("Header not set", mockResponse.getHeaderNames().contains(headerName));
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -31,7 +31,10 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -39,9 +42,6 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class ServerHttpResponseTests {
|
||||
|
||||
public static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
|
||||
@Test
|
||||
public void writeWith() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
@@ -52,9 +52,9 @@ public class ServerHttpResponseTests {
|
||||
assertTrue(response.cookiesWritten);
|
||||
|
||||
assertEquals(3, response.body.size());
|
||||
assertEquals("a", new String(response.body.get(0).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("b", new String(response.body.get(1).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("c", new String(response.body.get(2).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("a", new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
assertEquals("b", new String(response.body.get(1).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
assertEquals("c", new String(response.body.get(2).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,9 +96,9 @@ public class ServerHttpResponseTests {
|
||||
assertSame(cookie, response.getCookies().getFirst("ID"));
|
||||
|
||||
assertEquals(3, response.body.size());
|
||||
assertEquals("a", new String(response.body.get(0).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("b", new String(response.body.get(1).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("c", new String(response.body.get(2).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("a", new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
assertEquals("b", new String(response.body.get(1).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
assertEquals("c", new String(response.body.get(2).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -114,9 +114,9 @@ public class ServerHttpResponseTests {
|
||||
assertNull(response.getCookies().get("ID"));
|
||||
|
||||
assertEquals(3, response.body.size());
|
||||
assertEquals("a", new String(response.body.get(0).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("b", new String(response.body.get(1).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("c", new String(response.body.get(2).asByteBuffer().array(), UTF_8));
|
||||
assertEquals("a", new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
assertEquals("b", new String(response.body.get(1).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
assertEquals("c", new String(response.body.get(2).asByteBuffer().array(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -139,7 +139,7 @@ public class ServerHttpResponseTests {
|
||||
|
||||
|
||||
private DataBuffer wrap(String a) {
|
||||
return new DefaultDataBufferFactory().wrap(ByteBuffer.wrap(a.getBytes(UTF_8)));
|
||||
return new DefaultDataBufferFactory().wrap(ByteBuffer.wrap(a.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
@@ -27,6 +28,7 @@ import java.util.Set;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
@@ -44,8 +46,6 @@ 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.*;
|
||||
|
||||
/**
|
||||
@@ -215,7 +215,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||
@Test
|
||||
public void jsonPostForObject() throws URISyntaxException {
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
|
||||
entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
|
||||
MySampleBean bean = new MySampleBean();
|
||||
bean.setWith1("with");
|
||||
bean.setWith2("with");
|
||||
@@ -230,7 +230,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||
@Test
|
||||
public void jsonPostForObjectWithJacksonView() throws URISyntaxException {
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
|
||||
entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
|
||||
MySampleBean bean = new MySampleBean("with", "with", "without");
|
||||
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
|
||||
jacksonValue.setSerializationView(MyJacksonView1.class);
|
||||
@@ -257,7 +257,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||
ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<ParentClass>>() {};
|
||||
RequestEntity<List<ParentClass>> entity = RequestEntity
|
||||
.post(new URI(baseUrl + "/jsonpost"))
|
||||
.contentType(new MediaType("application", "json", Charset.forName("UTF-8")))
|
||||
.contentType(new MediaType("application", "json", StandardCharsets.UTF_8))
|
||||
.body(list, typeReference.getType());
|
||||
String content = template.exchange(entity, String.class).getBody();
|
||||
assertTrue(content.contains("\"type\":\"foo\""));
|
||||
|
||||
Reference in New Issue
Block a user