1515import static org .junit .jupiter .api .EqualsAndHashCodeAssertions .assertEqualsAndHashCode ;
1616import static org .junit .platform .commons .test .PreconditionAssertions .assertPreconditionViolationFor ;
1717import static org .junit .platform .commons .test .PreconditionAssertions .assertPreconditionViolationNotNullFor ;
18+ import static org .junit .platform .commons .test .PreconditionAssertions .assertPreconditionViolationNotNullOrBlankFor ;
1819
20+ import org .jspecify .annotations .Nullable ;
1921import org .junit .jupiter .api .Nested ;
2022import org .junit .jupiter .api .Test ;
2123import org .junit .jupiter .params .ParameterizedTest ;
24+ import org .junit .jupiter .params .provider .NullAndEmptySource ;
2225import org .junit .jupiter .params .provider .ValueSource ;
2326
27+ /**
28+ * Unit tests for {@link MediaType}.
29+ */
2430class MediaTypeTests {
2531
2632 @ Test
@@ -35,14 +41,16 @@ void equals() {
3541 @ Nested
3642 class ParseTests {
3743
38- @ Test
39- @ SuppressWarnings ("DataFlowIssue" ) // MediaType.parse() parameter is not @Nullable
40- void parseWithNullMediaType () {
41- assertPreconditionViolationNotNullFor ("value" , () -> MediaType .parse (null ));
44+ @ ParameterizedTest
45+ @ NullAndEmptySource
46+ @ ValueSource (strings = { " " , " \t " })
47+ @ SuppressWarnings ("DataFlowIssue" ) // MediaType.create() parameters are not @Nullable
48+ void parseWithNullOrBlankMediaType (@ Nullable String mediaType ) {
49+ assertPreconditionViolationNotNullOrBlankFor ("value" , () -> MediaType .parse (mediaType ));
4250 }
4351
4452 @ ParameterizedTest
45- @ ValueSource (strings = { "" , " " , " /" , " / " , "type" , "type/" , "/subtype" })
53+ @ ValueSource (strings = { "/" , " / " , "type" , "type/" , "/subtype" })
4654 void parseWithInvalidMediaType (String mediaType ) {
4755 assertPreconditionViolationFor (() -> MediaType .parse (mediaType ))//
4856 .withMessage ("Invalid media type: '%s'" , mediaType .strip ());
@@ -59,16 +67,37 @@ void parse(String value) {
5967 @ Nested
6068 class CreateTests {
6169
62- @ Test
70+ @ ParameterizedTest
71+ @ NullAndEmptySource
72+ @ ValueSource (strings = { " " , " \t " })
6373 @ SuppressWarnings ("DataFlowIssue" ) // MediaType.create() parameters are not @Nullable
64- void createWithNullType ( ) {
65- assertPreconditionViolationNotNullFor ("type" , () -> MediaType .create (null , "json" ));
74+ void createWithNullOrBlankType ( @ Nullable String type ) {
75+ assertPreconditionViolationNotNullOrBlankFor ("type" , () -> MediaType .create (type , "json" ));
6676 }
6777
68- @ Test
78+ @ ParameterizedTest
79+ @ NullAndEmptySource
80+ @ ValueSource (strings = { " " , " \t " })
81+ @ SuppressWarnings ("DataFlowIssue" ) // MediaType.create() parameters are not @Nullable
82+ void createWithNullOrBlankTypeAndCharset (@ Nullable String type ) {
83+ assertPreconditionViolationNotNullOrBlankFor ("type" , () -> MediaType .create (type , "json" , UTF_8 ));
84+ }
85+
86+ @ ParameterizedTest
87+ @ NullAndEmptySource
88+ @ ValueSource (strings = { " " , " \t " })
89+ @ SuppressWarnings ("DataFlowIssue" ) // MediaType.create() parameters are not @Nullable
90+ void createWithNullOrBlankSubtype (@ Nullable String subtype ) {
91+ assertPreconditionViolationNotNullOrBlankFor ("subtype" , () -> MediaType .create ("application" , subtype ));
92+ }
93+
94+ @ ParameterizedTest
95+ @ NullAndEmptySource
96+ @ ValueSource (strings = { " " , " \t " })
6997 @ SuppressWarnings ("DataFlowIssue" ) // MediaType.create() parameters are not @Nullable
70- void createWithNullSubtype () {
71- assertPreconditionViolationNotNullFor ("subtype" , () -> MediaType .create ("json" , null ));
98+ void createWithNullOrBlankSubtypeAndCharset (@ Nullable String subtype ) {
99+ assertPreconditionViolationNotNullOrBlankFor ("subtype" ,
100+ () -> MediaType .create ("application" , subtype , UTF_8 ));
72101 }
73102
74103 @ Test
@@ -78,14 +107,14 @@ void createWithNullCharset() {
78107 }
79108
80109 @ ParameterizedTest
81- @ ValueSource (strings = { "" , " " , " /" , " / " , "type/" , "/subtype" })
110+ @ ValueSource (strings = { "/" , " / " , "type/" , "/subtype" })
82111 void createWithInvalidType (String type ) {
83112 assertPreconditionViolationFor (() -> MediaType .create (type , "json" ))//
84113 .withMessage ("Invalid media type: '%s/json'" , type .strip ());
85114 }
86115
87116 @ ParameterizedTest
88- @ ValueSource (strings = { "" , " " , " /" , " / " , "type/" , "/subtype" })
117+ @ ValueSource (strings = { "/" , " / " , "type/" , "/subtype" })
89118 void createWithInvalidSubtype (String subtype ) {
90119 assertPreconditionViolationFor (() -> MediaType .create ("application" , subtype ))//
91120 .withMessage ("Invalid media type: 'application/%s'" , subtype .strip ());
0 commit comments