|
10 | 10 |
|
11 | 11 | package org.junit.jupiter.api.extension; |
12 | 12 |
|
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; |
14 | 15 | import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode; |
15 | 16 | import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor; |
16 | 17 | import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullFor; |
17 | 18 |
|
18 | | -import java.nio.charset.StandardCharsets; |
19 | | - |
| 19 | +import org.junit.jupiter.api.Nested; |
20 | 20 | import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.ValueSource; |
21 | 23 |
|
22 | 24 | class MediaTypeTests { |
23 | 25 |
|
24 | 26 | @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; |
29 | 31 |
|
30 | | - @Test |
31 | | - void create() { |
32 | | - MediaType mediaType = MediaType.create("application", "json"); |
33 | | - assertEquals("application/json", mediaType.toString()); |
| 32 | + assertEqualsAndHashCode(mediaType1, mediaType2, mediaType3); |
34 | 33 | } |
35 | 34 |
|
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 { |
41 | 37 |
|
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 | + } |
46 | 43 |
|
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 | + } |
51 | 55 | } |
52 | 56 |
|
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 { |
58 | 59 |
|
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 | + } |
60 | 101 | } |
61 | 102 |
|
62 | 103 | } |
0 commit comments