Skip to content

Commit

Permalink
Always register root directory for registered resource hints
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 31, 2022
1 parent ca33752 commit c3fca0a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void applyToWhenHasImportAwareConfigurationRegistersHints() {
.satisfies(resourceHint -> assertThat(resourceHint.getIncludes())
.map(ResourcePatternHint::getPattern)
.containsExactlyInAnyOrder(
"/",
"org",
"org/springframework",
"org/springframework/context",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,15 @@ public Builder includes(@Nullable TypeReference reachableType, String... include
* @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("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ 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"));
}

@Test
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"));
}

Expand All @@ -70,16 +70,26 @@ void registerTypeSeveralTimesAddsOnlyOneEntry() {
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
void registerExactMatches() {
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);
}

Expand All @@ -101,15 +111,15 @@ void registerRootPattern() {
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
void registerPatternWithIncludesAndExcludes() {
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")));
}

Expand All @@ -118,7 +128,7 @@ void registerIfPresentRegisterExistingLocation() {
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
Expand Down Expand Up @@ -152,7 +162,7 @@ void registerResourceWithExistingClassPathResource() {
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
Expand All @@ -161,7 +171,7 @@ void registerResourceWithExistingRelativeClassPathResource() {
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
Expand Down Expand Up @@ -197,8 +207,7 @@ private Consumer<ResourcePatternHints> patternOf(List<String> includes, List<Str

static class Nested {

static class Inner {

class Inner {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void resourceHintWithClass() {
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();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void registerWithLocationWithoutTrailingSlash() {
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
Expand All @@ -105,15 +105,15 @@ void registerWithLocationUsingResourceClasspathPrefix() {
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
void registerWithLocationUsingResourceClasspathPrefixAndTrailingSlash() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void resourceConfig() throws IOException, JSONException {
"resources": {
"includes": [
{"pattern": "\\\\Qcom/example/test.properties\\\\E"},
{"pattern": "\\\\Q/\\\\E"},
{"pattern": "\\\\Qcom\\\\E"},
{"pattern": "\\\\Qcom/example\\\\E"},
{"pattern": "\\\\Qcom/example/another.properties\\\\E"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void registerExactMatch() throws JSONException {
"resources": {
"includes": [
{ "pattern": "\\\\Qcom/example/test.properties\\\\E"},
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qcom\\\\E"},
{ "pattern": "\\\\Qcom/example\\\\E"},
{ "pattern": "\\\\Qcom/example/another.properties\\\\E"}
Expand Down Expand Up @@ -82,6 +83,7 @@ void registerWildcardInTheMiddlePattern() throws JSONException {
"resources": {
"includes": [
{ "pattern": "\\\\Qcom/example/\\\\E.*\\\\Q.properties\\\\E"},
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qcom\\\\E"},
{ "pattern": "\\\\Qcom/example\\\\E"}
]
Expand All @@ -98,6 +100,7 @@ void registerWildcardAtTheEndPattern() throws JSONException {
"resources": {
"includes": [
{ "pattern": "\\\\Qstatic/\\\\E.*"},
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qstatic\\\\E"}
]
}
Expand All @@ -114,6 +117,7 @@ void registerPatternWithIncludesAndExcludes() throws JSONException {
"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"},
Expand All @@ -137,6 +141,7 @@ void registerWithReachableTypeCondition() throws JSONException {
"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"}
]
Expand All @@ -153,6 +158,7 @@ void registerType() throws JSONException {
"resources": {
"includes": [
{ "pattern": "\\\\Qjava/lang/String.class\\\\E" },
{ "pattern": "\\\\Q/\\\\E" },
{ "pattern": "\\\\Qjava\\\\E" },
{ "pattern": "\\\\Qjava/lang\\\\E" }
]
Expand Down

0 comments on commit c3fca0a

Please sign in to comment.