Skip to content

Commit 759a037

Browse files
committed
Introduce org.junit.platform.commons.io.Resource
Resolves #4876.
1 parent e47c253 commit 759a037

File tree

30 files changed

+1040
-172
lines changed

30 files changed

+1040
-172
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-RC3.adoc

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,36 @@ JUnit repository on GitHub.
3232
- `LauncherDiscoveryRequestBuilder.outputDirectoryProvider(OutputDirectoryProvider)`
3333
- `TestPlan.getOutputDirectoryProvider()`
3434
- `EngineTestKit.Builder.outputDirectoryProvider(OutputDirectoryProvider)`
35+
* Deprecate `org.junit.platform.commons.support.Resource` interface in favor of the new
36+
`org.junit.platform.commons.io.Resource` one.
37+
* Deprecate `Resource`-related methods in `ReflectionSupport` in favor of corresponding
38+
methods in new `ResourceSupport` class:
39+
- `findAllResourcesInClasspathRoot(URI, Predicate)`
40+
- `findAllResourcesInModule(String, Predicate)`
41+
- `findAllResourcesInPackage(String, Predicate)`
42+
- `streamAllResourcesInClasspathRoot(URI, Predicate)`
43+
- `streamAllResourcesInModule(String, Predicate)`
44+
- `streamAllResourcesInPackage(String, Predicate)`
45+
- `tryToGetResources(String)`
46+
- `tryToGetResources(String, ClassLoader)`
47+
* Deprecate `DiscoverySelectors.selectClasspathResource(Set)` method in favor of
48+
`selectClasspathResourceByName(Set)`.
49+
* Deprecate `ClasspathResourceSelector.getClasspathResources()` method in favor of
50+
`getResources()`.
51+
* Deprecate
52+
`EngineDiscoveryRequestResolver.Builder.addResourceContainerSelectorResolver(Predicate)`
53+
method in favor of `addResourceContainerSelectorResolver(ResourceFilter)`.
54+
* Deprecate `Resource`-related methods in `ClasspathScanner` in favor of new methods using
55+
`org.junit.platform.commons.io.Resource` and `ResourceFilter`:
56+
- `scanForResourcesInPackage(String, Predicate)`
57+
- `scanForResourcesInClasspathRoot(URI, Predicate)`
3558

3659
[[release-notes-6.0.0-RC3-junit-platform-new-features-and-improvements]]
3760
==== New Features and Improvements
3861

39-
* New `Resource.from(String, URI)` static factory method for creating an
40-
`org.junit.platform.commons.support.Resource`.
62+
* New classpath resource abstraction in `org.junit.platform.commons.io.Resource` with
63+
support class for loading resources or finding them on the classpath via static utility
64+
methods in the new `org.junit.platform.commons.support.ResourceSupport` class.
4165

4266

4367
[[release-notes-6.0.0-RC3-junit-jupiter]]

junit-platform-commons/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
exports org.junit.platform.commons;
2828
exports org.junit.platform.commons.annotation;
2929
exports org.junit.platform.commons.function;
30+
exports org.junit.platform.commons.io;
3031
exports org.junit.platform.commons.logging to
3132
org.junit.jupiter.api,
3233
org.junit.jupiter.engine,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.io;
12+
13+
import java.net.URI;
14+
15+
import org.jspecify.annotations.Nullable;
16+
import org.junit.platform.commons.PreconditionViolationException;
17+
import org.junit.platform.commons.annotation.Contract;
18+
19+
/**
20+
* Default implementation of {@link Resource}.
21+
*
22+
* @since 6.0
23+
*/
24+
record DefaultResource(String name, URI uri) implements Resource {
25+
26+
DefaultResource {
27+
checkNotNull(name, "name");
28+
checkNotNull(uri, "uri");
29+
}
30+
31+
@Override
32+
public String getName() {
33+
return this.name;
34+
}
35+
36+
@Override
37+
public URI getUri() {
38+
return this.uri;
39+
}
40+
41+
// Cannot use Preconditions due to package cycle
42+
@Contract("null, _ -> fail; !null, _ -> param1")
43+
private static <T> void checkNotNull(@Nullable T input, String title) {
44+
if (input == null) {
45+
throw new PreconditionViolationException(title + " must not be null");
46+
}
47+
}
48+
49+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.io;
12+
13+
import static org.apiguardian.api.API.Status.MAINTAINED;
14+
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.net.URI;
18+
19+
import org.apiguardian.api.API;
20+
21+
/**
22+
* {@code Resource} represents a resource on the classpath.
23+
*
24+
* <p><strong>WARNING</strong>: a {@code Resource} must provide correct
25+
* {@link Object#equals(Object) equals} and {@link Object#hashCode() hashCode}
26+
* implementations since a {@code Resource} may potentially be stored in a
27+
* collection or map.
28+
*
29+
* @since 6.0
30+
* @see org.junit.platform.commons.support.ResourceSupport#findAllResourcesInClasspathRoot(URI, ResourceFilter)
31+
* @see org.junit.platform.commons.support.ResourceSupport#findAllResourcesInPackage(String, ResourceFilter)
32+
* @see org.junit.platform.commons.support.ResourceSupport#findAllResourcesInModule(String, ResourceFilter)
33+
* @see org.junit.platform.commons.support.ResourceSupport#streamAllResourcesInClasspathRoot(URI, ResourceFilter)
34+
* @see org.junit.platform.commons.support.ResourceSupport#streamAllResourcesInPackage(String, ResourceFilter)
35+
* @see org.junit.platform.commons.support.ResourceSupport#streamAllResourcesInModule(String, ResourceFilter)
36+
*/
37+
@API(status = MAINTAINED, since = "6.0")
38+
public interface Resource {
39+
40+
/**
41+
* Create a new {@link Resource} with the given name and URI.
42+
*
43+
* @param name the name of the resource; never {@code null}
44+
* @param uri the URI of the resource; never {@code null}
45+
* @return a new {@code Resource}
46+
* @since 6.0
47+
*/
48+
static Resource of(String name, URI uri) {
49+
return new DefaultResource(name, uri);
50+
}
51+
52+
/**
53+
* Get the name of this resource.
54+
*
55+
* <p>The resource name is a {@code /}-separated path. The path is relative
56+
* to the classpath root in which the resource is located.
57+
*
58+
* @return the resource name; never {@code null}
59+
*/
60+
String getName();
61+
62+
/**
63+
* Get the URI of this resource.
64+
*
65+
* @return the URI of the resource; never {@code null}
66+
*/
67+
URI getUri();
68+
69+
/**
70+
* Get an {@link InputStream} for reading this resource.
71+
*
72+
* <p>The default implementation delegates to {@link java.net.URL#openStream()}
73+
* for this resource's {@link #getUri() URI}.
74+
*
75+
* @return an input stream for this resource; never {@code null}
76+
* @throws IOException if an I/O exception occurs
77+
*/
78+
default InputStream getInputStream() throws IOException {
79+
return getUri().toURL().openStream();
80+
}
81+
82+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.io;
12+
13+
import static org.apiguardian.api.API.Status.MAINTAINED;
14+
15+
import java.util.function.Predicate;
16+
17+
import org.apiguardian.api.API;
18+
19+
/**
20+
* Resource filter used by reflection and classpath scanning support.
21+
*
22+
* @since 6.0
23+
* @see Resource
24+
*/
25+
@API(status = MAINTAINED, since = "6.0")
26+
public class ResourceFilter {
27+
28+
/**
29+
* Create a {@link ResourceFilter} instance from a predicate.
30+
*
31+
* @param resourcePredicate the resource predicate; never {@code null}
32+
* @return an instance of {@code ResourceFilter}; never {@code null}
33+
*/
34+
public static ResourceFilter of(Predicate<? super Resource> resourcePredicate) {
35+
return new ResourceFilter(resourcePredicate);
36+
}
37+
38+
private final Predicate<? super Resource> predicate;
39+
40+
private ResourceFilter(Predicate<? super Resource> predicate) {
41+
this.predicate = predicate;
42+
}
43+
44+
public boolean match(Resource resource) {
45+
return predicate.test(resource);
46+
}
47+
48+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* IO-related interfaces and support classes
3+
*/
4+
5+
@NullMarked
6+
package org.junit.platform.commons.io;
7+
8+
import org.jspecify.annotations.NullMarked;

junit-platform-commons/src/main/java/org/junit/platform/commons/support/AnnotationSupport.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @see ClassSupport
4545
* @see ModifierSupport
4646
* @see ReflectionSupport
47+
* @see ResourceSupport
4748
*/
4849
@API(status = MAINTAINED, since = "1.0")
4950
public final class AnnotationSupport {

junit-platform-commons/src/main/java/org/junit/platform/commons/support/ClassSupport.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @see AnnotationSupport
3232
* @see ModifierSupport
3333
* @see ReflectionSupport
34+
* @see ResourceSupport
3435
*/
3536
@API(status = MAINTAINED, since = "1.1")
3637
public final class ClassSupport {

junit-platform-commons/src/main/java/org/junit/platform/commons/support/DefaultResource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* @since 1.11
2222
*/
23+
@SuppressWarnings("removal")
2324
record DefaultResource(String name, URI uri) implements Resource {
2425

2526
public DefaultResource {

junit-platform-commons/src/main/java/org/junit/platform/commons/support/ModifierSupport.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @see AnnotationSupport
3434
* @see ClassSupport
3535
* @see ReflectionSupport
36+
* @see ResourceSupport
3637
*/
3738
@API(status = MAINTAINED, since = "1.4")
3839
public final class ModifierSupport {

0 commit comments

Comments
 (0)