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