Remove Velocity support
Issue: SPR-13795
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -141,8 +141,6 @@ import org.springframework.web.servlet.view.script.ScriptTemplateViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles3.SpringBeanPreparerFactory;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
@@ -742,11 +740,11 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testViewResolution() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-view-resolution.xml", 7);
|
||||
loadBeanDefinitions("mvc-config-view-resolution.xml", 6);
|
||||
|
||||
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
|
||||
assertNotNull(compositeResolver);
|
||||
assertEquals("Actual: " + compositeResolver.getViewResolvers(), 9, compositeResolver.getViewResolvers().size());
|
||||
assertEquals("Actual: " + compositeResolver.getViewResolvers(), 8, compositeResolver.getViewResolvers().size());
|
||||
assertEquals(Ordered.LOWEST_PRECEDENCE, compositeResolver.getOrder());
|
||||
|
||||
List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
|
||||
@@ -768,28 +766,21 @@ public class MvcNamespaceTests {
|
||||
assertEquals(1024, accessor.getPropertyValue("cacheLimit"));
|
||||
|
||||
resolver = resolvers.get(4);
|
||||
assertThat(resolver, instanceOf(VelocityViewResolver.class));
|
||||
accessor = new DirectFieldAccessor(resolver);
|
||||
assertEquals("", accessor.getPropertyValue("prefix"));
|
||||
assertEquals(".vm", accessor.getPropertyValue("suffix"));
|
||||
assertEquals(0, accessor.getPropertyValue("cacheLimit"));
|
||||
|
||||
resolver = resolvers.get(5);
|
||||
assertThat(resolver, instanceOf(GroovyMarkupViewResolver.class));
|
||||
accessor = new DirectFieldAccessor(resolver);
|
||||
assertEquals("", accessor.getPropertyValue("prefix"));
|
||||
assertEquals(".tpl", accessor.getPropertyValue("suffix"));
|
||||
assertEquals(1024, accessor.getPropertyValue("cacheLimit"));
|
||||
|
||||
resolver = resolvers.get(6);
|
||||
resolver = resolvers.get(5);
|
||||
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(6).getClass());
|
||||
assertEquals(InternalResourceViewResolver.class, resolvers.get(7).getClass());
|
||||
assertEquals(InternalResourceViewResolver.class, resolvers.get(8).getClass());
|
||||
|
||||
TilesConfigurer tilesConfigurer = appContext.getBean(TilesConfigurer.class);
|
||||
assertNotNull(tilesConfigurer);
|
||||
@@ -808,11 +799,6 @@ public class MvcNamespaceTests {
|
||||
accessor = new DirectFieldAccessor(freeMarkerConfigurer);
|
||||
assertArrayEquals(new String[] {"/", "/test"}, (String[]) accessor.getPropertyValue("templateLoaderPaths"));
|
||||
|
||||
VelocityConfigurer velocityConfigurer = appContext.getBean(VelocityConfigurer.class);
|
||||
assertNotNull(velocityConfigurer);
|
||||
accessor = new DirectFieldAccessor(velocityConfigurer);
|
||||
assertEquals("/test", accessor.getPropertyValue("resourceLoaderPath"));
|
||||
|
||||
GroovyMarkupConfigurer groovyMarkupConfigurer = appContext.getBean(GroovyMarkupConfigurer.class);
|
||||
assertNotNull(groovyMarkupConfigurer);
|
||||
assertEquals("/test", groovyMarkupConfigurer.getResourceLoaderPath());
|
||||
@@ -833,7 +819,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testViewResolutionWithContentNegotiation() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml", 7);
|
||||
loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml", 6);
|
||||
|
||||
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
|
||||
assertNotNull(compositeResolver);
|
||||
@@ -843,7 +829,7 @@ public class MvcNamespaceTests {
|
||||
List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
|
||||
assertEquals(ContentNegotiatingViewResolver.class, resolvers.get(0).getClass());
|
||||
ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
|
||||
assertEquals(7, cnvr.getViewResolvers().size());
|
||||
assertEquals(6, cnvr.getViewResolvers().size());
|
||||
assertEquals(1, cnvr.getDefaultViews().size());
|
||||
assertTrue(cnvr.isUseNotAcceptableStatusCode());
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ 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.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -61,12 +60,6 @@ public class ViewResolutionIntegrationTests {
|
||||
assertEquals("<html><body>Hello World!</body></html>", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocity() throws Exception {
|
||||
MockHttpServletResponse response = runTest(VelocityWebConfig.class);
|
||||
assertEquals("<html><body>Hello World!</body></html>", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tiles() throws Exception {
|
||||
MockHttpServletResponse response = runTest(TilesWebConfig.class);
|
||||
@@ -85,12 +78,6 @@ public class ViewResolutionIntegrationTests {
|
||||
runTest(InvalidFreeMarkerWebConfig.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocityInvalidConfig() throws Exception {
|
||||
this.thrown.expectMessage("In addition to a Velocity view resolver ");
|
||||
runTest(InvalidVelocityWebConfig.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tilesInvalidConfig() throws Exception {
|
||||
this.thrown.expectMessage("In addition to a Tiles view resolver ");
|
||||
@@ -165,22 +152,6 @@ public class ViewResolutionIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class VelocityWebConfig extends AbstractWebConfig {
|
||||
|
||||
@Override
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
registry.velocity();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VelocityConfigurer velocityConfigurer() {
|
||||
VelocityConfigurer configurer = new VelocityConfigurer();
|
||||
configurer.setResourceLoaderPath("/WEB-INF/");
|
||||
return configurer;
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TilesWebConfig extends AbstractWebConfig {
|
||||
|
||||
@@ -222,15 +193,6 @@ public class ViewResolutionIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class InvalidVelocityWebConfig extends WebMvcConfigurationSupport {
|
||||
|
||||
@Override
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
registry.velocity();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class InvalidTilesWebConfig extends WebMvcConfigurationSupport {
|
||||
|
||||
|
||||
@@ -38,8 +38,6 @@ 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;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
import org.springframework.web.servlet.view.xml.MarshallingView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -59,7 +57,6 @@ public class ViewResolverRegistryTests {
|
||||
public void setUp() {
|
||||
StaticWebApplicationContext context = new StaticWebApplicationContext();
|
||||
context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
|
||||
context.registerSingleton("velocityConfigurer", VelocityConfigurer.class);
|
||||
context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
|
||||
context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
|
||||
context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
|
||||
@@ -79,7 +76,7 @@ public class ViewResolverRegistryTests {
|
||||
@Test
|
||||
public void hasRegistrations() {
|
||||
assertFalse(this.registry.hasRegistrations());
|
||||
this.registry.velocity();
|
||||
this.registry.freeMarker();
|
||||
assertTrue(this.registry.hasRegistrations());
|
||||
}
|
||||
|
||||
@@ -141,20 +138,6 @@ public class ViewResolverRegistryTests {
|
||||
checkAndGetResolver(TilesViewResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocity() {
|
||||
this.registry.velocity().prefix("/").suffix(".vm").cache(true);
|
||||
VelocityViewResolver resolver = checkAndGetResolver(VelocityViewResolver.class);
|
||||
checkPropertyValues(resolver, "prefix", "/", "suffix", ".vm", "cacheLimit", 1024);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocityDefaultValues() {
|
||||
this.registry.velocity();
|
||||
VelocityViewResolver resolver = checkAndGetResolver(VelocityViewResolver.class);
|
||||
checkPropertyValues(resolver, "prefix", "", "suffix", ".vm");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void freeMarker() {
|
||||
this.registry.freeMarker().prefix("/").suffix(".fmt").cache(false);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -21,7 +21,6 @@ import java.util.Locale;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -50,13 +49,6 @@ public class JasperReportViewResolverTests {
|
||||
assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setIncorrectViewClass() {
|
||||
// Should not be able to set view class to a class that does not extend
|
||||
// AbstractJasperReportsView.
|
||||
new JasperReportsViewResolver().setViewClass(VelocityView.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withViewNamesAndEndsWithPattern() throws Exception {
|
||||
doViewNamesTest("DataSource*");
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* 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.velocity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.exception.ResourceNotFoundException;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 09.10.2004
|
||||
*/
|
||||
class TestVelocityEngine extends VelocityEngine {
|
||||
|
||||
private final Map<String, Template> templates = new HashMap<>();
|
||||
|
||||
|
||||
public TestVelocityEngine() {
|
||||
}
|
||||
|
||||
public TestVelocityEngine(String expectedName, Template template) {
|
||||
addTemplate(expectedName, template);
|
||||
}
|
||||
|
||||
public void addTemplate(String expectedName, Template template) {
|
||||
this.templates.put(expectedName, template);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Template getTemplate(String name) throws ResourceNotFoundException {
|
||||
Template template = this.templates.get(name);
|
||||
if (template == null) {
|
||||
throw new ResourceNotFoundException("No template registered for name [" + name + "]");
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
/*
|
||||
* 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.velocity;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.exception.VelocityException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.DescriptiveResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
|
||||
import org.springframework.ui.velocity.VelocityEngineUtils;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class VelocityConfigurerTests {
|
||||
|
||||
@Test
|
||||
public void velocityEngineFactoryBeanWithConfigLocation() throws VelocityException {
|
||||
VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
|
||||
vefb.setConfigLocation(new FileSystemResource("myprops.properties"));
|
||||
Properties props = new Properties();
|
||||
props.setProperty("myprop", "/mydir");
|
||||
vefb.setVelocityProperties(props);
|
||||
try {
|
||||
vefb.afterPropertiesSet();
|
||||
fail("Should have thrown IOException");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException {
|
||||
VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
|
||||
Properties props = new Properties();
|
||||
props.setProperty("myprop", "/mydir");
|
||||
vefb.setVelocityProperties(props);
|
||||
Object value = new Object();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("myentry", value);
|
||||
vefb.setVelocityPropertiesMap(map);
|
||||
vefb.afterPropertiesSet();
|
||||
assertThat(vefb.getObject(), instanceOf(VelocityEngine.class));
|
||||
VelocityEngine ve = vefb.getObject();
|
||||
assertEquals("/mydir", ve.getProperty("myprop"));
|
||||
assertEquals(value, ve.getProperty("myentry"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocityEngineFactoryBeanWithResourceLoaderPath() throws IOException, VelocityException {
|
||||
VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
|
||||
vefb.setResourceLoaderPath("file:/mydir");
|
||||
vefb.afterPropertiesSet();
|
||||
assertThat(vefb.getObject(), instanceOf(VelocityEngine.class));
|
||||
VelocityEngine ve = vefb.getObject();
|
||||
assertEquals(new File("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void velocityEngineFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
|
||||
VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
|
||||
vefb.setResourceLoaderPath("file:/mydir");
|
||||
vefb.setResourceLoader(new ResourceLoader() {
|
||||
@Override
|
||||
public Resource getResource(String location) {
|
||||
if (location.equals("file:/mydir") || location.equals("file:/mydir/test")) {
|
||||
return new ByteArrayResource("test".getBytes(), "test");
|
||||
}
|
||||
try {
|
||||
return new UrlResource(location);
|
||||
}
|
||||
catch (MalformedURLException ex) {
|
||||
throw new IllegalArgumentException(ex.toString());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public ClassLoader getClassLoader() {
|
||||
return getClass().getClassLoader();
|
||||
}
|
||||
});
|
||||
vefb.afterPropertiesSet();
|
||||
assertThat(vefb.getObject(), instanceOf(VelocityEngine.class));
|
||||
VelocityEngine ve = vefb.getObject();
|
||||
assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", Collections.emptyMap()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocityConfigurer() throws IOException, VelocityException {
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setResourceLoaderPath("file:/mydir");
|
||||
vc.afterPropertiesSet();
|
||||
assertThat(vc.createVelocityEngine(), instanceOf(VelocityEngine.class));
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
assertEquals(new File("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void velocityConfigurerWithCsvPath() throws IOException, VelocityException {
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setResourceLoaderPath("file:/mydir,file:/yourdir");
|
||||
vc.afterPropertiesSet();
|
||||
assertThat(vc.createVelocityEngine(), instanceOf(VelocityEngine.class));
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
Vector<String> paths = new Vector<>();
|
||||
paths.add(new File("/mydir").getAbsolutePath());
|
||||
paths.add(new File("/yourdir").getAbsolutePath());
|
||||
assertEquals(paths, ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void velocityConfigurerWithCsvPathAndNonFileAccess() throws IOException, VelocityException {
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setResourceLoaderPath("file:/mydir,file:/yourdir");
|
||||
vc.setResourceLoader(new ResourceLoader() {
|
||||
@Override
|
||||
public Resource getResource(String location) {
|
||||
if ("file:/yourdir/test".equals(location)) {
|
||||
return new DescriptiveResource("");
|
||||
}
|
||||
return new ByteArrayResource("test".getBytes(), "test");
|
||||
}
|
||||
@Override
|
||||
public ClassLoader getClassLoader() {
|
||||
return getClass().getClassLoader();
|
||||
}
|
||||
});
|
||||
vc.setPreferFileSystemAccess(false);
|
||||
vc.afterPropertiesSet();
|
||||
assertThat(vc.createVelocityEngine(), instanceOf(VelocityEngine.class));
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", Collections.emptyMap()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,266 +0,0 @@
|
||||
/*
|
||||
* 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.velocity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.context.Context;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.web.servlet.support.BindStatus;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.theme.FixedThemeResolver;
|
||||
import org.springframework.web.servlet.view.DummyMacroRequestContext;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Darren Davison
|
||||
* @author Juergen Hoeller
|
||||
* @since 18.06.2004
|
||||
*/
|
||||
public class VelocityMacroTests {
|
||||
|
||||
private static final String TEMPLATE_FILE = "test.vm";
|
||||
|
||||
private StaticWebApplicationContext wac;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
|
||||
final Template expectedTemplate = new Template();
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine(TEMPLATE_FILE, expectedTemplate);
|
||||
}
|
||||
};
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
|
||||
wac.refresh();
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
|
||||
response = new MockHttpServletResponse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exposeSpringMacroHelpers() throws Exception {
|
||||
VelocityView vv = new VelocityView() {
|
||||
@Override
|
||||
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) {
|
||||
assertTrue(context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext);
|
||||
RequestContext rc = (RequestContext) context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
|
||||
BindStatus status = rc.getBindStatus("tb.name");
|
||||
assertEquals("name", status.getExpression());
|
||||
assertEquals("juergen", status.getValue());
|
||||
}
|
||||
};
|
||||
vv.setUrl(TEMPLATE_FILE);
|
||||
vv.setApplicationContext(wac);
|
||||
vv.setExposeSpringMacroHelpers(true);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("tb", new TestBean("juergen", 99));
|
||||
vv.render(model, request, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void springMacroRequestContextAttributeUsed() {
|
||||
final String helperTool = "wrongType";
|
||||
|
||||
VelocityView vv = new VelocityView() {
|
||||
@Override
|
||||
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) {
|
||||
fail();
|
||||
}
|
||||
};
|
||||
vv.setUrl(TEMPLATE_FILE);
|
||||
vv.setApplicationContext(wac);
|
||||
vv.setExposeSpringMacroHelpers(true);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool);
|
||||
|
||||
try {
|
||||
vv.render(model, request, response);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
assertTrue(ex instanceof ServletException);
|
||||
assertTrue(ex.getMessage().contains(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allMacros() throws Exception {
|
||||
DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
|
||||
Map<String, String> msgMap = new HashMap<String, String>();
|
||||
msgMap.put("hello", "Howdy");
|
||||
msgMap.put("world", "Mundo");
|
||||
rc.setMessageMap(msgMap);
|
||||
Map<String, String> themeMsgMap = new HashMap<String, String>();
|
||||
themeMsgMap.put("hello", "Howdy!");
|
||||
themeMsgMap.put("world", "Mundo!");
|
||||
rc.setThemeMessageMap(themeMsgMap);
|
||||
rc.setContextPath("/springtest");
|
||||
|
||||
TestBean tb = new TestBean("Darren", 99);
|
||||
tb.setJedi(true);
|
||||
tb.setStringArray(new String[] {"John", "Fred"});
|
||||
request.setAttribute("command", tb);
|
||||
|
||||
Map<String, String> names = new HashMap<String, String>();
|
||||
names.put("Darren", "Darren Davison");
|
||||
names.put("John", "John Doe");
|
||||
names.put("Fred", "Fred Bloggs");
|
||||
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setPreferFileSystemAccess(false);
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("command", tb);
|
||||
model.put("springMacroRequestContext", rc);
|
||||
model.put("nameOptionMap", names);
|
||||
|
||||
VelocityView view = new VelocityView();
|
||||
view.setBeanName("myView");
|
||||
view.setUrl("org/springframework/web/servlet/view/velocity/test.vm");
|
||||
view.setEncoding("UTF-8");
|
||||
view.setExposeSpringMacroHelpers(false);
|
||||
view.setVelocityEngine(ve);
|
||||
|
||||
view.render(model, request, response);
|
||||
|
||||
// tokenize output and ignore whitespace
|
||||
String output = response.getContentAsString();
|
||||
System.out.println(output);
|
||||
String[] tokens = StringUtils.tokenizeToStringArray(output, "\t\n");
|
||||
|
||||
for (int i = 0; i < tokens.length; i++) {
|
||||
if (tokens[i].equals("NAME")) assertEquals("Darren", tokens[i + 1]);
|
||||
if (tokens[i].equals("AGE")) assertEquals("99", tokens[i + 1]);
|
||||
if (tokens[i].equals("MESSAGE")) assertEquals("Howdy Mundo", tokens[i + 1]);
|
||||
if (tokens[i].equals("DEFAULTMESSAGE")) assertEquals("hi planet", tokens[i + 1]);
|
||||
if (tokens[i].equals("THEME")) assertEquals("Howdy! Mundo!", tokens[i + 1]);
|
||||
if (tokens[i].equals("DEFAULTTHEME")) assertEquals("hi! planet!", tokens[i + 1]);
|
||||
if (tokens[i].equals("URL")) assertEquals("/springtest/aftercontext.html", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM1")) assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM2")) assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" class=\"myCssClass\">", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM3")) assertEquals("<textarea id=\"name\" name=\"name\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM3")) assertEquals("Darren</textarea>", tokens[i + 2]);
|
||||
if (tokens[i].equals("FORM4")) assertEquals("<textarea id=\"name\" name=\"name\" rows=10 cols=30>", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM4")) assertEquals("Darren</textarea>", tokens[i + 2]);
|
||||
//TODO verify remaining output (fix whitespace)
|
||||
if (tokens[i].equals("FORM9")) assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM10")) assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"hidden\" name=\"_name\" value=\"on\"/>", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"checkbox\" id=\"name\" name=\"name\" />", tokens[i + 2]);
|
||||
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"hidden\" name=\"_jedi\" value=\"on\"/>", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"checked\" />", tokens[i + 2]);
|
||||
}
|
||||
}
|
||||
|
||||
// SPR-5172
|
||||
|
||||
@Test
|
||||
public void idContainsBraces() throws Exception {
|
||||
DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
|
||||
Map<String, String> msgMap = new HashMap<String, String>();
|
||||
msgMap.put("hello", "Howdy");
|
||||
msgMap.put("world", "Mundo");
|
||||
rc.setMessageMap(msgMap);
|
||||
Map<String, String> themeMsgMap = new HashMap<String, String>();
|
||||
themeMsgMap.put("hello", "Howdy!");
|
||||
themeMsgMap.put("world", "Mundo!");
|
||||
rc.setThemeMessageMap(themeMsgMap);
|
||||
rc.setContextPath("/springtest");
|
||||
|
||||
TestBean darren = new TestBean("Darren", 99);
|
||||
TestBean fred = new TestBean("Fred");
|
||||
fred.setJedi(true);
|
||||
darren.setSpouse(fred);
|
||||
darren.setJedi(true);
|
||||
darren.setStringArray(new String[] {"John", "Fred"});
|
||||
request.setAttribute("command", darren);
|
||||
|
||||
Map<String, String> names = new HashMap<String, String>();
|
||||
names.put("Darren", "Darren Davison");
|
||||
names.put("John", "John Doe");
|
||||
names.put("Fred", "Fred Bloggs");
|
||||
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setPreferFileSystemAccess(false);
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("command", darren);
|
||||
model.put("springMacroRequestContext", rc);
|
||||
model.put("nameOptionMap", names);
|
||||
|
||||
VelocityView view = new VelocityView();
|
||||
view.setBeanName("myView");
|
||||
view.setUrl("org/springframework/web/servlet/view/velocity/test-spr5172.vm");
|
||||
view.setEncoding("UTF-8");
|
||||
view.setExposeSpringMacroHelpers(false);
|
||||
view.setVelocityEngine(ve);
|
||||
|
||||
view.render(model, request, response);
|
||||
|
||||
// tokenize output and ignore whitespace
|
||||
String output = response.getContentAsString();
|
||||
String[] tokens = StringUtils.tokenizeToStringArray(output, "\t\n");
|
||||
|
||||
for (int i = 0; i < tokens.length; i++) {
|
||||
if (tokens[i].equals("FORM1")) assertEquals("<input type=\"text\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"Fred\" >", tokens[i + 1]); //
|
||||
if (tokens[i].equals("FORM2")) assertEquals("<textarea id=\"spouses0.name\" name=\"spouses[0].name\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM2")) assertEquals("Fred</textarea>", tokens[i + 2]);
|
||||
if (tokens[i].equals("FORM3")) assertEquals("<select id=\"spouses0.name\" name=\"spouses[0].name\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM4")) assertEquals("<select multiple=\"multiple\" id=\"spouses\" name=\"spouses\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM5")) assertEquals("<input type=\"radio\" name=\"spouses[0].name\" value=\"Darren\"", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM6")) assertEquals("<input type=\"password\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM7")) assertEquals("<input type=\"hidden\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"Fred\" >", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM8")) assertEquals("<input type=\"hidden\" name=\"_spouses0.name\" value=\"on\"/>", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM8")) assertEquals("<input type=\"checkbox\" id=\"spouses0.name\" name=\"spouses[0].name\" />", tokens[i + 2]);
|
||||
if (tokens[i].equals("FORM9")) assertEquals("<input type=\"hidden\" name=\"_spouses0.jedi\" value=\"on\"/>", tokens[i + 1]);
|
||||
if (tokens[i].equals("FORM9")) assertEquals("<input type=\"checkbox\" id=\"spouses0.jedi\" name=\"spouses[0].jedi\" checked=\"checked\" />", tokens[i + 2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.velocity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.exception.MethodInvocationException;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.web.servlet.theme.FixedThemeResolver;
|
||||
import org.springframework.web.util.NestedServletException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class VelocityRenderTests {
|
||||
|
||||
private StaticWebApplicationContext wac;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
|
||||
final Template expectedTemplate = new Template();
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine("test.vm", expectedTemplate);
|
||||
}
|
||||
};
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
|
||||
wac.refresh();
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
|
||||
response = new MockHttpServletResponse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleRender() throws Exception {
|
||||
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setPreferFileSystemAccess(false);
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
|
||||
VelocityView view = new VelocityView();
|
||||
view.setBeanName("myView");
|
||||
view.setUrl("org/springframework/web/servlet/view/velocity/simple.vm");
|
||||
view.setVelocityEngine(ve);
|
||||
view.setApplicationContext(wac);
|
||||
|
||||
|
||||
Map<String,Object> model = new HashMap<String,Object>();
|
||||
model.put("command", new TestBean("juergen", 99));
|
||||
view.render(model, request, response);
|
||||
assertEquals("\nNAME\njuergen\n", response.getContentAsString().replace("\r\n", "\n"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // This works with Velocity 1.6.2
|
||||
public void testSimpleRenderWithError() throws Exception {
|
||||
|
||||
thrown.expect(NestedServletException.class);
|
||||
|
||||
thrown.expect(new TypeSafeMatcher<Exception>() {
|
||||
@Override
|
||||
public boolean matchesSafely(Exception item) {
|
||||
return item.getCause() instanceof MethodInvocationException;
|
||||
}
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("exception has cause of MethodInvocationException");
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setPreferFileSystemAccess(false);
|
||||
vc.setVelocityPropertiesMap(Collections.<String,Object>singletonMap("runtime.references.strict", "true"));
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
|
||||
VelocityView view = new VelocityView();
|
||||
view.setBeanName("myView");
|
||||
view.setUrl("org/springframework/web/servlet/view/velocity/error.vm");
|
||||
view.setVelocityEngine(ve);
|
||||
view.setApplicationContext(wac);
|
||||
|
||||
Map<String,Object> model = new HashMap<String,Object>();
|
||||
model.put("command", new TestBean("juergen", 99));
|
||||
view.render(model, request, response);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleRenderWithIOError() throws Exception {
|
||||
|
||||
thrown.expect(NestedServletException.class);
|
||||
|
||||
thrown.expect(new TypeSafeMatcher<Exception>() {
|
||||
@Override
|
||||
public boolean matchesSafely(Exception item) {
|
||||
return item.getCause() instanceof IOException;
|
||||
}
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("exception has cause of IOException");
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
VelocityConfigurer vc = new VelocityConfigurer();
|
||||
vc.setPreferFileSystemAccess(false);
|
||||
vc.setVelocityPropertiesMap(Collections.<String,Object>singletonMap("runtime.references.strict", "true"));
|
||||
VelocityEngine ve = vc.createVelocityEngine();
|
||||
|
||||
VelocityView view = new VelocityView();
|
||||
view.setBeanName("myView");
|
||||
view.setUrl("org/springframework/web/servlet/view/velocity/ioerror.vm");
|
||||
view.setVelocityEngine(ve);
|
||||
view.setApplicationContext(wac);
|
||||
|
||||
Map<String,Object> model = new HashMap<String,Object>();
|
||||
model.put("command", new TestBean("juergen", 99));
|
||||
view.render(model, request, response);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package org.springframework.web.servlet.view.velocity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.context.Context;
|
||||
import org.apache.velocity.tools.generic.DateTool;
|
||||
import org.apache.velocity.tools.generic.MathTool;
|
||||
import org.apache.velocity.tools.view.context.ChainedContext;
|
||||
import org.apache.velocity.tools.view.tools.LinkTool;
|
||||
import org.junit.Test;
|
||||
|
||||
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.support.StaticWebApplicationContext;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class VelocityToolboxViewTests {
|
||||
|
||||
@Test
|
||||
public void testVelocityToolboxView() throws Exception {
|
||||
final String templateName = "test.vm";
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
final Template expectedTemplate = new Template();
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine(templateName, expectedTemplate);
|
||||
}
|
||||
};
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
|
||||
|
||||
final HttpServletRequest expectedRequest = new MockHttpServletRequest();
|
||||
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
|
||||
|
||||
VelocityToolboxView vv = new VelocityToolboxView() {
|
||||
@Override
|
||||
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
|
||||
assertTrue(template == expectedTemplate);
|
||||
assertTrue(response == expectedResponse);
|
||||
assertTrue(context instanceof ChainedContext);
|
||||
|
||||
assertEquals("this is foo.", context.get("foo"));
|
||||
assertTrue(context.get("map") instanceof HashMap<?,?>);
|
||||
assertTrue(context.get("date") instanceof DateTool);
|
||||
assertTrue(context.get("math") instanceof MathTool);
|
||||
|
||||
assertTrue(context.get("link") instanceof LinkTool);
|
||||
LinkTool linkTool = (LinkTool) context.get("link");
|
||||
assertNotNull(linkTool.getContextURL());
|
||||
|
||||
assertTrue(context.get("link2") instanceof LinkTool);
|
||||
LinkTool linkTool2 = (LinkTool) context.get("link2");
|
||||
assertNotNull(linkTool2.getContextURL());
|
||||
}
|
||||
};
|
||||
|
||||
vv.setUrl(templateName);
|
||||
vv.setApplicationContext(wac);
|
||||
Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>();
|
||||
toolAttributes.put("math", MathTool.class);
|
||||
toolAttributes.put("link2", LinkTool.class);
|
||||
vv.setToolAttributes(toolAttributes);
|
||||
vv.setToolboxConfigLocation("org/springframework/web/servlet/view/velocity/toolbox.xml");
|
||||
vv.setExposeSpringMacroHelpers(false);
|
||||
|
||||
vv.render(new HashMap<String,Object>(), expectedRequest, expectedResponse);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
package org.springframework.web.servlet.view.velocity;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.view.InternalResourceView;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class VelocityViewResolverTests {
|
||||
|
||||
@Test
|
||||
public void testVelocityViewResolver() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine("prefix_test_suffix", new Template());
|
||||
}
|
||||
};
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.getBeanFactory().registerSingleton("configurer", vc);
|
||||
wac.refresh();
|
||||
|
||||
VelocityViewResolver vr = new VelocityViewResolver();
|
||||
vr.setPrefix("prefix_");
|
||||
vr.setSuffix("_suffix");
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("test", Locale.CANADA);
|
||||
assertEquals("Correct view class", VelocityView.class, view.getClass());
|
||||
assertEquals("Correct URL", "prefix_test_suffix", ((VelocityView) view).getUrl());
|
||||
|
||||
view = vr.resolveViewName("non-existing", Locale.CANADA);
|
||||
assertNull(view);
|
||||
|
||||
view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
|
||||
assertEquals("Correct view class", RedirectView.class, view.getClass());
|
||||
assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
|
||||
|
||||
view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
|
||||
assertEquals("Correct view class", InternalResourceView.class, view.getClass());
|
||||
assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVelocityViewResolverWithToolbox() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine("prefix_test_suffix", new Template());
|
||||
}
|
||||
};
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.getBeanFactory().registerSingleton("configurer", vc);
|
||||
wac.refresh();
|
||||
|
||||
String toolbox = "org/springframework/web/servlet/view/velocity/toolbox.xml";
|
||||
|
||||
VelocityViewResolver vr = new VelocityViewResolver();
|
||||
vr.setPrefix("prefix_");
|
||||
vr.setSuffix("_suffix");
|
||||
vr.setToolboxConfigLocation(toolbox);
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("test", Locale.CANADA);
|
||||
assertEquals("Correct view class", VelocityToolboxView.class, view.getClass());
|
||||
assertEquals("Correct URL", "prefix_test_suffix", ((VelocityView) view).getUrl());
|
||||
assertEquals("Correct toolbox", toolbox, ((VelocityToolboxView) view).getToolboxConfigLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVelocityViewResolverWithToolboxSubclass() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
TestVelocityEngine ve = new TestVelocityEngine();
|
||||
ve.addTemplate("prefix_test_suffix", new Template());
|
||||
ve.addTemplate(VelocityLayoutView.DEFAULT_LAYOUT_URL, new Template());
|
||||
return ve;
|
||||
}
|
||||
};
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.getBeanFactory().registerSingleton("configurer", vc);
|
||||
wac.refresh();
|
||||
|
||||
String toolbox = "org/springframework/web/servlet/view/velocity/toolbox.xml";
|
||||
|
||||
VelocityViewResolver vr = new VelocityViewResolver();
|
||||
vr.setViewClass(VelocityLayoutView.class);
|
||||
vr.setPrefix("prefix_");
|
||||
vr.setSuffix("_suffix");
|
||||
vr.setToolboxConfigLocation(toolbox);
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("test", Locale.CANADA);
|
||||
assertEquals("Correct view class", VelocityLayoutView.class, view.getClass());
|
||||
assertEquals("Correct URL", "prefix_test_suffix", ((VelocityView) view).getUrl());
|
||||
assertEquals("Correct toolbox", toolbox, ((VelocityToolboxView) view).getToolboxConfigLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVelocityLayoutViewResolver() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
TestVelocityEngine ve = new TestVelocityEngine();
|
||||
ve.addTemplate("prefix_test_suffix", new Template());
|
||||
ve.addTemplate("myLayoutUrl", new Template());
|
||||
return ve;
|
||||
}
|
||||
};
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.getBeanFactory().registerSingleton("configurer", vc);
|
||||
wac.refresh();
|
||||
|
||||
VelocityLayoutViewResolver vr = new VelocityLayoutViewResolver();
|
||||
vr.setPrefix("prefix_");
|
||||
vr.setSuffix("_suffix");
|
||||
vr.setLayoutUrl("myLayoutUrl");
|
||||
vr.setLayoutKey("myLayoutKey");
|
||||
vr.setScreenContentKey("myScreenContentKey");
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("test", Locale.CANADA);
|
||||
assertEquals("Correct view class", VelocityLayoutView.class, view.getClass());
|
||||
assertEquals("Correct URL", "prefix_test_suffix", ((VelocityView) view).getUrl());
|
||||
// TODO: Need to test actual VelocityLayoutView properties and their functionality!
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,280 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.velocity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.context.Context;
|
||||
import org.apache.velocity.exception.MethodInvocationException;
|
||||
import org.apache.velocity.exception.ParseErrorException;
|
||||
import org.apache.velocity.tools.generic.DateTool;
|
||||
import org.apache.velocity.tools.generic.MathTool;
|
||||
import org.apache.velocity.tools.generic.NumberTool;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
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;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
||||
import org.springframework.web.servlet.view.AbstractView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class VelocityViewTests {
|
||||
|
||||
@Test
|
||||
public void testNoVelocityConfig() throws Exception {
|
||||
VelocityView vv = new VelocityView();
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(new HashMap<String, VelocityConfig>());
|
||||
|
||||
vv.setUrl("anythingButNull");
|
||||
try {
|
||||
vv.setApplicationContext(wac);
|
||||
fail();
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
// Check there's a helpful error message
|
||||
assertTrue(ex.getMessage().contains("VelocityConfig"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoTemplateName() throws Exception {
|
||||
VelocityView vv = new VelocityView();
|
||||
try {
|
||||
vv.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// Check there's a helpful error message
|
||||
assertTrue(ex.getMessage().indexOf("url") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeTemplateSucceeds() throws Exception {
|
||||
testValidTemplateName(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeTemplateFailureWithIOException() throws Exception {
|
||||
testValidTemplateName(new IOException());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeTemplateFailureWithParseErrorException() throws Exception {
|
||||
testValidTemplateName(new ParseErrorException(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeTemplateFailureWithUnspecifiedException() throws Exception {
|
||||
testValidTemplateName(new Exception(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeTemplateFailureWithMethodInvocationException() throws Exception {
|
||||
testValidTemplateName(new MethodInvocationException("Bad template", null, "none", "foo.vm", 1, 100));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mergeTemplateFailureException may be null in which case mergeTemplate override will succeed.
|
||||
* If it's non null it will be checked
|
||||
*/
|
||||
private void testValidTemplateName(final Exception mergeTemplateFailureException) throws Exception {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("foo", "bar");
|
||||
|
||||
final String templateName = "test.vm";
|
||||
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
final Template expectedTemplate = new Template();
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine(templateName, expectedTemplate);
|
||||
}
|
||||
};
|
||||
Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
|
||||
configurers.put("velocityConfigurer", vc);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);
|
||||
given(wac.getServletContext()).willReturn(sc);
|
||||
given(wac.getBean("requestDataValueProcessor",
|
||||
RequestDataValueProcessor.class)).willReturn(null);
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
|
||||
|
||||
VelocityView vv = new VelocityView() {
|
||||
@Override
|
||||
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
|
||||
assertTrue(template == expectedTemplate);
|
||||
assertTrue(context.getKeys().length >= 1);
|
||||
assertTrue(context.get("foo").equals("bar"));
|
||||
assertTrue(response == expectedResponse);
|
||||
if (mergeTemplateFailureException != null) {
|
||||
throw mergeTemplateFailureException;
|
||||
}
|
||||
}
|
||||
};
|
||||
vv.setUrl(templateName);
|
||||
vv.setApplicationContext(wac);
|
||||
|
||||
try {
|
||||
vv.render(model, request, expectedResponse);
|
||||
if (mergeTemplateFailureException != null) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
assertNotNull(mergeTemplateFailureException);
|
||||
assertEquals(ex, mergeTemplateFailureException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeepExistingContentType() throws Exception {
|
||||
final String templateName = "test.vm";
|
||||
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
final Template expectedTemplate = new Template();
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine(templateName, expectedTemplate);
|
||||
}
|
||||
};
|
||||
Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
|
||||
configurers.put("velocityConfigurer", vc);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);
|
||||
given(wac.getServletContext()).willReturn(sc);
|
||||
given(wac.getBean("requestDataValueProcessor",
|
||||
RequestDataValueProcessor.class)).willReturn(null);
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
|
||||
expectedResponse.setContentType("myContentType");
|
||||
|
||||
VelocityView vv = new VelocityView() {
|
||||
@Override
|
||||
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) {
|
||||
assertTrue(template == expectedTemplate);
|
||||
assertTrue(response == expectedResponse);
|
||||
}
|
||||
@Override
|
||||
protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
|
||||
model.put("myHelper", "myValue");
|
||||
}
|
||||
};
|
||||
|
||||
vv.setUrl(templateName);
|
||||
vv.setApplicationContext(wac);
|
||||
vv.render(new HashMap<String, Object>(), request, expectedResponse);
|
||||
|
||||
assertEquals("myContentType", expectedResponse.getContentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExposeHelpers() throws Exception {
|
||||
final String templateName = "test.vm";
|
||||
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
given(wac.getServletContext()).willReturn(new MockServletContext());
|
||||
|
||||
final Template expectedTemplate = new Template();
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
@Override
|
||||
public VelocityEngine getVelocityEngine() {
|
||||
return new TestVelocityEngine(templateName, expectedTemplate);
|
||||
}
|
||||
};
|
||||
Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
|
||||
configurers.put("velocityConfigurer", vc);
|
||||
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);
|
||||
|
||||
|
||||
// let it ask for locale
|
||||
HttpServletRequest req = mock(HttpServletRequest.class);
|
||||
given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null);
|
||||
given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver());
|
||||
given(req.getLocale()).willReturn(Locale.CANADA);
|
||||
|
||||
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
|
||||
|
||||
VelocityView vv = new VelocityView() {
|
||||
@Override
|
||||
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
|
||||
assertTrue(template == expectedTemplate);
|
||||
assertTrue(response == expectedResponse);
|
||||
|
||||
assertEquals("myValue", context.get("myHelper"));
|
||||
assertTrue(context.get("math") instanceof MathTool);
|
||||
|
||||
assertTrue(context.get("dateTool") instanceof DateTool);
|
||||
DateTool dateTool = (DateTool) context.get("dateTool");
|
||||
assertTrue(dateTool.getLocale().equals(Locale.CANADA));
|
||||
|
||||
assertTrue(context.get("numberTool") instanceof NumberTool);
|
||||
NumberTool numberTool = (NumberTool) context.get("numberTool");
|
||||
assertTrue(numberTool.getLocale().equals(Locale.CANADA));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
|
||||
model.put("myHelper", "myValue");
|
||||
}
|
||||
};
|
||||
|
||||
vv.setUrl(templateName);
|
||||
vv.setApplicationContext(wac);
|
||||
Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>();
|
||||
toolAttributes.put("math", MathTool.class);
|
||||
vv.setToolAttributes(toolAttributes);
|
||||
vv.setDateToolAttribute("dateTool");
|
||||
vv.setNumberToolAttribute("numberTool");
|
||||
vv.setExposeSpringMacroHelpers(false);
|
||||
|
||||
vv.render(new HashMap<String, Object>(), req, expectedResponse);
|
||||
|
||||
assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user