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
base: main
Are you sure you want to change the base?
Changes from 14 commits
02ef108
2f76493
277272a
f683deb
c457b58
c9b3b0e
e5a1751
2bab069
f34c4c9
c84ad5e
e43258e
c374b93
440e0e7
6cca62a
704e584
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.nio.file.Path; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* @since 1.11 | ||
*/ | ||
class ClasspathFilters { | ||
|
||
static final String CLASS_FILE_SUFFIX = ".class"; | ||
private static final String PACKAGE_INFO_FILE_NAME = "package-info" + CLASS_FILE_SUFFIX; | ||
private static final String MODULE_INFO_FILE_NAME = "module-info" + CLASS_FILE_SUFFIX; | ||
|
||
static Predicate<Path> classFiles() { | ||
return file -> isNotPackageInfo(file) && isNotModuleInfo(file) && isClassFile(file); | ||
} | ||
|
||
static Predicate<Path> resourceFiles() { | ||
return file -> !isClassFile(file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am explicitly excluding |
||
} | ||
|
||
private static boolean isNotPackageInfo(Path path) { | ||
return !path.endsWith(PACKAGE_INFO_FILE_NAME); | ||
} | ||
|
||
private static boolean isNotModuleInfo(Path path) { | ||
return !path.endsWith(MODULE_INFO_FILE_NAME); | ||
} | ||
|
||
private static boolean isClassFile(Path file) { | ||
return file.getFileName().toString().endsWith(CLASS_FILE_SUFFIX); | ||
} | ||
|
||
} |
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.