Support default package for TypeReference in ResourceHintsPredicates

Prior to this commit, if the TypeReference supplied to
ResourceHintsPredicates.forResource(TypeReference,String) was for a
class declared in the default package (i.e., without a package), the
resolveAbsoluteResourceName() method incorrectly prepended two leading
slashes (//) to the absolute resource name, causing correct matches to
fail.

This commit fixes this by adding special handling for a TypeReference
without a package name. In addition, this commit introduces lenient
handling of resource names by consistently removing a leading slash in
ResourceHintsPredicates.forResource(*) methods. The latter aligns with
absolute resource path handling in other places in the framework, such
as ClassPathResource.

Closes gh-29086
This commit is contained in:
Sam Brannen
2022-09-06 11:53:09 +02:00
parent 135f9070c5
commit 97b98c3378
3 changed files with 49 additions and 23 deletions

View File

@@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link InstrumentedMethod}.
*
* @author Brian Clozel
* @author Sam Brannen
*/
class InstrumentedMethodTests {
@@ -556,7 +557,7 @@ class InstrumentedMethodTests {
void classGetResourceShouldMatchResourcePatternWhenAbsolute() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE)
.onInstance(InstrumentedMethodTests.class).withArgument("/some/path/resource.txt").build();
hints.resources().registerPattern("/some/*");
hints.resources().registerPattern("some/*");
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETRESOURCE, invocation);
}
@@ -564,7 +565,7 @@ class InstrumentedMethodTests {
void classGetResourceShouldMatchResourcePatternWhenRelative() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE)
.onInstance(InstrumentedMethodTests.class).withArgument("resource.txt").build();
hints.resources().registerPattern("/org/springframework/aot/agent/*");
hints.resources().registerPattern("org/springframework/aot/agent/*");
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETRESOURCE, invocation);
}
@@ -572,7 +573,7 @@ class InstrumentedMethodTests {
void classGetResourceShouldNotMatchResourcePatternWhenInvalid() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE)
.onInstance(InstrumentedMethodTests.class).withArgument("/some/path/resource.txt").build();
hints.resources().registerPattern("/other/*");
hints.resources().registerPattern("other/*");
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETRESOURCE, invocation);
}
@@ -580,7 +581,7 @@ class InstrumentedMethodTests {
void classGetResourceShouldNotMatchResourcePatternWhenExcluded() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE)
.onInstance(InstrumentedMethodTests.class).withArgument("/some/path/resource.txt").build();
hints.resources().registerPattern(resourceHint -> resourceHint.includes("/some/*").excludes("/some/path/*"));
hints.resources().registerPattern(resourceHint -> resourceHint.includes("some/*").excludes("some/path/*"));
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETRESOURCE, invocation);
}