From 97c9b05c15037a75a9f02dc8f532007001a7361a Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Sat, 17 Sep 2016 22:18:55 +0200 Subject: [PATCH] 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 --- .../view/script/ScriptTemplateView.java | 15 +++++ .../view/script/ScriptTemplateViewTests.java | 64 +++++++++---------- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java index 908b273ea60..e2a71ea0b9c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java @@ -16,6 +16,7 @@ package org.springframework.web.servlet.view.script; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; @@ -25,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import javax.script.Invocable; import javax.script.ScriptEngine; @@ -187,6 +189,19 @@ public class ScriptTemplateView extends AbstractUrlBasedView { this.resourceLoaderPath = resourceLoaderPath; } + @Override + public boolean checkResource(Locale locale) throws Exception { + try { + getTemplate(getUrl()); + return true; + } + catch (FileNotFoundException exc) { + if (logger.isDebugEnabled()) { + logger.debug("No ScriptTemplate view found for URL: " + getUrl()); + } + return false; + } + } @Override protected void initApplicationContext(ApplicationContext context) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java index 21ec8e58a9f..e756d27fc88 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -31,7 +32,9 @@ import javax.script.ScriptEngine; import org.hamcrest.Matchers; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.beans.DirectFieldAccessor; import org.springframework.context.ApplicationContextException; @@ -64,6 +67,8 @@ public class ScriptTemplateViewTests { private StaticWebApplicationContext wac; + @Rule + public ExpectedException expectedException = ExpectedException.none(); @Before public void setup() { @@ -75,15 +80,20 @@ public class ScriptTemplateViewTests { } + @Test + public void missingTemplate() throws Exception { + this.view.setUrl(RESOURCE_LOADER_PATH + "missing.txt"); + this.view.setEngine(mock(InvocableScriptEngine.class)); + this.configurer.setRenderFunction("render"); + this.view.setApplicationContext(this.wac); + assertFalse(this.view.checkResource(Locale.ENGLISH)); + } + @Test public void missingScriptTemplateConfig() throws Exception { - try { - this.view.setApplicationContext(new StaticApplicationContext()); - fail("Should have thrown ApplicationContextException"); - } - catch (ApplicationContextException ex) { - assertTrue(ex.getMessage().contains("ScriptTemplateConfig")); - } + this.expectedException.expect(ApplicationContextException.class); + this.view.setApplicationContext(new StaticApplicationContext()); + this.expectedException.expectMessage(contains("ScriptTemplateConfig")); } @Test @@ -159,25 +169,17 @@ public class ScriptTemplateViewTests { @Test public void nonInvocableScriptEngine() throws Exception { - try { - this.view.setEngine(mock(ScriptEngine.class)); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - assertThat(ex.getMessage(), containsString("instance")); - } + this.expectedException.expect(IllegalArgumentException.class); + this.view.setEngine(mock(ScriptEngine.class)); + this.expectedException.expectMessage(contains("instance")); } @Test public void noRenderFunctionDefined() { this.view.setEngine(mock(InvocableScriptEngine.class)); - try { - this.view.setApplicationContext(this.wac); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - assertThat(ex.getMessage(), containsString("renderFunction")); - } + this.expectedException.expect(IllegalArgumentException.class); + this.view.setApplicationContext(this.wac); + this.expectedException.expectMessage(contains("renderFunction")); } @Test @@ -185,13 +187,9 @@ public class ScriptTemplateViewTests { this.view.setEngine(mock(InvocableScriptEngine.class)); this.view.setEngineName("test"); this.view.setRenderFunction("render"); - try { - this.view.setApplicationContext(this.wac); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - assertThat(ex.getMessage(), containsString("'engine' or 'engineName'")); - } + this.expectedException.expect(IllegalArgumentException.class); + this.view.setApplicationContext(this.wac); + this.expectedException.expectMessage(contains("'engine' or 'engineName'")); } @Test @@ -199,13 +197,9 @@ public class ScriptTemplateViewTests { this.view.setEngine(mock(InvocableScriptEngine.class)); this.view.setRenderFunction("render"); this.view.setSharedEngine(false); - try { - this.view.setApplicationContext(this.wac); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - assertThat(ex.getMessage(), containsString("sharedEngine")); - } + this.expectedException.expect(IllegalArgumentException.class); + this.view.setApplicationContext(this.wac); + this.expectedException.expectMessage(contains("sharedEngine")); } @Test