Skip to content

Commit 97c9b05

Browse files
committed
Check template availability in ScriptTemplateView
This commit overrides the `checkResource` implementation in `ScriptTemplateView` in order to check if the template file resource is available and if the resolver can then proceed with rendering the template. Issue: SPR-14729
1 parent ed19dc7 commit 97c9b05

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.servlet.view.script;
1818

19+
import java.io.FileNotFoundException;
1920
import java.io.IOException;
2021
import java.io.InputStreamReader;
2122
import java.net.URL;
@@ -25,6 +26,7 @@
2526
import java.util.Arrays;
2627
import java.util.HashMap;
2728
import java.util.List;
29+
import java.util.Locale;
2830
import java.util.Map;
2931
import javax.script.Invocable;
3032
import javax.script.ScriptEngine;
@@ -187,6 +189,19 @@ public void setResourceLoaderPath(String resourceLoaderPath) {
187189
this.resourceLoaderPath = resourceLoaderPath;
188190
}
189191

192+
@Override
193+
public boolean checkResource(Locale locale) throws Exception {
194+
try {
195+
getTemplate(getUrl());
196+
return true;
197+
}
198+
catch (FileNotFoundException exc) {
199+
if (logger.isDebugEnabled()) {
200+
logger.debug("No ScriptTemplate view found for URL: " + getUrl());
201+
}
202+
return false;
203+
}
204+
}
190205

191206
@Override
192207
protected void initApplicationContext(ApplicationContext context) {

spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.HashMap;
2424
import java.util.List;
25+
import java.util.Locale;
2526
import java.util.Map;
2627
import java.util.concurrent.ExecutorService;
2728
import java.util.concurrent.Executors;
@@ -31,7 +32,9 @@
3132

3233
import org.hamcrest.Matchers;
3334
import org.junit.Before;
35+
import org.junit.Rule;
3436
import org.junit.Test;
37+
import org.junit.rules.ExpectedException;
3538

3639
import org.springframework.beans.DirectFieldAccessor;
3740
import org.springframework.context.ApplicationContextException;
@@ -64,6 +67,8 @@ public class ScriptTemplateViewTests {
6467

6568
private StaticWebApplicationContext wac;
6669

70+
@Rule
71+
public ExpectedException expectedException = ExpectedException.none();
6772

6873
@Before
6974
public void setup() {
@@ -75,15 +80,20 @@ public void setup() {
7580
}
7681

7782

83+
@Test
84+
public void missingTemplate() throws Exception {
85+
this.view.setUrl(RESOURCE_LOADER_PATH + "missing.txt");
86+
this.view.setEngine(mock(InvocableScriptEngine.class));
87+
this.configurer.setRenderFunction("render");
88+
this.view.setApplicationContext(this.wac);
89+
assertFalse(this.view.checkResource(Locale.ENGLISH));
90+
}
91+
7892
@Test
7993
public void missingScriptTemplateConfig() throws Exception {
80-
try {
81-
this.view.setApplicationContext(new StaticApplicationContext());
82-
fail("Should have thrown ApplicationContextException");
83-
}
84-
catch (ApplicationContextException ex) {
85-
assertTrue(ex.getMessage().contains("ScriptTemplateConfig"));
86-
}
94+
this.expectedException.expect(ApplicationContextException.class);
95+
this.view.setApplicationContext(new StaticApplicationContext());
96+
this.expectedException.expectMessage(contains("ScriptTemplateConfig"));
8797
}
8898

8999
@Test
@@ -159,53 +169,37 @@ public void nonSharedEngine() throws Exception {
159169

160170
@Test
161171
public void nonInvocableScriptEngine() throws Exception {
162-
try {
163-
this.view.setEngine(mock(ScriptEngine.class));
164-
fail("Should have thrown IllegalArgumentException");
165-
}
166-
catch (IllegalArgumentException ex) {
167-
assertThat(ex.getMessage(), containsString("instance"));
168-
}
172+
this.expectedException.expect(IllegalArgumentException.class);
173+
this.view.setEngine(mock(ScriptEngine.class));
174+
this.expectedException.expectMessage(contains("instance"));
169175
}
170176

171177
@Test
172178
public void noRenderFunctionDefined() {
173179
this.view.setEngine(mock(InvocableScriptEngine.class));
174-
try {
175-
this.view.setApplicationContext(this.wac);
176-
fail("Should have thrown IllegalArgumentException");
177-
}
178-
catch (IllegalArgumentException ex) {
179-
assertThat(ex.getMessage(), containsString("renderFunction"));
180-
}
180+
this.expectedException.expect(IllegalArgumentException.class);
181+
this.view.setApplicationContext(this.wac);
182+
this.expectedException.expectMessage(contains("renderFunction"));
181183
}
182184

183185
@Test
184186
public void engineAndEngineNameBothDefined() {
185187
this.view.setEngine(mock(InvocableScriptEngine.class));
186188
this.view.setEngineName("test");
187189
this.view.setRenderFunction("render");
188-
try {
189-
this.view.setApplicationContext(this.wac);
190-
fail("Should have thrown IllegalArgumentException");
191-
}
192-
catch (IllegalArgumentException ex) {
193-
assertThat(ex.getMessage(), containsString("'engine' or 'engineName'"));
194-
}
190+
this.expectedException.expect(IllegalArgumentException.class);
191+
this.view.setApplicationContext(this.wac);
192+
this.expectedException.expectMessage(contains("'engine' or 'engineName'"));
195193
}
196194

197195
@Test
198196
public void engineSetterAndNonSharedEngine() {
199197
this.view.setEngine(mock(InvocableScriptEngine.class));
200198
this.view.setRenderFunction("render");
201199
this.view.setSharedEngine(false);
202-
try {
203-
this.view.setApplicationContext(this.wac);
204-
fail("Should have thrown IllegalArgumentException");
205-
}
206-
catch (IllegalArgumentException ex) {
207-
assertThat(ex.getMessage(), containsString("sharedEngine"));
208-
}
200+
this.expectedException.expect(IllegalArgumentException.class);
201+
this.view.setApplicationContext(this.wac);
202+
this.expectedException.expectMessage(contains("sharedEngine"));
209203
}
210204

211205
@Test

0 commit comments

Comments
 (0)