SPR-6246: split up Velocity test cases for neatness. Add fix for bug in exception creation in VelocityView.
This commit is contained in:
parent
3346752cfd
commit
bce723d9e3
|
|
@ -126,7 +126,13 @@
|
|||
<version>1.1.2</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<!-- dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity</artifactId>
|
||||
<version>1.6.2</version>
|
||||
<optional>true</optional>
|
||||
</dependency-->
|
||||
<dependency>
|
||||
<groupId>velocity</groupId>
|
||||
<artifactId>velocity</artifactId>
|
||||
<version>1.5</version>
|
||||
|
|
|
|||
|
|
@ -73,6 +73,7 @@ import org.springframework.web.util.NestedServletException;
|
|||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Dave Syer
|
||||
* @see VelocityConfig
|
||||
* @see VelocityConfigurer
|
||||
* @see #setUrl
|
||||
|
|
@ -516,11 +517,12 @@ public class VelocityView extends AbstractTemplateView {
|
|||
template.merge(context, response.getWriter());
|
||||
}
|
||||
catch (MethodInvocationException ex) {
|
||||
Throwable cause = ex.getWrappedThrowable();
|
||||
throw new NestedServletException(
|
||||
"Method invocation failed during rendering of Velocity view with name '" +
|
||||
getBeanName() + "': " + ex.getMessage() + "; reference [" + ex.getReferenceName() +
|
||||
"], method '" + ex.getMethodName() + "'",
|
||||
ex.getWrappedThrowable());
|
||||
cause==null ? ex : cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ import org.springframework.web.servlet.tags.TransformTag;
|
|||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
* @author Jeremy Grelle
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class SelectTagTests extends AbstractFormTagTests {
|
||||
|
||||
|
|
@ -172,7 +173,7 @@ public class SelectTagTests extends AbstractFormTagTests {
|
|||
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean");
|
||||
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
setValue(new Country(text, ""));
|
||||
setValue(Country.getCountryWithIsoCode(text));
|
||||
}
|
||||
public String getAsText() {
|
||||
return ((Country) getValue()).getName();
|
||||
|
|
@ -187,9 +188,122 @@ public class SelectTagTests extends AbstractFormTagTests {
|
|||
transformTag.setParent(this.tag);
|
||||
transformTag.setPageContext(getPageContext());
|
||||
transformTag.doStartTag();
|
||||
String output = getOutput();
|
||||
System.err.println(output);
|
||||
assertEquals("Austria", getPageContext().findAttribute("key"));
|
||||
}
|
||||
|
||||
public void testWithListAndEditor() throws Exception {
|
||||
this.tag.setPath("realCountry");
|
||||
this.tag.setItems("${countries}");
|
||||
this.tag.setItemValue("isoCode");
|
||||
this.tag.setItemLabel("name");
|
||||
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean");
|
||||
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
setValue(Country.getCountryWithIsoCode(text));
|
||||
}
|
||||
public String getAsText() {
|
||||
return ((Country) getValue()).getName();
|
||||
}
|
||||
});
|
||||
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
|
||||
this.tag.doStartTag();
|
||||
String output = getOutput();
|
||||
assertTrue(output.startsWith("<select "));
|
||||
assertTrue(output.endsWith("</select>"));
|
||||
assertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria"));
|
||||
}
|
||||
|
||||
public void testNestedPathWithListAndEditorAndNullValue() throws Exception {
|
||||
this.tag.setPath("bean.realCountry");
|
||||
this.tag.setItems("${countries}");
|
||||
this.tag.setItemValue("isoCode");
|
||||
this.tag.setItemLabel("name");
|
||||
this.tag.setMultiple("false");
|
||||
TestBeanWrapper testBean = new TestBeanWrapper();
|
||||
TestBeanWithRealCountry withCountry = (TestBeanWithRealCountry) getTestBean();
|
||||
withCountry.setRealCountry(null);
|
||||
testBean.setBean(withCountry);
|
||||
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean , "testBean");
|
||||
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
if (text==null || text.length()==0) {
|
||||
setValue(null);
|
||||
return;
|
||||
}
|
||||
setValue(Country.getCountryWithIsoCode(text));
|
||||
}
|
||||
public String getAsText() {
|
||||
Country value = (Country) getValue();
|
||||
if (value==null) {
|
||||
return null;
|
||||
}
|
||||
return ((Country) value).getName();
|
||||
}
|
||||
});
|
||||
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
|
||||
this.tag.doStartTag();
|
||||
String output = getOutput();
|
||||
System.err.println(output);
|
||||
assertTrue(output.startsWith("<select "));
|
||||
assertTrue(output.endsWith("</select>"));
|
||||
assertFalse(output.contains("selected=\"selected\""));
|
||||
}
|
||||
|
||||
public void testNestedPathWithListAndEditor() throws Exception {
|
||||
this.tag.setPath("bean.realCountry");
|
||||
this.tag.setItems("${countries}");
|
||||
this.tag.setItemValue("isoCode");
|
||||
this.tag.setItemLabel("name");
|
||||
TestBeanWrapper testBean = new TestBeanWrapper();
|
||||
testBean.setBean(getTestBean());
|
||||
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean , "testBean");
|
||||
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
setValue(Country.getCountryWithIsoCode(text));
|
||||
}
|
||||
public String getAsText() {
|
||||
return ((Country) getValue()).getName();
|
||||
}
|
||||
});
|
||||
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
|
||||
this.tag.doStartTag();
|
||||
String output = getOutput();
|
||||
assertTrue(output.startsWith("<select "));
|
||||
assertTrue(output.endsWith("</select>"));
|
||||
assertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria"));
|
||||
}
|
||||
|
||||
public void testWithListAndEditorAndNullValue() throws Exception {
|
||||
this.tag.setPath("realCountry");
|
||||
this.tag.setItems("${countries}");
|
||||
this.tag.setItemValue("isoCode");
|
||||
this.tag.setItemLabel("name");
|
||||
TestBeanWithRealCountry testBean = (TestBeanWithRealCountry) getTestBean();
|
||||
testBean.setRealCountry(null);
|
||||
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
|
||||
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
setValue(Country.getCountryWithIsoCode(text));
|
||||
}
|
||||
public String getAsText() {
|
||||
Country value = (Country) getValue();
|
||||
if (value==null) {
|
||||
return "";
|
||||
}
|
||||
return ((Country) value).getName();
|
||||
}
|
||||
});
|
||||
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
|
||||
this.tag.doStartTag();
|
||||
String output = getOutput();
|
||||
System.err.println(output);
|
||||
assertTrue(output.startsWith("<select "));
|
||||
assertTrue(output.endsWith("</select>"));
|
||||
assertFalse(output.contains("selected=\"selected\""));
|
||||
}
|
||||
|
||||
public void testWithMap() throws Exception {
|
||||
this.tag.setPath("sex");
|
||||
this.tag.setItems("${sexes}");
|
||||
|
|
@ -638,4 +752,17 @@ public class SelectTagTests extends AbstractFormTagTests {
|
|||
return (TestBean) getPageContext().getRequest().getAttribute(COMMAND_NAME);
|
||||
}
|
||||
|
||||
public static class TestBeanWrapper {
|
||||
private TestBean bean;
|
||||
|
||||
public TestBean getBean() {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public void setBean(TestBean bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ public class TestVelocityEngine extends VelocityEngine {
|
|||
}
|
||||
|
||||
|
||||
public Template getTemplate(String name) {
|
||||
public Template getTemplate(String name) throws ResourceNotFoundException {
|
||||
Template template = (Template) this.templates.get(name);
|
||||
if (template == null) {
|
||||
throw new ResourceNotFoundException("No template registered for name [" + name + "]");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
* Copyright 2002-2007 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
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.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.internal.matchers.TypeSafeMatcher;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @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() {
|
||||
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());
|
||||
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
package org.springframework.web.servlet.view.velocity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
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.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
* @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() {
|
||||
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() {
|
||||
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);
|
||||
@SuppressWarnings("unchecked")
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,142 @@
|
|||
package org.springframework.web.servlet.view.velocity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class VelocityViewResolverTests {
|
||||
|
||||
@Test
|
||||
public void testVelocityViewResolver() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
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() {
|
||||
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() {
|
||||
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() {
|
||||
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!
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -16,44 +16,46 @@
|
|||
|
||||
package org.springframework.web.servlet.view.velocity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
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 junit.framework.TestCase;
|
||||
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.apache.velocity.tools.view.context.ChainedContext;
|
||||
import org.apache.velocity.tools.view.tools.LinkTool;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.web.servlet.view.AbstractView;
|
||||
import org.springframework.web.servlet.view.InternalResourceView;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class VelocityViewTests extends TestCase {
|
||||
public class VelocityViewTests {
|
||||
|
||||
@Test
|
||||
public void testNoVelocityConfig() throws Exception {
|
||||
VelocityView vv = new VelocityView();
|
||||
MockControl wmc = MockControl.createControl(WebApplicationContext.class);
|
||||
|
|
@ -77,6 +79,7 @@ public class VelocityViewTests extends TestCase {
|
|||
wmc.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoTemplateName() throws Exception {
|
||||
VelocityView vv = new VelocityView();
|
||||
try {
|
||||
|
|
@ -89,22 +92,31 @@ public class VelocityViewTests extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@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
|
||||
|
|
@ -167,6 +179,7 @@ public class VelocityViewTests extends TestCase {
|
|||
wmc.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeepExistingContentType() throws Exception {
|
||||
final String templateName = "test.vm";
|
||||
|
||||
|
|
@ -213,6 +226,7 @@ public class VelocityViewTests extends TestCase {
|
|||
assertEquals("myContentType", expectedResponse.getContentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExposeHelpers() throws Exception {
|
||||
final String templateName = "test.vm";
|
||||
|
||||
|
|
@ -283,167 +297,4 @@ public class VelocityViewTests extends TestCase {
|
|||
assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType());
|
||||
}
|
||||
|
||||
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() {
|
||||
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() {
|
||||
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(), expectedRequest, expectedResponse);
|
||||
}
|
||||
|
||||
public void testVelocityViewResolver() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
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());
|
||||
}
|
||||
|
||||
public void testVelocityViewResolverWithToolbox() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
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());
|
||||
}
|
||||
|
||||
public void testVelocityViewResolverWithToolboxSubclass() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
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());
|
||||
}
|
||||
|
||||
public void testVelocityLayoutViewResolver() throws Exception {
|
||||
VelocityConfig vc = new VelocityConfig() {
|
||||
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!
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
##
|
||||
## test template for Velocity view with missing method
|
||||
##
|
||||
$command.nonexistent()
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
##
|
||||
## test template for Velocity view with exception
|
||||
##
|
||||
$command.unreliableFileOperation()
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
##
|
||||
## test template for Velocity view
|
||||
##
|
||||
|
||||
NAME
|
||||
$command.name
|
||||
|
|
@ -21,4 +21,11 @@ public class NestedServletExceptionTests {
|
|||
assertEquals(cause, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedServletExceptionStringNullThrowable() {
|
||||
// This can happen if someone is sloppy with Throwable causes...
|
||||
NestedServletException exception = new NestedServletException("foo", null);
|
||||
assertEquals("foo", exception.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ public class NestedServletException extends ServletException {
|
|||
super(msg, cause);
|
||||
// Set JDK 1.4 exception chain cause if not done by ServletException class already
|
||||
// (this differs between Servlet API versions).
|
||||
if (getCause() == null) {
|
||||
if (getCause() == null && cause!=null) {
|
||||
initCause(cause);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue