FreeMarkerView and ResourceBundle/XmlViewResolver can be initialized with ServletContext only (e.g. for testing)
This commit is contained in:
parent
40b06b5f19
commit
88e336cf56
|
|
@ -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.
|
||||
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.web.servlet.view;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
|
@ -28,6 +27,7 @@ import java.util.ResourceBundle;
|
|||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
|
@ -61,7 +61,8 @@ import org.springframework.web.servlet.View;
|
|||
* @see java.util.PropertyResourceBundle
|
||||
* @see UrlBasedViewResolver
|
||||
*/
|
||||
public class ResourceBundleViewResolver extends AbstractCachingViewResolver implements Ordered, DisposableBean {
|
||||
public class ResourceBundleViewResolver extends AbstractCachingViewResolver
|
||||
implements Ordered, InitializingBean, DisposableBean {
|
||||
|
||||
/** The default basename if no other basename is supplied. */
|
||||
public final static String DEFAULT_BASENAME = "views";
|
||||
|
|
@ -175,9 +176,11 @@ public class ResourceBundleViewResolver extends AbstractCachingViewResolver impl
|
|||
this.localesToInitialize = localesToInitialize;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void initApplicationContext() throws BeansException {
|
||||
/**
|
||||
* Eagerly initialize Locales if necessary.
|
||||
* @see #setLocalesToInitialize
|
||||
*/
|
||||
public void afterPropertiesSet() throws BeansException {
|
||||
if (this.localesToInitialize != null) {
|
||||
for (Locale locale : this.localesToInitialize) {
|
||||
initFactory(locale);
|
||||
|
|
@ -185,6 +188,7 @@ public class ResourceBundleViewResolver extends AbstractCachingViewResolver impl
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected View loadView(String viewName, Locale locale) throws Exception {
|
||||
BeanFactory factory = initFactory(locale);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2006 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.
|
||||
|
|
@ -21,6 +21,7 @@ import java.util.Locale;
|
|||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.xml.ResourceEntityResolver;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
|
|
@ -50,7 +51,8 @@ import org.springframework.web.servlet.View;
|
|||
* @see ResourceBundleViewResolver
|
||||
* @see UrlBasedViewResolver
|
||||
*/
|
||||
public class XmlViewResolver extends AbstractCachingViewResolver implements Ordered, DisposableBean {
|
||||
public class XmlViewResolver extends AbstractCachingViewResolver
|
||||
implements Ordered, InitializingBean, DisposableBean {
|
||||
|
||||
/** Default if no other location is supplied */
|
||||
public final static String DEFAULT_LOCATION = "/WEB-INF/views.xml";
|
||||
|
|
@ -84,8 +86,7 @@ public class XmlViewResolver extends AbstractCachingViewResolver implements Orde
|
|||
* Pre-initialize the factory from the XML file.
|
||||
* Only effective if caching is enabled.
|
||||
*/
|
||||
@Override
|
||||
protected void initApplicationContext() throws BeansException {
|
||||
public void afterPropertiesSet() throws BeansException {
|
||||
if (isCache()) {
|
||||
initFactory();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -138,11 +138,9 @@ public class FreeMarkerView extends AbstractTemplateView {
|
|||
* @see freemarker.cache.TemplateCache#getTemplate
|
||||
*/
|
||||
@Override
|
||||
protected void initApplicationContext() throws BeansException {
|
||||
super.initApplicationContext();
|
||||
|
||||
protected void initServletContext(ServletContext servletContext) throws BeansException {
|
||||
if (getConfiguration() != null) {
|
||||
this.taglibFactory = new TaglibFactory(getServletContext());
|
||||
this.taglibFactory = new TaglibFactory(servletContext);
|
||||
}
|
||||
else {
|
||||
FreeMarkerConfig config = autodetectConfiguration();
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -16,13 +16,10 @@
|
|||
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
|
@ -33,7 +30,9 @@ import javax.servlet.http.HttpServletResponse;
|
|||
import javax.servlet.jsp.jstl.core.Config;
|
||||
import javax.servlet.jsp.jstl.fmt.LocalizationContext;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.TestBean;
|
||||
|
|
@ -440,6 +439,7 @@ public class ViewResolverTests {
|
|||
XmlViewResolver vr = new XmlViewResolver();
|
||||
try {
|
||||
vr.setApplicationContext(wac);
|
||||
vr.afterPropertiesSet();
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
|
|
|
|||
|
|
@ -22,14 +22,14 @@ import java.io.Writer;
|
|||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
|
@ -44,9 +44,10 @@ import org.springframework.web.servlet.view.AbstractView;
|
|||
* @author Juergen Hoeller
|
||||
* @since 14.03.2004
|
||||
*/
|
||||
public class FreeMarkerViewTests extends TestCase {
|
||||
public class FreeMarkerViewTests {
|
||||
|
||||
public void testNoFreemarkerConfig() {
|
||||
@Test
|
||||
public void testNoFreemarkerConfig() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
MockControl wmc = MockControl.createControl(WebApplicationContext.class);
|
||||
|
|
@ -55,12 +56,15 @@ public class FreeMarkerViewTests extends TestCase {
|
|||
wmc.setReturnValue(new HashMap());
|
||||
wac.getParentBeanFactory();
|
||||
wmc.setReturnValue(null);
|
||||
wac.getServletContext();
|
||||
wmc.setReturnValue(new MockServletContext());
|
||||
wmc.replay();
|
||||
|
||||
fv.setUrl("anythingButNull");
|
||||
try {
|
||||
fv.setApplicationContext(wac);
|
||||
fail();
|
||||
fv.afterPropertiesSet();
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
// Check there's a helpful error message
|
||||
|
|
@ -70,6 +74,7 @@ public class FreeMarkerViewTests extends TestCase {
|
|||
wmc.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoTemplateName() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
try {
|
||||
|
|
@ -82,6 +87,7 @@ public class FreeMarkerViewTests extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidTemplateName() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
|
|
@ -118,6 +124,7 @@ public class FreeMarkerViewTests extends TestCase {
|
|||
assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeepExistingContentType() throws Exception {
|
||||
FreeMarkerView fv = new FreeMarkerView();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue