Add script based templating support
This commit adds support for script based templating. Any templating
library running on top of a JSR-223 ScriptEngine that implements
Invocable like Nashorn or JRuby could be used.
For example, in order to render Mustache templates thanks to the Nashorn
Javascript engine provided with Java 8+, you should declare the following
configuration:
@Configuration
@EnableWebMvc
public class MustacheConfig extends WebMvcConfigurerAdapter {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.scriptTemplate();
}
@Bean
public ScriptTemplateConfigurer configurer() {
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
configurer.setEngineName("nashorn");
configurer.setScripts("mustache.js");
configurer.setRenderObject("Mustache");
configurer.setRenderFunction("render");
return configurer;
}
}
The XML counterpart is:
<beans>
<mvc:annotation-driven />
<mvc:view-resolvers>
<mvc:script-template />
</mvc:view-resolvers>
<mvc:script-template-configurer engine-name="nashorn" render-object="Mustache" render-function="render">
<mvc:script location="mustache.js" />
</mvc:script-template-configurer>
</beans>
Tested with:
- Handlebars running on Nashorn
- Mustache running on Nashorn
- React running on Nashorn
- EJS running on Nashorn
- ERB running on JRuby
- String templates running on Jython
Issue: SPR-12266
This commit is contained in:
@@ -21,6 +21,7 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -129,6 +130,8 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
|
||||
import org.springframework.web.servlet.view.script.ScriptTemplateConfigurer;
|
||||
import org.springframework.web.servlet.view.script.ScriptTemplateViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
@@ -720,11 +723,11 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testViewResolution() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-view-resolution.xml", 6);
|
||||
loadBeanDefinitions("mvc-config-view-resolution.xml", 7);
|
||||
|
||||
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
|
||||
assertNotNull(compositeResolver);
|
||||
assertEquals("Actual: " + compositeResolver.getViewResolvers(), 8, compositeResolver.getViewResolvers().size());
|
||||
assertEquals("Actual: " + compositeResolver.getViewResolvers(), 9, compositeResolver.getViewResolvers().size());
|
||||
assertEquals(Ordered.LOWEST_PRECEDENCE, compositeResolver.getOrder());
|
||||
|
||||
List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
|
||||
@@ -759,8 +762,15 @@ public class MvcNamespaceTests {
|
||||
assertEquals(".tpl", accessor.getPropertyValue("suffix"));
|
||||
assertEquals(1024, accessor.getPropertyValue("cacheLimit"));
|
||||
|
||||
assertEquals(InternalResourceViewResolver.class, resolvers.get(6).getClass());
|
||||
resolver = resolvers.get(6);
|
||||
assertThat(resolver, instanceOf(ScriptTemplateViewResolver.class));
|
||||
accessor = new DirectFieldAccessor(resolver);
|
||||
assertEquals("", accessor.getPropertyValue("prefix"));
|
||||
assertEquals("", accessor.getPropertyValue("suffix"));
|
||||
assertEquals(1024, accessor.getPropertyValue("cacheLimit"));
|
||||
|
||||
assertEquals(InternalResourceViewResolver.class, resolvers.get(7).getClass());
|
||||
assertEquals(InternalResourceViewResolver.class, resolvers.get(8).getClass());
|
||||
|
||||
TilesConfigurer tilesConfigurer = appContext.getBean(TilesConfigurer.class);
|
||||
assertNotNull(tilesConfigurer);
|
||||
@@ -787,11 +797,21 @@ public class MvcNamespaceTests {
|
||||
assertEquals("/test", groovyMarkupConfigurer.getResourceLoaderPath());
|
||||
assertTrue(groovyMarkupConfigurer.isAutoIndent());
|
||||
assertFalse(groovyMarkupConfigurer.isCacheTemplates());
|
||||
|
||||
ScriptTemplateConfigurer scriptTemplateConfigurer = appContext.getBean(ScriptTemplateConfigurer.class);
|
||||
assertNotNull(scriptTemplateConfigurer);
|
||||
assertEquals("Mustache", scriptTemplateConfigurer.getRenderObject());
|
||||
assertEquals("render", scriptTemplateConfigurer.getRenderFunction());
|
||||
assertEquals(StandardCharsets.ISO_8859_1, scriptTemplateConfigurer.getCharset());
|
||||
assertEquals("classpath:", scriptTemplateConfigurer.getResourceLoaderPath());
|
||||
String[] scripts = { "/META-INF/resources/webjars/mustachejs/0.8.2/mustache.js" };
|
||||
accessor = new DirectFieldAccessor(scriptTemplateConfigurer);
|
||||
assertArrayEquals(scripts, (String[]) accessor.getPropertyValue("scripts"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testViewResolutionWithContentNegotiation() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml", 6);
|
||||
loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml", 7);
|
||||
|
||||
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
|
||||
assertNotNull(compositeResolver);
|
||||
@@ -801,7 +821,7 @@ public class MvcNamespaceTests {
|
||||
List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
|
||||
assertEquals(ContentNegotiatingViewResolver.class, resolvers.get(0).getClass());
|
||||
ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
|
||||
assertEquals(6, cnvr.getViewResolvers().size());
|
||||
assertEquals(7, cnvr.getViewResolvers().size());
|
||||
assertEquals(1, cnvr.getDefaultViews().size());
|
||||
assertTrue(cnvr.isUseNotAcceptableStatusCode());
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,6 +34,8 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
|
||||
import org.springframework.web.servlet.view.script.ScriptTemplateConfigurer;
|
||||
import org.springframework.web.servlet.view.script.ScriptTemplateViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
@@ -60,6 +62,7 @@ public class ViewResolverRegistryTests {
|
||||
context.registerSingleton("velocityConfigurer", VelocityConfigurer.class);
|
||||
context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
|
||||
context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
|
||||
context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
|
||||
this.registry = new ViewResolverRegistry();
|
||||
this.registry.setApplicationContext(context);
|
||||
this.registry.setContentNegotiationManager(new ContentNegotiationManager());
|
||||
@@ -182,6 +185,20 @@ public class ViewResolverRegistryTests {
|
||||
checkPropertyValues(resolver, "prefix", "", "suffix", ".tpl");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scriptTemplate() {
|
||||
this.registry.scriptTemplate().prefix("/").suffix(".html").cache(true);
|
||||
ScriptTemplateViewResolver resolver = checkAndGetResolver(ScriptTemplateViewResolver.class);
|
||||
checkPropertyValues(resolver, "prefix", "/", "suffix", ".html", "cacheLimit", 1024);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scriptTemplateDefaultValues() {
|
||||
this.registry.scriptTemplate();
|
||||
ScriptTemplateViewResolver resolver = checkAndGetResolver(ScriptTemplateViewResolver.class);
|
||||
checkPropertyValues(resolver, "prefix", "", "suffix", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentNegotiation() {
|
||||
MappingJackson2JsonView view = new MappingJackson2JsonView();
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit tests for ERB templates running on JRuby.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ErbJrubyScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/erb/template.erb", model);
|
||||
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
view.renderMergedOutputModel(model, request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ScriptTemplatingConfiguration.class);
|
||||
ctx.refresh();
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
view.setUrl(viewUrl);
|
||||
view.afterPropertiesSet();
|
||||
return view;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ScriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer jrubyConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setScripts("org/springframework/web/servlet/view/script/erb/render.rb");
|
||||
configurer.setEngineName("jruby");
|
||||
configurer.setRenderFunction("render");
|
||||
return configurer;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit tests for Handlebars templates running on Nashorn Javascript engine.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class HandlebarsNashornScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/handlebars/template.html", model);
|
||||
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
view.renderMergedOutputModel(model, request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ScriptTemplatingConfiguration.class);
|
||||
ctx.refresh();
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
view.setUrl(viewUrl);
|
||||
view.afterPropertiesSet();
|
||||
return view;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ScriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer handlebarsConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setEngineName("nashorn");
|
||||
configurer.setScripts( "org/springframework/web/servlet/view/script/handlebars/polyfill.js",
|
||||
"/META-INF/resources/webjars/handlebars/3.0.0-1/handlebars.js",
|
||||
"org/springframework/web/servlet/view/script/handlebars/render.js");
|
||||
configurer.setRenderFunction("render");
|
||||
return configurer;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit tests for Mustache templates running on Nashorn Javascript engine.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class MustacheNashornScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/mustache/template.html", model);
|
||||
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
view.renderMergedOutputModel(model, request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ScriptTemplatingConfiguration.class);
|
||||
ctx.refresh();
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
view.setUrl(viewUrl);
|
||||
view.afterPropertiesSet();
|
||||
return view;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ScriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer mustacheConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setEngineName("nashorn");
|
||||
configurer.setScripts("/META-INF/resources/webjars/mustachejs/0.8.2/mustache.js");
|
||||
configurer.setRenderObject("Mustache");
|
||||
configurer.setRenderFunction("render");
|
||||
return configurer;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit tests for React templates running on Nashorn Javascript engine.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ReactNashornScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderJavascriptTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/react/template.js", model);
|
||||
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderJsxTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/react/template.jsx", model);
|
||||
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
view.renderMergedOutputModel(model, request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
if (viewUrl.endsWith(".jsx")) {
|
||||
ctx.register(JsxTemplatingConfiguration.class);
|
||||
}
|
||||
else {
|
||||
ctx.register(JavascriptTemplatingConfiguration.class);
|
||||
}
|
||||
ctx.refresh();
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
view.setUrl(viewUrl);
|
||||
view.afterPropertiesSet();
|
||||
return view;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class JavascriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer reactConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setEngineName("nashorn");
|
||||
configurer.setScripts( "org/springframework/web/servlet/view/script/react/polyfill.js",
|
||||
"/META-INF/resources/webjars/react/0.12.2/react.js",
|
||||
"/META-INF/resources/webjars/react/0.12.2/JSXTransformer.js",
|
||||
"org/springframework/web/servlet/view/script/react/render.js");
|
||||
configurer.setRenderFunction("render");
|
||||
return configurer;
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class JsxTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer reactConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setEngineName("nashorn");
|
||||
configurer.setScripts( "org/springframework/web/servlet/view/script/react/polyfill.js",
|
||||
"/META-INF/resources/webjars/react/0.12.2/react.js",
|
||||
"/META-INF/resources/webjars/react/0.12.2/JSXTransformer.js",
|
||||
"org/springframework/web/servlet/view/script/react/render.js");
|
||||
configurer.setRenderFunction("renderJsx");
|
||||
return configurer;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ScriptTemplateConfigurer}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ScriptTemplateConfigurerTests {
|
||||
|
||||
private static final String RESOURCE_LOADER_PATH = "classpath:org/springframework/web/servlet/view/script/";
|
||||
|
||||
private StaticApplicationContext applicationContext;
|
||||
|
||||
private ScriptTemplateConfigurer configurer;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.applicationContext = new StaticApplicationContext();
|
||||
this.configurer = new ScriptTemplateConfigurer();
|
||||
this.configurer.setResourceLoaderPath(RESOURCE_LOADER_PATH);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customEngineAndRenderFunction() throws Exception {
|
||||
this.configurer.setApplicationContext(this.applicationContext);
|
||||
ScriptEngine engine = mock(InvocableScriptEngine.class);
|
||||
given(engine.get("key")).willReturn("value");
|
||||
this.configurer.setEngine(engine);
|
||||
this.configurer.setRenderFunction("render");
|
||||
this.configurer.afterPropertiesSet();
|
||||
|
||||
engine = this.configurer.getEngine();
|
||||
assertNotNull(engine);
|
||||
assertEquals("value", engine.get("key"));
|
||||
assertNull(this.configurer.getRenderObject());
|
||||
assertEquals("render", this.configurer.getRenderFunction());
|
||||
assertEquals(StandardCharsets.UTF_8, this.configurer.getCharset());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void nonInvocableScriptEngine() throws Exception {
|
||||
this.configurer.setApplicationContext(this.applicationContext);
|
||||
ScriptEngine engine = mock(ScriptEngine.class);
|
||||
this.configurer.setEngine(engine);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void noRenderFunctionDefined() throws Exception {
|
||||
this.configurer.setApplicationContext(this.applicationContext);
|
||||
ScriptEngine engine = mock(InvocableScriptEngine.class);
|
||||
this.configurer.setEngine(engine);
|
||||
this.configurer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parentLoader() throws Exception {
|
||||
|
||||
this.configurer.setApplicationContext(this.applicationContext);
|
||||
|
||||
ClassLoader classLoader = this.configurer.createClassLoader();
|
||||
assertNotNull(classLoader);
|
||||
URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
|
||||
assertThat(Arrays.asList(urlClassLoader.getURLs()), Matchers.hasSize(1));
|
||||
assertThat(Arrays.asList(urlClassLoader.getURLs()).get(0).toString(),
|
||||
Matchers.endsWith("org/springframework/web/servlet/view/script/"));
|
||||
|
||||
this.configurer.setResourceLoaderPath(RESOURCE_LOADER_PATH + ",classpath:org/springframework/web/servlet/view/");
|
||||
classLoader = this.configurer.createClassLoader();
|
||||
assertNotNull(classLoader);
|
||||
urlClassLoader = (URLClassLoader) classLoader;
|
||||
assertThat(Arrays.asList(urlClassLoader.getURLs()), Matchers.hasSize(2));
|
||||
assertThat(Arrays.asList(urlClassLoader.getURLs()).get(0).toString(),
|
||||
Matchers.endsWith("org/springframework/web/servlet/view/script/"));
|
||||
assertThat(Arrays.asList(urlClassLoader.getURLs()).get(1).toString(),
|
||||
Matchers.endsWith("org/springframework/web/servlet/view/"));
|
||||
}
|
||||
|
||||
|
||||
private interface InvocableScriptEngine extends ScriptEngine, Invocable {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ScriptTemplateViewResolver}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ScriptTemplateViewResolverTests {
|
||||
|
||||
@Test
|
||||
public void viewClass() throws Exception {
|
||||
ScriptTemplateViewResolver resolver = new ScriptTemplateViewResolver();
|
||||
Assert.assertEquals(ScriptTemplateView.class, resolver.requiredViewClass());
|
||||
DirectFieldAccessor viewAccessor = new DirectFieldAccessor(resolver);
|
||||
Class viewClass = (Class) viewAccessor.getPropertyValue("viewClass");
|
||||
Assert.assertEquals(ScriptTemplateView.class, viewClass);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ScriptTemplateView}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ScriptTemplateViewTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingScriptTemplateConfig() throws Exception {
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
given(this.webAppContext.getBeansOfType(ScriptTemplateConfig.class, true, false))
|
||||
.willReturn(new HashMap<String, ScriptTemplateConfig>());
|
||||
|
||||
view.setUrl("sampleView");
|
||||
try {
|
||||
view.setApplicationContext(this.webAppContext);
|
||||
fail();
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
assertTrue(ex.getMessage().contains("ScriptTemplateConfig"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dectectScriptTemplateConfig() throws Exception {
|
||||
InvocableScriptEngine engine = mock(InvocableScriptEngine.class);
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setEngine(engine);
|
||||
configurer.setRenderObject("Template");
|
||||
configurer.setRenderFunction("render");
|
||||
configurer.setCharset(StandardCharsets.ISO_8859_1);
|
||||
Map<String, ScriptTemplateConfig> configMap = new HashMap<String, ScriptTemplateConfig>();
|
||||
configMap.put("scriptTemplateConfigurer", configurer);
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
given(this.webAppContext.getBeansOfType(ScriptTemplateConfig.class, true, false)).willReturn(configMap);
|
||||
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(view);
|
||||
view.setUrl("sampleView");
|
||||
view.setApplicationContext(this.webAppContext);
|
||||
assertEquals(engine, accessor.getPropertyValue("engine"));
|
||||
assertEquals(StandardCharsets.ISO_8859_1, accessor.getPropertyValue("charset"));
|
||||
assertEquals("Template", accessor.getPropertyValue("renderObject"));
|
||||
assertEquals("render", accessor.getPropertyValue("renderFunction"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void nonInvocableScriptEngine() throws Exception {
|
||||
ScriptEngine engine = mock(ScriptEngine.class);
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setEngine(engine);
|
||||
fail();
|
||||
}
|
||||
|
||||
private interface InvocableScriptEngine extends ScriptEngine, Invocable {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit tests for String templates running on Jython.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class StringJythonScriptTemplateTests {
|
||||
|
||||
private WebApplicationContext webAppContext;
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.webAppContext = mock(WebApplicationContext.class);
|
||||
this.servletContext = new MockServletContext();
|
||||
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplate() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("title", "Layout example");
|
||||
model.put("body", "This is the body");
|
||||
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/python/template.html", model);
|
||||
assertEquals("<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html>",
|
||||
response.getContentAsString());
|
||||
}
|
||||
|
||||
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model) throws Exception {
|
||||
ScriptTemplateView view = createViewWithUrl(viewUrl);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
view.renderMergedOutputModel(model, request, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private ScriptTemplateView createViewWithUrl(String viewUrl) throws Exception {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ScriptTemplatingConfiguration.class);
|
||||
ctx.refresh();
|
||||
|
||||
ScriptTemplateView view = new ScriptTemplateView();
|
||||
view.setApplicationContext(ctx);
|
||||
view.setUrl(viewUrl);
|
||||
view.afterPropertiesSet();
|
||||
return view;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ScriptTemplatingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ScriptTemplateConfigurer jythonConfigurer() {
|
||||
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
|
||||
configurer.setScripts("org/springframework/web/servlet/view/script/python/render.py");
|
||||
configurer.setEngineName("jython");
|
||||
configurer.setRenderFunction("render");
|
||||
return configurer;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user