Skip to content

Commit 169392e

Browse files
committed
Polish StringHttpMessageConverterTests
1 parent c050642 commit 169392e

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

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

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
2121

22-
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.http.HttpHeaders;
@@ -30,38 +29,33 @@
3029
import static org.assertj.core.api.Assertions.assertThat;
3130

3231
/**
32+
* Tests for {@link StringHttpMessageConverter}.
33+
*
3334
* @author Arjen Poutsma
3435
* @author Rossen Stoyanchev
3536
*/
36-
public class StringHttpMessageConverterTests {
37-
38-
public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
37+
class StringHttpMessageConverterTests {
3938

40-
private StringHttpMessageConverter converter;
39+
private static final MediaType TEXT_PLAIN_UTF_8 = new MediaType("text", "plain", StandardCharsets.UTF_8);
4140

42-
private MockHttpOutputMessage outputMessage;
41+
private final StringHttpMessageConverter converter = new StringHttpMessageConverter();
4342

44-
45-
@BeforeEach
46-
public void setUp() {
47-
this.converter = new StringHttpMessageConverter();
48-
this.outputMessage = new MockHttpOutputMessage();
49-
}
43+
private final MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
5044

5145

5246
@Test
53-
public void canRead() {
47+
void canRead() {
5448
assertThat(this.converter.canRead(String.class, MediaType.TEXT_PLAIN)).isTrue();
5549
}
5650

5751
@Test
58-
public void canWrite() {
52+
void canWrite() {
5953
assertThat(this.converter.canWrite(String.class, MediaType.TEXT_PLAIN)).isTrue();
6054
assertThat(this.converter.canWrite(String.class, MediaType.ALL)).isTrue();
6155
}
6256

6357
@Test
64-
public void read() throws IOException {
58+
void read() throws IOException {
6559
String body = "Hello World";
6660
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
6761
inputMessage.getHeaders().setContentType(TEXT_PLAIN_UTF_8);
@@ -71,7 +65,7 @@ public void read() throws IOException {
7165
}
7266

7367
@Test
74-
public void readWithContentLengthHeader() throws IOException {
68+
void readWithContentLengthHeader() throws IOException {
7569
String body = "Hello World";
7670
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
7771
inputMessage.getHeaders().setContentLength(body.length());
@@ -82,7 +76,7 @@ public void readWithContentLengthHeader() throws IOException {
8276
}
8377

8478
@Test // gh-24123
85-
public void readJson() throws IOException {
79+
void readJson() throws IOException {
8680
String body = "{\"result\":\"\u0414\u0410\"}";
8781
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
8882
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
@@ -92,7 +86,7 @@ public void readJson() throws IOException {
9286
}
9387

9488
@Test // gh-25328
95-
public void readJsonApi() throws IOException {
89+
void readJsonApi() throws IOException {
9690
String body = "{\"result\":\"\u0414\u0410\"}";
9791
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
9892
inputMessage.getHeaders().setContentType(new MediaType("application", "vnd.api.v1+json"));
@@ -102,31 +96,31 @@ public void readJsonApi() throws IOException {
10296
}
10397

10498
@Test
105-
public void writeDefaultCharset() throws IOException {
99+
void writeDefaultCharset() throws IOException {
106100
String body = "H\u00e9llo W\u00f6rld";
107101
this.converter.write(body, null, this.outputMessage);
108102

109103
HttpHeaders headers = this.outputMessage.getHeaders();
110104
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1)).isEqualTo(body);
111105
assertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
112106
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.ISO_8859_1).length);
113-
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
107+
assertThat(headers.getAcceptCharset()).isEmpty();
114108
}
115109

116110
@Test // gh-24123
117-
public void writeJson() throws IOException {
111+
void writeJson() throws IOException {
118112
String body = "{\"føø\":\"bår\"}";
119113
this.converter.write(body, MediaType.APPLICATION_JSON, this.outputMessage);
120114

121115
HttpHeaders headers = this.outputMessage.getHeaders();
122116
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
123117
assertThat(headers.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
124118
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
125-
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
119+
assertThat(headers.getAcceptCharset()).isEmpty();
126120
}
127121

128122
@Test // gh-25328
129-
public void writeJsonApi() throws IOException {
123+
void writeJsonApi() throws IOException {
130124
String body = "{\"føø\":\"bår\"}";
131125
MediaType contentType = new MediaType("application", "vnd.api.v1+json");
132126
this.converter.write(body, contentType, this.outputMessage);
@@ -135,23 +129,23 @@ public void writeJsonApi() throws IOException {
135129
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
136130
assertThat(headers.getContentType()).isEqualTo(contentType);
137131
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
138-
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
132+
assertThat(headers.getAcceptCharset()).isEmpty();
139133
}
140134

141135
@Test
142-
public void writeUTF8() throws IOException {
136+
void writeUTF8() throws IOException {
143137
String body = "H\u00e9llo W\u00f6rld";
144138
this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage);
145139

146140
HttpHeaders headers = this.outputMessage.getHeaders();
147141
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
148142
assertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
149143
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
150-
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
144+
assertThat(headers.getAcceptCharset()).isEmpty();
151145
}
152146

153147
@Test // SPR-8867
154-
public void writeOverrideRequestedContentType() throws IOException {
148+
void writeOverrideRequestedContentType() throws IOException {
155149
String body = "H\u00e9llo W\u00f6rld";
156150
MediaType requestedContentType = new MediaType("text", "html");
157151

@@ -162,19 +156,19 @@ public void writeOverrideRequestedContentType() throws IOException {
162156
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
163157
assertThat(headers.getContentType()).isEqualTo(TEXT_PLAIN_UTF_8);
164158
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
165-
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
159+
assertThat(headers.getAcceptCharset()).isEmpty();
166160
}
167161

168162
@Test // gh-24283
169-
public void writeWithWildCardMediaType() throws IOException {
163+
void writeWithWildCardMediaType() throws IOException {
170164
String body = "Hello World";
171165
this.converter.write(body, MediaType.ALL, this.outputMessage);
172166

173167
HttpHeaders headers = this.outputMessage.getHeaders();
174168
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.US_ASCII)).isEqualTo(body);
175169
assertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
176170
assertThat(headers.getContentLength()).isEqualTo(body.getBytes().length);
177-
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
171+
assertThat(headers.getAcceptCharset()).isEmpty();
178172
}
179173

180174
}

0 commit comments

Comments
 (0)