Skip to content

Commit 9e1238e

Browse files
committed
Consider resource loader path when checking Groovy template availability
Closes gh-8304
1 parent abc9f7c commit 9e1238e

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAvailabilityProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,11 +38,14 @@ public boolean isTemplateAvailable(String view, Environment environment,
3838
if (ClassUtils.isPresent("groovy.text.TemplateEngine", classLoader)) {
3939
PropertyResolver resolver = new RelaxedPropertyResolver(environment,
4040
"spring.groovy.template.");
41+
String loaderPath = resolver.getProperty("resource-loader-path",
42+
GroovyTemplateProperties.DEFAULT_RESOURCE_LOADER_PATH);
4143
String prefix = resolver.getProperty("prefix",
4244
GroovyTemplateProperties.DEFAULT_PREFIX);
4345
String suffix = resolver.getProperty("suffix",
4446
GroovyTemplateProperties.DEFAULT_SUFFIX);
45-
return resourceLoader.getResource(prefix + view + suffix).exists();
47+
return resourceLoader.getResource(loaderPath + prefix + view + suffix)
48+
.exists();
4649
}
4750
return false;
4851
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.groovy.template;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
22+
import org.springframework.core.io.DefaultResourceLoader;
23+
import org.springframework.core.io.ResourceLoader;
24+
import org.springframework.mock.env.MockEnvironment;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link GroovyTemplateAvailabilityProvider}.
30+
*
31+
* @author Andy Wilkinson
32+
*/
33+
public class GroovyTemplateAvailabilityProviderTests {
34+
35+
private final TemplateAvailabilityProvider provider = new GroovyTemplateAvailabilityProvider();
36+
37+
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
38+
39+
private final MockEnvironment environment = new MockEnvironment();
40+
41+
@Test
42+
public void availabilityOfTemplateInDefaultLocation() {
43+
assertThat(this.provider.isTemplateAvailable("home", this.environment,
44+
getClass().getClassLoader(), this.resourceLoader)).isTrue();
45+
}
46+
47+
@Test
48+
public void availabilityOfTemplateThatDoesNotExist() {
49+
assertThat(this.provider.isTemplateAvailable("whatever", this.environment,
50+
getClass().getClassLoader(), this.resourceLoader)).isFalse();
51+
}
52+
53+
@Test
54+
public void availabilityOfTemplateWithCustomLoaderPath() {
55+
this.environment.setProperty("spring.groovy.template.resource-loader-path",
56+
"classpath:/custom-templates/");
57+
assertThat(this.provider.isTemplateAvailable("custom", this.environment,
58+
getClass().getClassLoader(), this.resourceLoader)).isTrue();
59+
}
60+
61+
@Test
62+
public void availabilityOfTemplateWithCustomPrefix() {
63+
this.environment.setProperty("spring.groovy.template.prefix", "prefix/");
64+
assertThat(this.provider.isTemplateAvailable("prefixed", this.environment,
65+
getClass().getClassLoader(), this.resourceLoader)).isTrue();
66+
}
67+
68+
@Test
69+
public void availabilityOfTemplateWithCustomSuffix() {
70+
this.environment.setProperty("spring.groovy.template.suffix", ".groovytemplate");
71+
assertThat(this.provider.isTemplateAvailable("suffixed", this.environment,
72+
getClass().getClassLoader(), this.resourceLoader)).isTrue();
73+
}
74+
75+
}

0 commit comments

Comments
 (0)