Skip to content

Commit eae3773

Browse files
committed
Introduce Resource.from() and make DefaultResource package-private
Closes #4879
1 parent 06e0742 commit eae3773

File tree

6 files changed

+20
-17
lines changed

6 files changed

+20
-17
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ guidance on upgrading from JUnit 5.x.y to 6.0.0.
3838
[[release-notes-6.0.0-junit-platform-new-features-and-improvements]]
3939
==== New Features and Improvements
4040

41-
* ❓
41+
* New `Resource.from(String, URI)` static factory method for creating an
42+
`org.junit.platform.commons.support.Resource`.
4243

4344

4445
[[release-notes-6.0.0-junit-jupiter]]

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,17 @@
1010

1111
package org.junit.platform.commons.support;
1212

13-
import static org.apiguardian.api.API.Status.INTERNAL;
14-
1513
import java.net.URI;
1614

17-
import org.apiguardian.api.API;
1815
import org.junit.platform.commons.util.Preconditions;
1916
import org.junit.platform.commons.util.ToStringBuilder;
2017

2118
/**
22-
* <h2>DISCLAIMER</h2>
23-
*
24-
* <p>This class is intended solely for usage within the JUnit framework itself.
25-
* <strong>Any usage by external parties is not supported.</strong>
26-
* Use at your own risk!
19+
* Default implementation of {@link Resource}.
2720
*
2821
* @since 1.11
2922
*/
30-
@API(status = INTERNAL, since = "1.12")
31-
public record DefaultResource(String name, URI uri) implements Resource {
23+
record DefaultResource(String name, URI uri) implements Resource {
3224

3325
public DefaultResource {
3426
Preconditions.notNull(name, "name must not be null");

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@
3838
@API(status = MAINTAINED, since = "1.13.3")
3939
public interface Resource {
4040

41+
/**
42+
* Create a new {@link Resource} with the given name and URI.
43+
*
44+
* @param name the name of the resource; never {@code null}
45+
* @param uri the URI of the resource; never {@code null}
46+
* @return a new {@code Resource}
47+
* @since 6.0
48+
*/
49+
@API(status = MAINTAINED, since = "6.0")
50+
static Resource from(String name, URI uri) {
51+
return new DefaultResource(name, uri);
52+
}
53+
4154
/**
4255
* Get the name of this resource.
4356
*

junit-platform-commons/src/main/java/org/junit/platform/commons/support/scanning/DefaultClasspathScanner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.junit.platform.commons.function.Try;
3939
import org.junit.platform.commons.logging.Logger;
4040
import org.junit.platform.commons.logging.LoggerFactory;
41-
import org.junit.platform.commons.support.DefaultResource;
4241
import org.junit.platform.commons.support.Resource;
4342
import org.junit.platform.commons.util.PackageUtils;
4443
import org.junit.platform.commons.util.Preconditions;
@@ -215,7 +214,7 @@ private void processResourceFileSafely(Path baseDir, String basePackageName, Pre
215214
try {
216215
String fullyQualifiedResourceName = determineFullyQualifiedResourceName(baseDir, basePackageName,
217216
resourceFile);
218-
Resource resource = new DefaultResource(fullyQualifiedResourceName, resourceFile.toUri());
217+
Resource resource = Resource.from(fullyQualifiedResourceName, resourceFile.toUri());
219218
if (resourceFilter.test(resource)) {
220219
resourceConsumer.accept(resource);
221220
}

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ModuleUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.junit.platform.commons.JUnitException;
3737
import org.junit.platform.commons.logging.Logger;
3838
import org.junit.platform.commons.logging.LoggerFactory;
39-
import org.junit.platform.commons.support.DefaultResource;
4039
import org.junit.platform.commons.support.Resource;
4140
import org.junit.platform.commons.support.scanning.ClassFilter;
4241

@@ -289,7 +288,7 @@ List<Resource> scan(ModuleReference reference) {
289288
private Resource loadResourceUnchecked(String binaryName) {
290289
try {
291290
URI uri = requireNonNull(classLoader.getResource(binaryName)).toURI();
292-
return new DefaultResource(binaryName, uri);
291+
return Resource.from(binaryName, uri);
293292
}
294293
catch (NullPointerException | URISyntaxException e) {
295294
throw new JUnitException("Failed to load resource with name '" + binaryName + "'.", e);

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import org.junit.platform.commons.function.Try;
6161
import org.junit.platform.commons.logging.Logger;
6262
import org.junit.platform.commons.logging.LoggerFactory;
63-
import org.junit.platform.commons.support.DefaultResource;
6463
import org.junit.platform.commons.support.Resource;
6564
import org.junit.platform.commons.support.scanning.ClassFilter;
6665
import org.junit.platform.commons.support.scanning.ClasspathScanner;
@@ -795,7 +794,7 @@ public static Try<Set<Resource>> tryToGetResources(String classpathResourceName,
795794
List<URL> resources = Collections.list(classLoader.getResources(canonicalClasspathResourceName));
796795
return resources.stream().map(url -> {
797796
try {
798-
return new DefaultResource(canonicalClasspathResourceName, url.toURI());
797+
return Resource.from(canonicalClasspathResourceName, url.toURI());
799798
}
800799
catch (URISyntaxException e) {
801800
throw ExceptionUtils.throwAsUncheckedException(e);

0 commit comments

Comments
 (0)