Skip to content

Commit 6da876b

Browse files
committed
Increase test coverage for MediaType
1 parent 0ec8d11 commit 6da876b

File tree

1 file changed

+71
-30
lines changed

1 file changed

+71
-30
lines changed

jupiter-tests/src/test/java/org/junit/jupiter/api/extension/MediaTypeTests.java

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,94 @@
1010

1111
package org.junit.jupiter.api.extension;
1212

13-
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static java.nio.charset.StandardCharsets.UTF_8;
14+
import static org.assertj.core.api.Assertions.assertThat;
1415
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;
1516
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
1617
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullFor;
1718

18-
import java.nio.charset.StandardCharsets;
19-
19+
import org.junit.jupiter.api.Nested;
2020
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
2123

2224
class MediaTypeTests {
2325

2426
@Test
25-
void parse() {
26-
MediaType mediaType = MediaType.parse("text/plain");
27-
assertEquals("text/plain", mediaType.toString());
28-
}
27+
void equals() {
28+
var mediaType1 = MediaType.TEXT_PLAIN;
29+
var mediaType2 = MediaType.parse(mediaType1.toString());
30+
var mediaType3 = MediaType.APPLICATION_JSON;
2931

30-
@Test
31-
void create() {
32-
MediaType mediaType = MediaType.create("application", "json");
33-
assertEquals("application/json", mediaType.toString());
32+
assertEqualsAndHashCode(mediaType1, mediaType2, mediaType3);
3433
}
3534

36-
@Test
37-
void createWithCharset() {
38-
MediaType mediaType = MediaType.create("application", "json", StandardCharsets.UTF_8);
39-
assertEquals("application/json; charset=UTF-8", mediaType.toString());
40-
}
35+
@Nested
36+
class ParseTests {
4137

42-
@Test
43-
void parseWithInvalidMediaType() {
44-
assertPreconditionViolationFor(() -> MediaType.parse("invalid")).withMessage("Invalid media type: 'invalid'");
45-
}
38+
@Test
39+
@SuppressWarnings("DataFlowIssue") // MediaType.parse() parameter is not @Nullable
40+
void parseWithNullMediaType() {
41+
assertPreconditionViolationNotNullFor("value", () -> MediaType.parse(null));
42+
}
4643

47-
@SuppressWarnings("DataFlowIssue")
48-
@Test
49-
void parseWithNullMediaType() {
50-
assertPreconditionViolationNotNullFor("value", () -> MediaType.parse(null));
44+
@ParameterizedTest
45+
@ValueSource(strings = { "", " ", "/", " / ", "type", "type/", "/subtype" })
46+
void parseWithInvalidMediaType(String mediaType) {
47+
assertPreconditionViolationFor(() -> MediaType.parse(mediaType))//
48+
.withMessage("Invalid media type: '%s'", mediaType);
49+
}
50+
51+
@Test
52+
void parse() {
53+
assertThat(MediaType.parse("text/plain")).hasToString("text/plain");
54+
}
5155
}
5256

53-
@Test
54-
void equals() {
55-
MediaType mediaType1 = MediaType.TEXT_PLAIN;
56-
MediaType mediaType2 = MediaType.parse("text/plain");
57-
MediaType mediaType3 = MediaType.parse("application/json");
57+
@Nested
58+
class CreateTests {
5859

59-
assertEqualsAndHashCode(mediaType1, mediaType2, mediaType3);
60+
@Test
61+
@SuppressWarnings("DataFlowIssue") // MediaType.create() parameters are not @Nullable
62+
void createWithNullType() {
63+
assertPreconditionViolationNotNullFor("type", () -> MediaType.create(null, "json"));
64+
}
65+
66+
@Test
67+
@SuppressWarnings("DataFlowIssue") // MediaType.create() parameters are not @Nullable
68+
void createWithNullSubtype() {
69+
assertPreconditionViolationNotNullFor("subtype", () -> MediaType.create("json", null));
70+
}
71+
72+
@Test
73+
@SuppressWarnings("DataFlowIssue") // MediaType.create() parameters are not @Nullable
74+
void createWithNullCharset() {
75+
assertPreconditionViolationNotNullFor("charset", () -> MediaType.create("application", "json", null));
76+
}
77+
78+
@ParameterizedTest
79+
@ValueSource(strings = { "", " ", "/", " / ", "type/", "/subtype" })
80+
void createWithInvalidType(String type) {
81+
assertPreconditionViolationFor(() -> MediaType.create(type, "json"))//
82+
.withMessage("Invalid media type: '%s/json'", type);
83+
}
84+
85+
@ParameterizedTest
86+
@ValueSource(strings = { "", " ", "/", " / ", "type/", "/subtype" })
87+
void createWithInvalidSubtype(String subtype) {
88+
assertPreconditionViolationFor(() -> MediaType.create("application", subtype))//
89+
.withMessage("Invalid media type: 'application/%s'", subtype);
90+
}
91+
92+
@Test
93+
void create() {
94+
assertThat(MediaType.create("application", "json")).hasToString("application/json");
95+
}
96+
97+
@Test
98+
void createWithCharset() {
99+
assertThat(MediaType.create("text", "plain", UTF_8)).hasToString("text/plain; charset=UTF-8");
100+
}
60101
}
61102

62103
}

0 commit comments

Comments
 (0)