-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Relocate Resource
to org.junit.platform.commons.io
package
#4891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marcphilipp
wants to merge
8
commits into
main
Choose a base branch
from
marc/4876-resource-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,099
−172
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a08acf9
Introduce `org.junit.platform.commons.io.Resource`
marcphilipp 6b58202
Simplify `ClasspathScanner`
marcphilipp 6757688
Accept broken binary compatibility for making abstract method to default
marcphilipp ebc6a85
Make `Resource` implementation interoperable
marcphilipp 9f8b5f1
Remove usage of `ToStringBuilder` to avoid package cycle
marcphilipp d3469fa
Fix release notes entry
marcphilipp 839b8ee
Add explicit `this`
marcphilipp c5051f4
Merge branch 'main' into marc/4876-resource-package
marcphilipp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
junit-platform-commons/src/main/java/org/junit/platform/commons/io/DefaultResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.commons.io; | ||
|
||
import java.net.URI; | ||
import java.util.Objects; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
import org.junit.platform.commons.PreconditionViolationException; | ||
import org.junit.platform.commons.annotation.Contract; | ||
|
||
/** | ||
* Default implementation of {@link Resource}. | ||
* | ||
* @since 6.0 | ||
*/ | ||
record DefaultResource(String name, URI uri) implements Resource { | ||
|
||
DefaultResource { | ||
checkNotNull(name, "name"); | ||
checkNotNull(uri, "uri"); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public URI getUri() { | ||
return this.uri; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (obj instanceof org.junit.platform.commons.io.Resource that) { | ||
return this.name.equals(that.getName()) // | ||
&& this.uri.equals(that.getUri()); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, uri); | ||
} | ||
|
||
// Cannot use Preconditions due to package cycle | ||
@Contract("null, _ -> fail; !null, _ -> param1") | ||
private static <T> void checkNotNull(@Nullable T input, String title) { | ||
if (input == null) { | ||
throw new PreconditionViolationException(title + " must not be null"); | ||
} | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
junit-platform-commons/src/main/java/org/junit/platform/commons/io/Resource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.commons.io; | ||
|
||
import static org.apiguardian.api.API.Status.MAINTAINED; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* {@code Resource} represents a resource on the classpath. | ||
* | ||
* <p><strong>WARNING</strong>: a {@code Resource} must provide correct | ||
* {@link Object#equals(Object) equals} and {@link Object#hashCode() hashCode} | ||
* implementations since a {@code Resource} may potentially be stored in a | ||
* collection or map. | ||
* | ||
* @since 6.0 | ||
* @see org.junit.platform.commons.support.ResourceSupport#findAllResourcesInClasspathRoot(URI, ResourceFilter) | ||
* @see org.junit.platform.commons.support.ResourceSupport#findAllResourcesInPackage(String, ResourceFilter) | ||
* @see org.junit.platform.commons.support.ResourceSupport#findAllResourcesInModule(String, ResourceFilter) | ||
* @see org.junit.platform.commons.support.ResourceSupport#streamAllResourcesInClasspathRoot(URI, ResourceFilter) | ||
* @see org.junit.platform.commons.support.ResourceSupport#streamAllResourcesInPackage(String, ResourceFilter) | ||
* @see org.junit.platform.commons.support.ResourceSupport#streamAllResourcesInModule(String, ResourceFilter) | ||
*/ | ||
@API(status = MAINTAINED, since = "6.0") | ||
public interface Resource { | ||
|
||
/** | ||
* Create a new {@link Resource} with the given name and URI. | ||
* | ||
* @param name the name of the resource; never {@code null} | ||
* @param uri the URI of the resource; never {@code null} | ||
* @return a new {@code Resource} | ||
* @since 6.0 | ||
*/ | ||
static Resource of(String name, URI uri) { | ||
return new DefaultResource(name, uri); | ||
} | ||
|
||
/** | ||
* Get the name of this resource. | ||
* | ||
* <p>The resource name is a {@code /}-separated path. The path is relative | ||
* to the classpath root in which the resource is located. | ||
* | ||
* @return the resource name; never {@code null} | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Get the URI of this resource. | ||
* | ||
* @return the URI of the resource; never {@code null} | ||
*/ | ||
URI getUri(); | ||
|
||
/** | ||
* Get an {@link InputStream} for reading this resource. | ||
* | ||
* <p>The default implementation delegates to {@link java.net.URL#openStream()} | ||
* for this resource's {@link #getUri() URI}. | ||
* | ||
* @return an input stream for this resource; never {@code null} | ||
* @throws IOException if an I/O exception occurs | ||
*/ | ||
default InputStream getInputStream() throws IOException { | ||
return getUri().toURL().openStream(); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
junit-platform-commons/src/main/java/org/junit/platform/commons/io/ResourceFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.commons.io; | ||
|
||
import static org.apiguardian.api.API.Status.MAINTAINED; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* Resource filter used by reflection and classpath scanning support. | ||
* | ||
* @since 6.0 | ||
* @see Resource | ||
*/ | ||
@API(status = MAINTAINED, since = "6.0") | ||
public class ResourceFilter { | ||
|
||
/** | ||
* Create a {@link ResourceFilter} instance from a predicate. | ||
* | ||
* @param resourcePredicate the resource predicate; never {@code null} | ||
* @return an instance of {@code ResourceFilter}; never {@code null} | ||
*/ | ||
public static ResourceFilter of(Predicate<? super Resource> resourcePredicate) { | ||
return new ResourceFilter(resourcePredicate); | ||
} | ||
|
||
private final Predicate<? super Resource> predicate; | ||
|
||
private ResourceFilter(Predicate<? super Resource> predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
public boolean match(Resource resource) { | ||
return predicate.test(resource); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
junit-platform-commons/src/main/java/org/junit/platform/commons/io/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* IO-related interfaces and support classes | ||
*/ | ||
|
||
@NullMarked | ||
package org.junit.platform.commons.io; | ||
|
||
import org.jspecify.annotations.NullMarked; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.