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
Support scanning for classpath resources #3705
Open
mpkorstanje
wants to merge
15
commits into
junit-team:main
Choose a base branch
from
mpkorstanje:support-classpath-resources-scanning
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
Open
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
02ef108
Support scanning for class path resources
mpkorstanje 2f76493
Formatting
mpkorstanje 277272a
Doc
mpkorstanje f683deb
Fix
mpkorstanje c457b58
Clean up todos
mpkorstanje c9b3b0e
Clean up todos
mpkorstanje e5a1751
Clean up
mpkorstanje 2bab069
Extract common code in findClassesForPath and findResourcesForPath
mpkorstanje f34c4c9
Extract common file visitor code
mpkorstanje c84ad5e
Docs
mpkorstanje e43258e
Tests
mpkorstanje c374b93
Spotless
mpkorstanje 440e0e7
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje 6cca62a
Update CHANGELOG
mpkorstanje 704e584
Update CHANGELOG
mpkorstanje File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
61 changes: 61 additions & 0 deletions
61
junit-platform-commons/src/main/java/org/junit/platform/commons/support/Resource.java
This file contains 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,61 @@ | ||
/* | ||
* Copyright 2015-2024 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.support; | ||
|
||
import static org.apiguardian.api.API.Status.EXPERIMENTAL; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.util.function.Predicate; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* Represents a resource on the classpath. | ||
* | ||
* @see ReflectionSupport#findAllResourcesInClasspathRoot(URI, Predicate, Predicate) | ||
* @see ReflectionSupport#findAllResourcesInPackage(String, Predicate, Predicate) | ||
* @see ReflectionSupport#findAllResourcesInModule(String, Predicate, Predicate) | ||
* @see ReflectionSupport#streamAllResourcesInClasspathRoot(URI, Predicate, Predicate) | ||
* @see ReflectionSupport#streamAllResourcesInPackage(String, Predicate, Predicate) | ||
* @see ReflectionSupport#streamAllResourcesInModule(String, Predicate, Predicate) | ||
*/ | ||
@API(status = EXPERIMENTAL, since = "1.11") | ||
public interface Resource { | ||
|
||
/** | ||
* Get the resource name. | ||
* <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 URI to a resource. | ||
* | ||
* @return the uri of the resource; never {@code null} | ||
*/ | ||
URI getUri(); | ||
|
||
/** | ||
* Returns an input stream for reading this resource. | ||
* | ||
* @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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClasspathResource.java
This file contains 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,51 @@ | ||
/* | ||
* Copyright 2015-2024 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.util; | ||
|
||
import java.net.URI; | ||
import java.util.Objects; | ||
|
||
import org.junit.platform.commons.support.Resource; | ||
|
||
class ClasspathResource implements Resource { | ||
private final String name; | ||
private final URI uri; | ||
|
||
ClasspathResource(String name, URI uri) { | ||
this.name = Preconditions.notNull(name, "name must not be null"); | ||
this.uri = Preconditions.notNull(uri, "uri must not be null"); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public URI getUri() { | ||
return uri; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
ClasspathResource that = (ClasspathResource) o; | ||
return name.equals(that.name) && uri.equals(that.uri); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, uri); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have intentionally expanded the original definition of
Resource
somewhat.A few reasons:
To make it possible to use the package filter in the
EngineDiscoveryRequestResolver
in combination with the discovery to resources,ReflectionUtils
. needs aPredicate<String> resourceNameFilter
. ThePredicate<Resource> resourceFilter
should have access to the at least as much information as thePredicate<String>
.To actually implement the package filter with resources some more work will have to be done to get the resource path and change it from a
/
separated path into.
separated package name. But that is for later.