Skip to content

Commit 5014726

Browse files
committed
Add resource hints for default templates location
Closes gh-31310
1 parent 590bfd8 commit 5014726

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.template;
18+
19+
import org.springframework.aot.hint.RuntimeHints;
20+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
21+
22+
/**
23+
* {@link RuntimeHintsRegistrar} for default template location.
24+
*
25+
* @author Stephane Nicoll
26+
*/
27+
class TemplateRuntimeHints implements RuntimeHintsRegistrar {
28+
29+
@Override
30+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
31+
hints.resources().registerPatternIfPresent(classLoader, "templates", (hint) -> hint.includes("templates/*"));
32+
}
33+
34+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2+
org.springframework.boot.autoconfigure.template.TemplateRuntimeHints
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.template;
18+
19+
import java.util.List;
20+
import java.util.function.Predicate;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
26+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
27+
import org.springframework.beans.factory.aot.AotFactoriesLoader;
28+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
29+
import org.springframework.boot.test.context.FilteredClassLoader;
30+
import org.springframework.core.io.ClassPathResource;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Tests for {@link TemplateRuntimeHints}.$
36+
*
37+
* @author Stephane Nicoll
38+
*/
39+
class TemplateRuntimeHintsTests {
40+
41+
public static final Predicate<RuntimeHints> TEST_PREDICATE = RuntimeHintsPredicates.resource()
42+
.forResource("templates/something/hello.html");
43+
44+
@Test
45+
void templateRuntimeHintsIsRegistered() {
46+
List<RuntimeHintsRegistrar> registrar = new AotFactoriesLoader(new DefaultListableBeanFactory())
47+
.load(RuntimeHintsRegistrar.class);
48+
assertThat(registrar).anyMatch(TemplateRuntimeHints.class::isInstance);
49+
}
50+
51+
@Test
52+
void contributeWhenTemplateLocationExists() {
53+
RuntimeHints runtimeHints = contribute(getClass().getClassLoader());
54+
assertThat(TEST_PREDICATE.test(runtimeHints)).isTrue();
55+
}
56+
57+
@Test
58+
void contributeWhenTemplateLocationDoesNotExist() {
59+
FilteredClassLoader classLoader = new FilteredClassLoader(new ClassPathResource("templates"));
60+
RuntimeHints runtimeHints = contribute(classLoader);
61+
assertThat(TEST_PREDICATE.test(runtimeHints)).isFalse();
62+
}
63+
64+
private RuntimeHints contribute(ClassLoader classLoader) {
65+
RuntimeHints runtimeHints = new RuntimeHints();
66+
new TemplateRuntimeHints().registerHints(runtimeHints, classLoader);
67+
return runtimeHints;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)