Velocity/FreeMarker/TilesViewResolver only return a view if the target resource exists now

This commit is contained in:
Juergen Hoeller
2009-05-13 14:22:03 +00:00
parent b0ed6d5685
commit 47992ea07c
16 changed files with 211 additions and 154 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.web.servlet.view.freemarker;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
@@ -26,7 +27,6 @@ import javax.servlet.http.HttpServletResponse;
import freemarker.ext.servlet.AllHttpScopesHashModel;
import freemarker.template.Configuration;
import freemarker.template.SimpleScalar;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.easymock.MockControl;
@@ -38,9 +38,13 @@ 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 Juergen Hoeller
@@ -49,7 +53,7 @@ import org.springframework.web.servlet.view.AbstractView;
public class FreeMarkerViewTests {
@Test
public void testNoFreemarkerConfig() throws Exception {
public void testNoFreeMarkerConfig() throws Exception {
FreeMarkerView fv = new FreeMarkerView();
MockControl wmc = MockControl.createControl(WebApplicationContext.class);
@@ -101,7 +105,7 @@ public class FreeMarkerViewTests {
Map configs = new HashMap();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
configs.put("freemarkerConfig", configurer);
configs.put("configurer", configurer);
wmc.setReturnValue(configs);
wac.getParentBeanFactory();
wmc.setReturnValue(null);
@@ -138,7 +142,7 @@ public class FreeMarkerViewTests {
Map configs = new HashMap();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
configs.put("freemarkerConfig", configurer);
configs.put("configurer", configurer);
wmc.setReturnValue(configs);
wac.getParentBeanFactory();
wmc.setReturnValue(null);
@@ -164,19 +168,54 @@ public class FreeMarkerViewTests {
assertEquals("myContentType", response.getContentType());
}
@Test
public void testFreeMarkerViewResolver() throws Exception {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.getBeanFactory().registerSingleton("configurer", configurer);
wac.refresh();
FreeMarkerViewResolver vr = new FreeMarkerViewResolver();
vr.setPrefix("prefix_");
vr.setSuffix("_suffix");
vr.setApplicationContext(wac);
View view = vr.resolveViewName("test", Locale.CANADA);
assertEquals("Correct view class", FreeMarkerView.class, view.getClass());
assertEquals("Correct URL", "prefix_test_suffix", ((FreeMarkerView) 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());
}
private class TestConfiguration extends Configuration {
public Template getTemplate(String name, final Locale locale) throws IOException {
assertEquals("templateName", name);
return new Template(name, new StringReader("test")) {
public void process(Object model, Writer writer) throws TemplateException, IOException {
assertEquals(Locale.US, locale);
assertTrue(model instanceof AllHttpScopesHashModel);
AllHttpScopesHashModel fmModel = (AllHttpScopesHashModel) model;
assertEquals("myvalue", fmModel.get("myattr").toString());
}
};
if (name.equals("templateName") || name.equals("prefix_test_suffix")) {
return new Template(name, new StringReader("test")) {
public void process(Object model, Writer writer) throws TemplateException, IOException {
assertEquals(Locale.US, locale);
assertTrue(model instanceof AllHttpScopesHashModel);
AllHttpScopesHashModel fmModel = (AllHttpScopesHashModel) model;
assertEquals("myvalue", fmModel.get("myattr").toString());
}
};
}
else {
throw new FileNotFoundException();
}
}
}

View File

@@ -21,6 +21,7 @@ import java.util.Map;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ResourceNotFoundException;
/**
* @author Juergen Hoeller
@@ -46,7 +47,7 @@ public class TestVelocityEngine extends VelocityEngine {
public Template getTemplate(String name) {
Template template = (Template) this.templates.get(name);
if (template == null) {
throw new IllegalStateException("No template registered for name [" + name + "]");
throw new ResourceNotFoundException("No template registered for name [" + name + "]");
}
return template;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -28,7 +28,6 @@ import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.MathTool;
import org.apache.velocity.tools.generic.NumberTool;
@@ -72,7 +71,7 @@ public class VelocityViewTests extends TestCase {
}
catch (ApplicationContextException ex) {
// Check there's a helpful error message
assertTrue(ex.getMessage().indexOf("VelocityConfig") != -1);
assertTrue(ex.getMessage().contains("VelocityConfig"));
}
wmc.verify();
@@ -90,61 +89,6 @@ public class VelocityViewTests extends TestCase {
}
}
public void testCannotResolveTemplateNameResourceNotFoundException() throws Exception {
testCannotResolveTemplateName(new ResourceNotFoundException(""));
}
public void testCannotResolveTemplateNameParseErrorException() throws Exception {
testCannotResolveTemplateName(new ParseErrorException(""));
}
public void testCannotResolveTemplateNameNonspecificException() throws Exception {
testCannotResolveTemplateName(new Exception(""));
}
/**
* Check for failure to lookup a template for a range of reasons.
*/
private void testCannotResolveTemplateName(final Exception templateLookupException) throws Exception {
final String templateName = "test.vm";
MockControl wmc = MockControl.createControl(WebApplicationContext.class);
WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
wac.getParentBeanFactory();
wmc.setReturnValue(null);
VelocityConfig vc = new VelocityConfig() {
public VelocityEngine getVelocityEngine() {
return new VelocityEngine() {
public Template getTemplate(String tn)
throws ResourceNotFoundException, ParseErrorException, Exception {
assertEquals(tn, templateName);
throw templateLookupException;
}
};
}
};
wac.getBeansOfType(VelocityConfig.class, true, false);
Map configurers = new HashMap();
configurers.put("velocityConfigurer", vc);
wmc.setReturnValue(configurers);
wmc.replay();
VelocityView vv = new VelocityView();
//vv.setExposeDateFormatter(false);
//vv.setExposeCurrencyFormatter(false);
vv.setUrl(templateName);
try {
vv.setApplicationContext(wac);
fail();
}
catch (ApplicationContextException ex) {
assertEquals(ex.getCause(), templateLookupException);
}
wmc.verify();
}
public void testMergeTemplateSucceeds() throws Exception {
testValidTemplateName(null);
}
@@ -397,6 +341,7 @@ public class VelocityViewTests extends TestCase {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("configurer", vc);
wac.refresh();
VelocityViewResolver vr = new VelocityViewResolver();
vr.setPrefix("prefix_");
@@ -407,6 +352,9 @@ public class VelocityViewTests extends TestCase {
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());
@@ -425,6 +373,7 @@ public class VelocityViewTests extends TestCase {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("configurer", vc);
wac.refresh();
String toolbox = "org/springframework/web/servlet/view/velocity/toolbox.xml";
@@ -452,6 +401,7 @@ public class VelocityViewTests extends TestCase {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("configurer", vc);
wac.refresh();
String toolbox = "org/springframework/web/servlet/view/velocity/toolbox.xml";
@@ -480,6 +430,7 @@ public class VelocityViewTests extends TestCase {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("configurer", vc);
wac.refresh();
VelocityLayoutViewResolver vr = new VelocityLayoutViewResolver();
vr.setPrefix("prefix_");