Skip to content

Commit 0a89968

Browse files
committed
Make MediaTyped sealed
1 parent 799e749 commit 0a89968

File tree

2 files changed

+2
-36
lines changed

2 files changed

+2
-36
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/MediaType.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
* @see TestReporter#publishFile(String, MediaType, org.junit.jupiter.api.function.ThrowingConsumer)
3535
* @see org.junit.jupiter.api.extension.ExtensionContext#publishFile(String, MediaType, org.junit.jupiter.api.function.ThrowingConsumer)
3636
*/
37+
@SuppressWarnings("removal")
3738
@API(status = MAINTAINED, since = "5.14")
38-
public class MediaType {
39+
public sealed class MediaType permits org.junit.jupiter.api.extension.MediaType {
3940

4041
private static final Pattern PATTERN;
4142

@@ -125,13 +126,6 @@ protected MediaType(String type, String subtype, @Nullable Charset charset) {
125126
}
126127

127128
protected MediaType(String value) {
128-
// Mimic sealed types, permitting only this class and api.extension.MediaType.
129-
if (getClass() != MediaType.class
130-
&& !getClass().getName().equals("org.junit.jupiter.api.extension.MediaType")) {
131-
throw new IllegalStateException(
132-
"Type '%s' is not permitted to extend MediaType".formatted(getClass().getName()));
133-
}
134-
135129
String strippedValue = Preconditions.notBlank(value, "value must not be null or blank").strip();
136130
Matcher matcher = PATTERN.matcher(strippedValue);
137131
Preconditions.condition(matcher.matches(), () -> "Invalid media type: '" + strippedValue + "'");

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212

1313
import static java.nio.charset.StandardCharsets.UTF_8;
1414
import static org.assertj.core.api.Assertions.assertThat;
15-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
1615
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;
1716
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
1817
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullFor;
1918
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullOrBlankFor;
2019

21-
import java.nio.charset.Charset;
22-
2320
import org.jspecify.annotations.Nullable;
2421
import org.junit.jupiter.params.ParameterizedTest;
2522
import org.junit.jupiter.params.provider.NullAndEmptySource;
@@ -33,31 +30,6 @@
3330
*/
3431
class MediaTypeTests {
3532

36-
@Test
37-
void pseudoSealedType() {
38-
39-
class CustomMediaType extends MediaType {
40-
41-
CustomMediaType(String value) {
42-
super(value);
43-
}
44-
45-
CustomMediaType(String type, String subtype, @Nullable Charset charset) {
46-
super(type, subtype, charset);
47-
}
48-
}
49-
50-
String message = "Type '%s' is not permitted to extend MediaType".formatted(CustomMediaType.class.getName());
51-
52-
assertThatIllegalStateException()//
53-
.isThrownBy(() -> new CustomMediaType("text/plain"))//
54-
.withMessage(message);
55-
56-
assertThatIllegalStateException()//
57-
.isThrownBy(() -> new CustomMediaType("text", "plain", UTF_8))//
58-
.withMessage(message);
59-
}
60-
6133
@Test
6234
void equals() {
6335
var mediaType1 = MediaType.TEXT_PLAIN;

0 commit comments

Comments
 (0)