diff --git a/org.springframework.web.servlet/pom.xml b/org.springframework.web.servlet/pom.xml index ece6c7c9cbf..29e7639e8ef 100644 --- a/org.springframework.web.servlet/pom.xml +++ b/org.springframework.web.servlet/pom.xml @@ -126,7 +126,13 @@ 1.1.2 true - + + velocity velocity 1.5 diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/velocity/VelocityView.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/velocity/VelocityView.java index 6699f514bb4..3637c8799f1 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/velocity/VelocityView.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/velocity/VelocityView.java @@ -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); } } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java index 46713c612c9..69df3655264 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java @@ -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("")); + 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("")); + assertFalse(output.contains("selected=\"selected\"")); + } + public void testWithMap() throws Exception { this.tag.setPath("sex"); this.tag.setItems("${sexes}"); @@ -637,5 +751,18 @@ public class SelectTagTests extends AbstractFormTagTests { private TestBean getTestBean() { 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; + } + + } } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/TestVelocityEngine.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/TestVelocityEngine.java index 98179384cdf..401c914230a 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/TestVelocityEngine.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/TestVelocityEngine.java @@ -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 + "]"); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityRenderTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityRenderTests.java new file mode 100644 index 00000000000..6072a9b58b4 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityRenderTests.java @@ -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 model = new HashMap(); + 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() { + @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.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 model = new HashMap(); + 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() { + @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.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 model = new HashMap(); + model.put("command", new TestBean("juergen", 99)); + view.render(model, request, response); + + } + +} diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityToolboxViewTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityToolboxViewTests.java new file mode 100644 index 00000000000..4161a6f155e --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityToolboxViewTests.java @@ -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 toolAttributes = new HashMap(); + 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); + } + +} diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewResolverTests.java new file mode 100644 index 00000000000..d8a43ebf481 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewResolverTests.java @@ -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! + } + +} diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java index e8269bc4a43..105e0e54b56 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java @@ -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 toolAttributes = new HashMap(); - 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! - } - } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/error.vm b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/error.vm new file mode 100644 index 00000000000..4942e9097c1 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/error.vm @@ -0,0 +1,4 @@ +## +## test template for Velocity view with missing method +## +$command.nonexistent() diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/ioerror.vm b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/ioerror.vm new file mode 100644 index 00000000000..21779d13155 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/ioerror.vm @@ -0,0 +1,4 @@ +## +## test template for Velocity view with exception +## +$command.unreliableFileOperation() diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/simple.vm b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/simple.vm new file mode 100644 index 00000000000..de1e3043610 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/simple.vm @@ -0,0 +1,6 @@ +## +## test template for Velocity view +## + +NAME +$command.name diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/util/NestedServletExceptionTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/util/NestedServletExceptionTests.java index 9fb644ad7aa..9f2acd57d47 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/util/NestedServletExceptionTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/util/NestedServletExceptionTests.java @@ -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()); + } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java b/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java index 31b1eb02c03..d19f1331e07 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java +++ b/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java @@ -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); } }