Skip to content

Support scanning for classpath resources #3705

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
02ef108
Support scanning for class path resources
mpkorstanje Jan 9, 2024
2f76493
Formatting
mpkorstanje Feb 25, 2024
277272a
Doc
mpkorstanje Feb 25, 2024
f683deb
Fix
mpkorstanje Feb 25, 2024
c457b58
Clean up todos
mpkorstanje Feb 29, 2024
c9b3b0e
Clean up todos
mpkorstanje Feb 29, 2024
e5a1751
Clean up
mpkorstanje Mar 7, 2024
2bab069
Extract common code in findClassesForPath and findResourcesForPath
mpkorstanje Mar 7, 2024
f34c4c9
Extract common file visitor code
mpkorstanje Mar 7, 2024
c84ad5e
Docs
mpkorstanje Mar 7, 2024
e43258e
Tests
mpkorstanje Mar 7, 2024
c374b93
Spotless
mpkorstanje Mar 7, 2024
440e0e7
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Mar 7, 2024
6cca62a
Update CHANGELOG
mpkorstanje Mar 7, 2024
704e584
Update CHANGELOG
mpkorstanje Mar 7, 2024
9f99477
Remove resource name filter predicate
mpkorstanje Jun 13, 2024
2df5201
Clean up to do.
mpkorstanje Jun 13, 2024
862cb3b
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 13, 2024
f5d892b
Replace resource filter with predicate
mpkorstanje Jun 14, 2024
6e95eeb
Revert "Replace resource filter with predicate"
mpkorstanje Jun 14, 2024
e59cc59
Revert "Remove resource name filter predicate"
mpkorstanje Jun 14, 2024
767dcf4
Load canonical resources through classloader
mpkorstanje Jun 14, 2024
94215bc
Touch up
mpkorstanje Jun 14, 2024
794793a
Correctly resolve shadowed resources
mpkorstanje Jun 14, 2024
ac77e69
Fixup changelog
mpkorstanje Jun 14, 2024
79f13bf
Fix up java doc
mpkorstanje Jun 14, 2024
cdeb3ea
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 14, 2024
966e21f
Fix tryToLoadResource
mpkorstanje Jun 15, 2024
358693f
Wrap class loader call in try
mpkorstanje Jun 15, 2024
917f004
Add more tryToLoadResource tests
mpkorstanje Jun 15, 2024
ca1f81b
Touchups
mpkorstanje Jun 15, 2024
59705ba
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 15, 2024
7ffff49
Remove unnesesary try-catch
mpkorstanje Jun 15, 2024
2144abe
Add more reflection support tests
mpkorstanje Jun 15, 2024
27c0750
Merge branch 'main' into support-classpath-resources-scanning
mpkorstanje Jun 17, 2024
52e5884
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 26, 2024
7df46a3
Find all available resources, including duplicates
mpkorstanje Jul 8, 2024
ee7b42a
Remove unused tryToLoadResource
mpkorstanje Jul 8, 2024
c0c9ee6
Suppress warnings
mpkorstanje Jul 8, 2024
8621935
Remove resourceNameFilter predicate from Javadoc
marcphilipp Jul 9, 2024
3a14696
Add test for finding duplicate resources
marcphilipp Jul 9, 2024
3f348b3
Merge branch 'main' into support-classpath-resources-scanning
marcphilipp Jul 9, 2024
b826b32
Clean up unused import
mpkorstanje Jul 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ repository on GitHub.
* New `rootCause()` condition in `TestExecutionResultConditions` that matches if an
exception's _root_ cause matches all supplied conditions, for use with the
`EngineTestKit`.
* `ReflectionSupport` now supports scanning for classpath resources.


[[release-notes-5.11.0-RC1-junit-jupiter]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ private ReflectionSupport() {
*/
@API(status = DEPRECATED, since = "1.4")
@Deprecated
@SuppressWarnings("deprecation")
public static Optional<Class<?>> loadClass(String name) {
return ReflectionUtils.loadClass(name);
}
Expand Down Expand Up @@ -137,6 +136,28 @@ public static List<Class<?>> findAllClassesInClasspathRoot(URI root, Predicate<C
return ReflectionUtils.findAllClassesInClasspathRoot(root, classFilter, classNameFilter);
}

/**
* Find all {@linkplain Resource resources} in the supplied classpath {@code root}
* that match the specified {@code resourceFilter} predicate.
*
* <p>The classpath scanning algorithm searches recursively in subpackages
* beginning with the root of the classpath.
*
* @param root the URI for the classpath root in which to scan; never
* {@code null}
* @param resourceFilter the resource type filter; never {@code null}
* @return an immutable list of all such resources found; never {@code null}
* but potentially empty
* @since 1.11
* @see #findAllResourcesInPackage(String, Predicate)
* @see #findAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static List<Resource> findAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter) {

return ReflectionUtils.findAllResourcesInClasspathRoot(root, resourceFilter);
}

/**
* Find all {@linkplain Class classes} in the supplied classpath {@code root}
* that match the specified {@code classFilter} and {@code classNameFilter}
Expand All @@ -162,6 +183,28 @@ public static Stream<Class<?>> streamAllClassesInClasspathRoot(URI root, Predica
return ReflectionUtils.streamAllClassesInClasspathRoot(root, classFilter, classNameFilter);
}

/**
* Find all {@linkplain Resource resources} in the supplied classpath {@code root}
* that match the specified {@code resourceFilter} predicate.
*
* <p>The classpath scanning algorithm searches recursively in subpackages
* beginning with the root of the classpath.
*
* @param root the URI for the classpath root in which to scan; never
* {@code null}
* @param resourceFilter the resource type filter; never {@code null}
* @return a stream of all such classes found; never {@code null}
* but potentially empty
* @since 1.11
* @see #streamAllResourcesInPackage(String, Predicate)
* @see #streamAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static Stream<Resource> streamAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter) {

return ReflectionUtils.streamAllResourcesInClasspathRoot(root, resourceFilter);
}

/**
* Find all {@linkplain Class classes} in the supplied {@code basePackageName}
* that match the specified {@code classFilter} and {@code classNameFilter}
Expand All @@ -186,6 +229,29 @@ public static List<Class<?>> findAllClassesInPackage(String basePackageName, Pre
return ReflectionUtils.findAllClassesInPackage(basePackageName, classFilter, classNameFilter);
}

/**
* Find all {@linkplain Resource resources} in the supplied {@code basePackageName}
* that match the specified {@code resourceFilter} predicate.
*
* <p>The classpath scanning algorithm searches recursively in subpackages
* beginning within the supplied base package.
*
* @param basePackageName the name of the base package in which to start
* scanning; must not be {@code null} and must be valid in terms of Java
* syntax
* @param resourceFilter the resource type filter; never {@code null}
* @return an immutable list of all such classes found; never {@code null}
* but potentially empty
* @since 1.11
* @see #findAllResourcesInClasspathRoot(URI, Predicate)
* @see #findAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static List<Resource> findAllResourcesInPackage(String basePackageName, Predicate<Resource> resourceFilter) {

return ReflectionUtils.findAllResourcesInPackage(basePackageName, resourceFilter);
}

/**
* Find all {@linkplain Class classes} in the supplied {@code basePackageName}
* that match the specified {@code classFilter} and {@code classNameFilter}
Expand All @@ -212,6 +278,30 @@ public static Stream<Class<?>> streamAllClassesInPackage(String basePackageName,
return ReflectionUtils.streamAllClassesInPackage(basePackageName, classFilter, classNameFilter);
}

/**
* Find all {@linkplain Resource resources} in the supplied {@code basePackageName}
* that match the specified {@code resourceFilter} predicate.
*
* <p>The classpath scanning algorithm searches recursively in subpackages
* beginning within the supplied base package.
*
* @param basePackageName the name of the base package in which to start
* scanning; must not be {@code null} and must be valid in terms of Java
* syntax
* @param resourceFilter the resource type filter; never {@code null}
* @return a stream of all such resources found; never {@code null}
* but potentially empty
* @since 1.11
* @see #streamAllResourcesInClasspathRoot(URI, Predicate)
* @see #streamAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static Stream<Resource> streamAllResourcesInPackage(String basePackageName,
Predicate<Resource> resourceFilter) {

return ReflectionUtils.streamAllResourcesInPackage(basePackageName, resourceFilter);
}

/**
* Find all {@linkplain Class classes} in the supplied {@code moduleName}
* that match the specified {@code classFilter} and {@code classNameFilter}
Expand All @@ -236,6 +326,28 @@ public static List<Class<?>> findAllClassesInModule(String moduleName, Predicate
return ReflectionUtils.findAllClassesInModule(moduleName, classFilter, classNameFilter);
}

/**
* Find all {@linkplain Resource resources} in the supplied {@code moduleName}
* that match the specified {@code resourceFilter} predicate.
*
* <p>The module-path scanning algorithm searches recursively in all
* packages contained in the module.
*
* @param moduleName the name of the module to scan; never {@code null} or
* <em>empty</em>
* @param resourceFilter the resource type filter; never {@code null}
* @return an immutable list of all such resources found; never {@code null}
* but potentially empty
* @since 1.11
* @see #findAllResourcesInClasspathRoot(URI, Predicate)
* @see #findAllResourcesInPackage(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static List<Resource> findAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter) {

return ReflectionUtils.findAllResourcesInModule(moduleName, resourceFilter);
}

/**
* Find all {@linkplain Class classes} in the supplied {@code moduleName}
* that match the specified {@code classFilter} and {@code classNameFilter}
Expand All @@ -261,6 +373,28 @@ public static Stream<Class<?>> streamAllClassesInModule(String moduleName, Predi
return ReflectionUtils.streamAllClassesInModule(moduleName, classFilter, classNameFilter);
}

/**
* Find all {@linkplain Resource resources} in the supplied {@code moduleName}
* that match the specified {@code resourceFilter} predicate.
*
* <p>The module-path scanning algorithm searches recursively in all
* packages contained in the module.
*
* @param moduleName the name of the module to scan; never {@code null} or
* <em>empty</em>
* @param resourceFilter the resource type filter; never {@code null}
* @return a stream of all such resources found; never {@code null}
* but potentially empty
* @since 1.11
* @see #streamAllResourcesInClasspathRoot(URI, Predicate)
* @see #streamAllResourcesInPackage(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static Stream<Resource> streamAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter) {

return ReflectionUtils.streamAllResourcesInModule(moduleName, resourceFilter);
}

/**
* Create a new instance of the specified {@link Class} by invoking
* the constructor whose argument list matches the types of the supplied
Expand Down
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.
* @since 1.11
* @see ReflectionSupport#findAllResourcesInClasspathRoot(URI, Predicate)
* @see ReflectionSupport#findAllResourcesInPackage(String, Predicate)
* @see ReflectionSupport#findAllResourcesInModule(String, Predicate)
* @see ReflectionSupport#streamAllResourcesInClasspathRoot(URI, Predicate)
* @see ReflectionSupport#streamAllResourcesInPackage(String, Predicate)
* @see ReflectionSupport#streamAllResourcesInModule(String, 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
Expand Up @@ -17,32 +17,33 @@
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;

/**
* @since 1.0
* @since 1.11
*/
class ClassFileVisitor extends SimpleFileVisitor<Path> {
class ClasspathFileVisitor extends SimpleFileVisitor<Path> {

private static final Logger logger = LoggerFactory.getLogger(ClassFileVisitor.class);
private static final Logger logger = LoggerFactory.getLogger(ClasspathFileVisitor.class);

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;
private final Path basePath;
private final BiConsumer<Path, Path> consumer;
private final Predicate<Path> filter;

private final Consumer<Path> classFileConsumer;

ClassFileVisitor(Consumer<Path> classFileConsumer) {
this.classFileConsumer = classFileConsumer;
ClasspathFileVisitor(Path basePath, Predicate<Path> filter, BiConsumer<Path, Path> consumer) {
this.basePath = basePath;
this.filter = filter;
this.consumer = consumer;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
if (isNotPackageInfo(file) && isNotModuleInfo(file) && isClassFile(file)) {
classFileConsumer.accept(file);
if (filter.test(file)) {
consumer.accept(basePath, file);
}
return CONTINUE;
}
Expand All @@ -61,16 +62,4 @@ public FileVisitResult postVisitDirectory(Path dir, IOException ex) {
return CONTINUE;
}

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);
}

}
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);
}

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);
}

}
Loading
Loading