Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove unused tryToLoadResource
  • Loading branch information
mpkorstanje committed Jul 8, 2024
commit ee7b42a54876aef2a89bf3f5ac35d4f04e5a81d2
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand Down Expand Up @@ -114,27 +113,6 @@ public static Try<Class<?>> tryToLoadClass(String name, ClassLoader classLoader)
return ReflectionUtils.tryToLoadClass(name, classLoader);
}

/**
* Tries to load the {@link Resource} for the supplied classpath resource name.
*
* <p>The name of a <em>classpath resource</em> must follow the semantics
* for resource paths as defined in {@link ClassLoader#getResource(String)}.
*
* <p>If the supplied classpath resource name is prefixed with a slash
* ({@code /}), the slash will be removed.
*
* @param classpathResourceName the name of the resource to load; never {@code null} or blank
* @param classLoader the {@code ClassLoader} to use; never {@code null}
* @return a successful {@code Try} containing the loaded class or a failed
* {@code Try} containing the exception if no such resource could be loaded;
* never {@code null}
* @since 1.11
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static Try<Resource> tryToLoadResource(String classpathResourceName, ClassLoader classLoader) {
return ReflectionUtils.tryToLoadResource(classpathResourceName, classLoader);
}

/**
* Find all {@linkplain Class classes} in the supplied classpath {@code root}
* that match the specified {@code classFilter} and {@code classNameFilter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -891,33 +890,6 @@ public static Try<Class<?>> tryToLoadClass(String name, ClassLoader classLoader)
});
}

/**
* Tries to load the {@link Resource} for the supplied classpath resource name.
*
* <p>See {@link org.junit.platform.commons.support.ReflectionSupport#tryToLoadResource(String, ClassLoader)}
* for details.
*
* @param classpathResourceName the name of the resource to load; never {@code null} or blank
* @param classLoader the {@code ClassLoader} to use; never {@code null}
* @since 1.11
*/
@API(status = INTERNAL, since = "1.11")
public static Try<Resource> tryToLoadResource(String classpathResourceName, ClassLoader classLoader) {
Preconditions.notBlank(classpathResourceName, "Resource name must not be null or blank");
Preconditions.notNull(classLoader, "ClassLoader must not be null");
boolean startsWithSlash = classpathResourceName.startsWith("/");
String canonicalClasspathResourceName = (startsWithSlash ? classpathResourceName.substring(1)
: classpathResourceName);

return Try.call(() -> {
URL resource = classLoader.getResource(canonicalClasspathResourceName);
if (resource == null) {
throw new NullPointerException("classLoader.getResource returned null");
}
return new ClasspathResource(canonicalClasspathResourceName, resource.toURI());
});
}

private static Class<?> loadArrayType(ClassLoader classLoader, String componentTypeName, int dimensions)
throws ClassNotFoundException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,6 @@ void tryToLoadClassWithExplicitClassLoaderDelegates() {
ReflectionSupport.tryToLoadClass("java.nio.Bits", classLoader));
}

/**
* @since 1.11
*/
@Test
void tryToLoadResourcesWithExplicitClassLoaderDelegates() {
var classLoader = getClass().getClassLoader();
var resource = "org/junit/platform/commons/example.resource";
assertEquals(ReflectionUtils.tryToLoadResource(resource, classLoader).toOptional(),
ReflectionSupport.tryToLoadResource(resource, classLoader).toOptional());
}

/**
* @since 1.10
*/
Expand All @@ -115,21 +104,6 @@ void tryToLoadClassWithExplicitClassLoaderPreconditions() {
assertPreconditionViolationException("ClassLoader", () -> ReflectionSupport.tryToLoadClass("int", null));
}

/**
* @since 1.11
*/
@Test
void tryToLoadResourceWithExplicitClassLoaderPreconditions() {
ClassLoader cl = getClass().getClassLoader();

assertPreconditionViolationExceptionForString("Resource name",
() -> ReflectionSupport.tryToLoadResource(null, cl));
assertPreconditionViolationExceptionForString("Resource name",
() -> ReflectionSupport.tryToLoadResource("", cl));

assertPreconditionViolationException("ClassLoader", () -> ReflectionSupport.tryToLoadResource("int", null));
}

@TestFactory
List<DynamicTest> findAllClassesInClasspathRootDelegates() throws Throwable {
List<DynamicTest> tests = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,46 +710,6 @@ private void privateMethod() {

}

@Nested
class ResourceLoadingTests {

@Test
void tryToLoadResourcePreconditions() {
assertThrows(PreconditionViolationException.class, () -> ReflectionUtils.tryToLoadResource("", null));
assertThrows(PreconditionViolationException.class, () -> ReflectionUtils.tryToLoadResource(" ", null));

assertThrows(PreconditionViolationException.class, () -> ReflectionUtils.tryToLoadResource(null, null));
assertThrows(PreconditionViolationException.class,
() -> ReflectionUtils.tryToLoadResource("org/junit/platform/commons/example.resource", null));
}

@Test
void tryToLoadResource() {
var classLoader = ClassLoaderUtils.getDefaultClassLoader();
var tryToLoadResource = ReflectionUtils.tryToLoadResource("org/junit/platform/commons/example.resource",
classLoader);
var resource = assertDoesNotThrow(tryToLoadResource::get);
assertThat(resource.getName()).isEqualTo("org/junit/platform/commons/example.resource");
}

@Test
void tryToLoadResourceWithPrefixedSlash() {
var classLoader = ClassLoaderUtils.getDefaultClassLoader();
var tryToLoadResource = ReflectionUtils.tryToLoadResource("/org/junit/platform/commons/example.resource",
classLoader);
var resource = assertDoesNotThrow(tryToLoadResource::get);
assertThat(resource.getName()).isEqualTo("org/junit/platform/commons/example.resource");
}

@Test
void tryToLoadResourceWhenResourceNotFound() {
var classLoader = ClassLoaderUtils.getDefaultClassLoader();
var tryToLoadResource = ReflectionUtils.tryToLoadResource("org/junit/platform/commons/no-such.resource",
classLoader);
assertThrows(NullPointerException.class, tryToLoadResource::get);
}
}

@Nested
class ClassLoadingTests {

Expand Down