Skip to content

Commit 66b370e

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 Cherry-picked from: 97c9b05
1 parent 2bbfbb1 commit 66b370e

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-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,11 +16,13 @@
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.nio.charset.Charset;
2223
import java.util.Arrays;
2324
import java.util.HashMap;
25+
import java.util.Locale;
2426
import java.util.Map;
2527
import javax.script.Invocable;
2628
import javax.script.ScriptEngine;
@@ -191,6 +193,19 @@ public void setResourceLoaderPath(String resourceLoaderPath) {
191193
}
192194
}
193195

196+
@Override
197+
public boolean checkResource(Locale locale) throws Exception {
198+
try {
199+
getTemplate(getUrl());
200+
return true;
201+
}
202+
catch (IllegalStateException exc) {
203+
if (logger.isDebugEnabled()) {
204+
logger.debug("No ScriptTemplate view found for URL: " + getUrl());
205+
}
206+
return false;
207+
}
208+
}
194209

195210
@Override
196211
protected void initApplicationContext(ApplicationContext context) {

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.HashMap;
2222
import java.util.List;
23+
import java.util.Locale;
2324
import java.util.Map;
2425
import java.util.concurrent.ExecutorService;
2526
import java.util.concurrent.Executors;
@@ -28,7 +29,9 @@
2829
import javax.script.ScriptEngine;
2930

3031
import org.junit.Before;
32+
import org.junit.Rule;
3133
import org.junit.Test;
34+
import org.junit.rules.ExpectedException;
3235

3336
import org.springframework.beans.DirectFieldAccessor;
3437
import org.springframework.context.ApplicationContextException;
@@ -58,6 +61,8 @@ public class ScriptTemplateViewTests {
5861

5962
private StaticWebApplicationContext wac;
6063

64+
@Rule
65+
public ExpectedException expectedException = ExpectedException.none();
6166

6267
@Before
6368
public void setup() {
@@ -68,15 +73,24 @@ public void setup() {
6873
}
6974

7075

76+
@Test
77+
public void missingTemplate() throws Exception {
78+
MockServletContext servletContext = new MockServletContext();
79+
this.wac.setServletContext(servletContext);
80+
this.wac.refresh();
81+
this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/");
82+
this.view.setUrl("missing.txt");
83+
this.view.setEngine(mock(InvocableScriptEngine.class));
84+
this.configurer.setRenderFunction("render");
85+
this.view.setApplicationContext(this.wac);
86+
assertFalse(this.view.checkResource(Locale.ENGLISH));
87+
}
88+
7189
@Test
7290
public void missingScriptTemplateConfig() throws Exception {
73-
try {
74-
this.view.setApplicationContext(new StaticApplicationContext());
75-
fail("Should have thrown ApplicationContextException");
76-
}
77-
catch (ApplicationContextException ex) {
78-
assertTrue(ex.getMessage().contains("ScriptTemplateConfig"));
79-
}
91+
this.expectedException.expect(ApplicationContextException.class);
92+
this.view.setApplicationContext(new StaticApplicationContext());
93+
this.expectedException.expectMessage(contains("ScriptTemplateConfig"));
8094
}
8195

8296
@Test
@@ -152,53 +166,37 @@ public void nonSharedEngine() throws Exception {
152166

153167
@Test
154168
public void nonInvocableScriptEngine() throws Exception {
155-
try {
156-
this.view.setEngine(mock(ScriptEngine.class));
157-
fail("Should have thrown IllegalArgumentException");
158-
}
159-
catch (IllegalArgumentException ex) {
160-
assertThat(ex.getMessage(), containsString("instance"));
161-
}
169+
this.expectedException.expect(IllegalArgumentException.class);
170+
this.view.setEngine(mock(ScriptEngine.class));
171+
this.expectedException.expectMessage(contains("instance"));
162172
}
163173

164174
@Test
165175
public void noRenderFunctionDefined() {
166176
this.view.setEngine(mock(InvocableScriptEngine.class));
167-
try {
168-
this.view.setApplicationContext(this.wac);
169-
fail("Should have thrown IllegalArgumentException");
170-
}
171-
catch (IllegalArgumentException ex) {
172-
assertThat(ex.getMessage(), containsString("renderFunction"));
173-
}
177+
this.expectedException.expect(IllegalArgumentException.class);
178+
this.view.setApplicationContext(this.wac);
179+
this.expectedException.expectMessage(contains("renderFunction"));
174180
}
175181

176182
@Test
177183
public void engineAndEngineNameBothDefined() {
178184
this.view.setEngine(mock(InvocableScriptEngine.class));
179185
this.view.setEngineName("test");
180186
this.view.setRenderFunction("render");
181-
try {
182-
this.view.setApplicationContext(this.wac);
183-
fail("Should have thrown IllegalArgumentException");
184-
}
185-
catch (IllegalArgumentException ex) {
186-
assertThat(ex.getMessage(), containsString("'engine' or 'engineName'"));
187-
}
187+
this.expectedException.expect(IllegalArgumentException.class);
188+
this.view.setApplicationContext(this.wac);
189+
this.expectedException.expectMessage(contains("'engine' or 'engineName'"));
188190
}
189191

190192
@Test
191193
public void engineSetterAndNonSharedEngine() {
192194
this.view.setEngine(mock(InvocableScriptEngine.class));
193195
this.view.setRenderFunction("render");
194196
this.view.setSharedEngine(false);
195-
try {
196-
this.view.setApplicationContext(this.wac);
197-
fail("Should have thrown IllegalArgumentException");
198-
}
199-
catch (IllegalArgumentException ex) {
200-
assertThat(ex.getMessage(), containsString("sharedEngine"));
201-
}
197+
this.expectedException.expect(IllegalArgumentException.class);
198+
this.view.setApplicationContext(this.wac);
199+
this.expectedException.expectMessage(contains("sharedEngine"));
202200
}
203201

204202
@Test // SPR-14210

0 commit comments

Comments
 (0)