Introduce ResourcePatternHint#toRegex

This change is done for several reasons:
 - Move the logic where it is documented.
 - Test it with ResourcePatternHintTests.
 - Allow RuntimeHintsPredicates to leverage this logic.

Closes gh-28620
This commit is contained in:
Sébastien Deleuze 2022-06-14 12:00:33 +02:00
parent 77ad4a1428
commit c235ad0b35
3 changed files with 88 additions and 14 deletions

View File

@ -16,7 +16,10 @@
package org.springframework.aot.hint; package org.springframework.aot.hint;
import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.lang.Nullable; 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 pattern to use for identifying the resources to match.
* @return the patterns * @return the pattern
*/ */
public String getPattern() { public String getPattern() {
return this.pattern; 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 @Nullable
@Override @Override
public TypeReference getReachableType() { public TypeReference getReachableType() {

View File

@ -16,13 +16,10 @@
package org.springframework.aot.nativex; package org.springframework.aot.nativex;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.springframework.aot.hint.ConditionalHint; import org.springframework.aot.hint.ConditionalHint;
@ -77,19 +74,10 @@ class ResourceHintsWriter {
private Map<String, Object> toAttributes(ResourcePatternHint hint) { private Map<String, Object> toAttributes(ResourcePatternHint hint) {
Map<String, Object> attributes = new LinkedHashMap<>(); Map<String, Object> attributes = new LinkedHashMap<>();
handleCondition(attributes, hint); handleCondition(attributes, hint);
attributes.put("pattern", patternToRegexp(hint.getPattern())); attributes.put("pattern", hint.toRegex().toString());
return attributes; 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<String, Object> attributes, String name, @Nullable Object value) { private void addIfNotEmpty(Map<String, Object> attributes, String name, @Nullable Object value) {
if (value instanceof Collection<?> collection) { if (value instanceof Collection<?> collection) {
if (!collection.isEmpty()) { if (!collection.isEmpty()) {

View File

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