Always register root directory for registered resource hints

See gh-29403
This commit is contained in:
Sam Brannen 2022-10-31 14:20:45 +01:00
parent ca33752267
commit c3fca0a826
7 changed files with 39 additions and 27 deletions

View File

@ -150,6 +150,7 @@ class ConfigurationClassPostProcessorAotContributionTests {
.satisfies(resourceHint -> assertThat(resourceHint.getIncludes())
.map(ResourcePatternHint::getPattern)
.containsExactlyInAnyOrder(
"/",
"org",
"org/springframework",
"org/springframework/context",

View File

@ -98,20 +98,15 @@ public final class ResourcePatternHints {
* @see <a href="https://github.com/spring-projects/spring-framework/issues/29403">gh-29403</a>
*/
private List<String> expandToIncludeDirectories(String includePattern) {
// Root resource or no explicit subdirectories?
// Resource in root or no explicit subdirectories?
if (!includePattern.contains("/")) {
if (includePattern.contains("*")) {
// If it's a root pattern, include the root directory as well as the pattern
return List.of("/", includePattern);
}
else {
// Include only the root resource
return List.of(includePattern);
}
// Include the root directory as well as the pattern
return List.of("/", includePattern);
}
List<String> includePatterns = new ArrayList<>();
// Ensure the original pattern is always included
// Ensure the root directory and original pattern are always included
includePatterns.add("/");
includePatterns.add(includePattern);
StringBuilder path = new StringBuilder();
for (String pathElement : includePattern.split("/")) {

View File

@ -46,14 +46,14 @@ class ResourceHintsTests {
void registerType() {
this.resourceHints.registerType(String.class);
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("java", "java/lang", "java/lang/String.class"));
patternOf("/", "java", "java/lang", "java/lang/String.class"));
}
@Test
void registerTypeWithNestedType() {
this.resourceHints.registerType(TypeReference.of(Nested.class));
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint",
patternOf("/", "org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint",
"org/springframework/aot/hint/ResourceHintsTests$Nested.class"));
}
@ -61,7 +61,7 @@ class ResourceHintsTests {
void registerTypeWithInnerNestedType() {
this.resourceHints.registerType(TypeReference.of(Inner.class));
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint",
patternOf("/", "org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint",
"org/springframework/aot/hint/ResourceHintsTests$Nested$Inner.class"));
}
@ -70,7 +70,7 @@ class ResourceHintsTests {
this.resourceHints.registerType(String.class);
this.resourceHints.registerType(TypeReference.of(String.class));
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("java", "java/lang", "java/lang/String.class"));
patternOf("/", "java", "java/lang", "java/lang/String.class"));
}
@Test
@ -78,8 +78,18 @@ class ResourceHintsTests {
this.resourceHints.registerPattern("com/example/test.properties");
this.resourceHints.registerPattern("com/example/another.properties");
assertThat(this.resourceHints.resourcePatternHints())
.anySatisfy(patternOf("com", "com/example", "com/example/test.properties"))
.anySatisfy(patternOf("com", "com/example", "com/example/another.properties"))
.anySatisfy(patternOf("/", "com", "com/example", "com/example/test.properties"))
.anySatisfy(patternOf("/", "com", "com/example", "com/example/another.properties"))
.hasSize(2);
}
@Test
void registerExactMatchesInRootDirectory() {
this.resourceHints.registerPattern("test.properties");
this.resourceHints.registerPattern("another.properties");
assertThat(this.resourceHints.resourcePatternHints())
.anySatisfy(patternOf("/", "test.properties"))
.anySatisfy(patternOf("/", "another.properties"))
.hasSize(2);
}
@ -101,7 +111,7 @@ class ResourceHintsTests {
void registerPattern() {
this.resourceHints.registerPattern("com/example/*.properties");
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("com", "com/example", "com/example/*.properties"));
patternOf("/", "com", "com/example", "com/example/*.properties"));
}
@Test
@ -109,7 +119,7 @@ class ResourceHintsTests {
this.resourceHints.registerPattern(resourceHint ->
resourceHint.includes("com/example/*.properties").excludes("com/example/to-ignore.properties"));
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(patternOf(
List.of("com", "com/example", "com/example/*.properties"),
List.of("/", "com", "com/example", "com/example/*.properties"),
List.of("com/example/to-ignore.properties")));
}
@ -118,7 +128,7 @@ class ResourceHintsTests {
this.resourceHints.registerPatternIfPresent(null, "META-INF/",
resourceHint -> resourceHint.includes("com/example/*.properties"));
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("com", "com/example", "com/example/*.properties"));
patternOf("/", "com", "com/example", "com/example/*.properties"));
}
@Test
@ -152,7 +162,7 @@ class ResourceHintsTests {
ClassPathResource resource = new ClassPathResource(path);
this.resourceHints.registerResource(resource);
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint", path));
patternOf("/", "org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint", path));
}
@Test
@ -161,7 +171,7 @@ class ResourceHintsTests {
ClassPathResource resource = new ClassPathResource("support", RuntimeHints.class);
this.resourceHints.registerResource(resource);
assertThat(this.resourceHints.resourcePatternHints()).singleElement().satisfies(
patternOf("org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint", path));
patternOf("/", "org", "org/springframework", "org/springframework/aot", "org/springframework/aot/hint", path));
}
@Test
@ -197,8 +207,7 @@ class ResourceHintsTests {
static class Nested {
static class Inner {
class Inner {
}
}

View File

@ -49,7 +49,7 @@ class RuntimeHintsTests {
this.hints.resources().registerType(String.class);
assertThat(this.hints.resources().resourcePatternHints()).singleElement().satisfies(resourceHint -> {
assertThat(resourceHint.getIncludes()).map(ResourcePatternHint::getPattern)
.containsExactlyInAnyOrder("java", "java/lang", "java/lang/String.class");
.containsExactlyInAnyOrder("/", "java", "java/lang", "java/lang/String.class");
assertThat(resourceHint.getExcludes()).isEmpty();
});
}

View File

@ -89,7 +89,7 @@ class FilePatternResourceHintsRegistrarTests {
new FilePatternResourceHintsRegistrar(List.of("test"), List.of("META-INF"), List.of(".txt"))
.registerHints(this.hints, null);
assertThat(this.hints.resourcePatternHints()).singleElement()
.satisfies(includes("META-INF", "META-INF/test*.txt"));
.satisfies(includes("/", "META-INF", "META-INF/test*.txt"));
}
@Test
@ -105,7 +105,7 @@ class FilePatternResourceHintsRegistrarTests {
new FilePatternResourceHintsRegistrar(List.of("test"), List.of("classpath:META-INF"), List.of(".txt"))
.registerHints(this.hints, null);
assertThat(this.hints.resourcePatternHints()).singleElement()
.satisfies(includes("META-INF", "META-INF/test*.txt"));
.satisfies(includes("/", "META-INF", "META-INF/test*.txt"));
}
@Test
@ -113,7 +113,7 @@ class FilePatternResourceHintsRegistrarTests {
new FilePatternResourceHintsRegistrar(List.of("test"), List.of("classpath:/META-INF"), List.of(".txt"))
.registerHints(this.hints, null);
assertThat(this.hints.resourcePatternHints()).singleElement()
.satisfies(includes("META-INF", "META-INF/test*.txt"));
.satisfies(includes("/", "META-INF", "META-INF/test*.txt"));
}
@Test

View File

@ -176,6 +176,7 @@ class FileNativeConfigurationWriterTests {
"resources": {
"includes": [
{"pattern": "\\\\Qcom/example/test.properties\\\\E"},
{"pattern": "\\\\Q/\\\\E"},
{"pattern": "\\\\Qcom\\\\E"},
{"pattern": "\\\\Qcom/example\\\\E"},
{"pattern": "\\\\Qcom/example/another.properties\\\\E"}

View File

@ -50,6 +50,7 @@ class ResourceHintsWriterTests {
"resources": {
"includes": [
{ "pattern": "\\\\Qcom/example/test.properties\\\\E"},
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qcom\\\\E"},
{ "pattern": "\\\\Qcom/example\\\\E"},
{ "pattern": "\\\\Qcom/example/another.properties\\\\E"}
@ -82,6 +83,7 @@ class ResourceHintsWriterTests {
"resources": {
"includes": [
{ "pattern": "\\\\Qcom/example/\\\\E.*\\\\Q.properties\\\\E"},
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qcom\\\\E"},
{ "pattern": "\\\\Qcom/example\\\\E"}
]
@ -98,6 +100,7 @@ class ResourceHintsWriterTests {
"resources": {
"includes": [
{ "pattern": "\\\\Qstatic/\\\\E.*"},
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qstatic\\\\E"}
]
}
@ -114,6 +117,7 @@ class ResourceHintsWriterTests {
"resources": {
"includes": [
{ "pattern": "\\\\Qcom/example/\\\\E.*\\\\Q.properties\\\\E"},
{ "pattern": "\\\\Q/\\\\E"},
{ "pattern": "\\\\Qcom\\\\E"},
{ "pattern": "\\\\Qcom/example\\\\E"},
{ "pattern": "\\\\Qorg/other/\\\\E.*\\\\Q.properties\\\\E"},
@ -137,6 +141,7 @@ class ResourceHintsWriterTests {
"resources": {
"includes": [
{ "condition": { "typeReachable": "com.example.Test"}, "pattern": "\\\\Qcom/example/test.properties\\\\E"},
{ "condition": { "typeReachable": "com.example.Test"}, "pattern": "\\\\Q/\\\\E"},
{ "condition": { "typeReachable": "com.example.Test"}, "pattern": "\\\\Qcom\\\\E"},
{ "condition": { "typeReachable": "com.example.Test"}, "pattern": "\\\\Qcom/example\\\\E"}
]
@ -153,6 +158,7 @@ class ResourceHintsWriterTests {
"resources": {
"includes": [
{ "pattern": "\\\\Qjava/lang/String.class\\\\E" },
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qjava\\\\E" },
{ "pattern": "\\\\Qjava/lang\\\\E" }
]