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