diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java b/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java index cfab75d5ac..5dd7a8f8bd 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java @@ -16,7 +16,10 @@ package org.springframework.aot.hint; +import java.util.Arrays; import java.util.Objects; +import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.springframework.lang.Nullable; @@ -58,12 +61,26 @@ public final class ResourcePatternHint implements ConditionalHint { /** * Return the pattern to use for identifying the resources to match. - * @return the patterns + * @return the pattern */ public String getPattern() { return this.pattern; } + /** + * Return the regex {@link Pattern} to use for identifying the resources to match. + * @return the regex pattern + */ + public Pattern toRegex() { + String prefix = (this.pattern.startsWith("*") ? ".*" : ""); + String suffix = (this.pattern.endsWith("*") ? ".*" : ""); + String regex = Arrays.stream(this.pattern.split("\\*")) + .filter(s -> !s.isEmpty()) + .map(Pattern::quote) + .collect(Collectors.joining(".*", prefix, suffix)); + return Pattern.compile(regex); + } + @Nullable @Override public TypeReference getReachableType() { diff --git a/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java b/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java index 8ca54f5ae3..99b87ee769 100644 --- a/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java +++ b/spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java @@ -16,13 +16,10 @@ package org.springframework.aot.nativex; -import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.springframework.aot.hint.ConditionalHint; @@ -77,19 +74,10 @@ class ResourceHintsWriter { private Map toAttributes(ResourcePatternHint hint) { Map attributes = new LinkedHashMap<>(); handleCondition(attributes, hint); - attributes.put("pattern", patternToRegexp(hint.getPattern())); + attributes.put("pattern", hint.toRegex().toString()); return attributes; } - private String patternToRegexp(String pattern) { - String prefix = (pattern.startsWith("*") ? ".*" : ""); - String suffix = (pattern.endsWith("*") ? ".*" : ""); - return Arrays.stream(pattern.split("\\*")) - .filter(s -> !s.isEmpty()) - .map(Pattern::quote) - .collect(Collectors.joining(".*", prefix, suffix)); - } - private void addIfNotEmpty(Map attributes, String name, @Nullable Object value) { if (value instanceof Collection collection) { if (!collection.isEmpty()) { diff --git a/spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java b/spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java new file mode 100644 index 0000000000..3fcc567b4d --- /dev/null +++ b/spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.aot.hint; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ResourcePatternHint}. + * + * @author Sebastien Deleuze + */ +public class ResourcePatternHintTests { + + @Test + void fileAtRoot() { + ResourcePatternHint hint = new ResourcePatternHint("file.properties", null); + assertThat(hint.toRegex().asMatchPredicate()) + .accepts("file.properties") + .rejects("com/example/file.properties", "file.prop", "another-file.properties"); + } + + @Test + void fileInDirectory() { + ResourcePatternHint hint = new ResourcePatternHint("com/example/file.properties", null); + assertThat(hint.toRegex().asMatchPredicate()) + .accepts("com/example/file.properties") + .rejects("file.properties", "com/file.properties", "com/example/another-file.properties"); + } + + @Test + void extension() { + ResourcePatternHint hint = new ResourcePatternHint("*.properties", null); + assertThat(hint.toRegex().asMatchPredicate()) + .accepts("file.properties", "com/example/file.properties") + .rejects("file.prop", "com/example/file.prop"); + } + + @Test + void extensionInDirectoryAtAnyDepth() { + ResourcePatternHint hint = new ResourcePatternHint("com/example/*.properties", null); + assertThat(hint.toRegex().asMatchPredicate()) + .accepts("com/example/file.properties", "com/example/another/file.properties") + .rejects("file.properties", "com/file.properties"); + } + + @Test + void anyFileInDirectoryAtAnyDepth() { + ResourcePatternHint hint = new ResourcePatternHint("com/example/*", null); + assertThat(hint.toRegex().asMatchPredicate()) + .accepts("com/example/file.properties", "com/example/another/file.properties", "com/example/another") + .rejects("file.properties", "com/file.properties"); + } +}