-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support scanning for classpath resources (#3705)
Adds several new methods to ReflectionSupport to find or stream resource objects in the classpath root, package, and modules. A resource is represented via thew new `Resource` interface. Resolves #3630.
- Loading branch information
1 parent
6d2ae1d
commit 8e8268c
Showing
18 changed files
with
976 additions
and
74 deletions.
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
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. | ||
* @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(); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClasspathFilters.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,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); | ||
} | ||
|
||
} |
Oops, something went wrong.