38 changed files with 65 additions and 1305 deletions
@ -1,296 +0,0 @@
@@ -1,296 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2020 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 |
||||
* |
||||
* https://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; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
import java.util.MissingResourceException; |
||||
import java.util.ResourceBundle; |
||||
|
||||
import org.jspecify.annotations.Nullable; |
||||
|
||||
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.context.ConfigurableApplicationContext; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.web.context.support.GenericWebApplicationContext; |
||||
import org.springframework.web.servlet.View; |
||||
|
||||
/** |
||||
* A {@link org.springframework.web.servlet.ViewResolver} implementation that uses |
||||
* bean definitions in a {@link ResourceBundle}, specified by the bundle basename. |
||||
* |
||||
* <p>The bundle is typically defined in a properties file, located in the classpath. |
||||
* The default bundle basename is "views". |
||||
* |
||||
* <p>This {@code ViewResolver} supports localized view definitions, using the |
||||
* default support of {@link java.util.PropertyResourceBundle}. For example, the |
||||
* basename "views" will be resolved as class path resources "views_de_AT.properties", |
||||
* "views_de.properties", "views.properties" - for a given Locale "de_AT". |
||||
* |
||||
* <p>Note: This {@code ViewResolver} implements the {@link Ordered} interface
|
||||
* in order to allow for flexible participation in {@code ViewResolver} chaining. |
||||
* For example, some special views could be defined via this {@code ViewResolver} |
||||
* (giving it 0 as "order" value), while all remaining views could be resolved by |
||||
* a {@link UrlBasedViewResolver}. |
||||
* |
||||
* @author Rod Johnson |
||||
* @author Juergen Hoeller |
||||
* @see java.util.ResourceBundle#getBundle |
||||
* @see java.util.PropertyResourceBundle |
||||
* @see UrlBasedViewResolver |
||||
* @see BeanNameViewResolver |
||||
* @deprecated as of 5.3, in favor of Spring's common view resolver variants |
||||
* and/or custom resolver implementations |
||||
*/ |
||||
@Deprecated |
||||
public class ResourceBundleViewResolver extends AbstractCachingViewResolver |
||||
implements Ordered, InitializingBean, DisposableBean { |
||||
|
||||
/** The default basename if no other basename is supplied. */ |
||||
public static final String DEFAULT_BASENAME = "views"; |
||||
|
||||
|
||||
private String[] basenames = new String[] {DEFAULT_BASENAME}; |
||||
|
||||
private ClassLoader bundleClassLoader = Thread.currentThread().getContextClassLoader(); |
||||
|
||||
private @Nullable String defaultParentView; |
||||
|
||||
private Locale @Nullable [] localesToInitialize; |
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
|
||||
|
||||
/* Locale -> BeanFactory */ |
||||
private final Map<Locale, BeanFactory> localeCache = new HashMap<>(); |
||||
|
||||
/* List of ResourceBundle -> BeanFactory */ |
||||
private final Map<List<ResourceBundle>, ConfigurableApplicationContext> bundleCache = new HashMap<>(); |
||||
|
||||
|
||||
/** |
||||
* Set a single basename, following {@link java.util.ResourceBundle} conventions. |
||||
* The default is "views". |
||||
* <p>{@code ResourceBundle} supports different locale suffixes. For example, |
||||
* a base name of "views" might map to {@code ResourceBundle} files |
||||
* "views", "views_en_au" and "views_de". |
||||
* <p>Note that ResourceBundle names are effectively classpath locations: As a |
||||
* consequence, the JDK's standard ResourceBundle treats dots as package separators. |
||||
* This means that "test.theme" is effectively equivalent to "test/theme", |
||||
* just like it is for programmatic {@code java.util.ResourceBundle} usage. |
||||
* @see #setBasenames |
||||
* @see ResourceBundle#getBundle(String) |
||||
* @see ResourceBundle#getBundle(String, Locale) |
||||
*/ |
||||
public void setBasename(String basename) { |
||||
setBasenames(basename); |
||||
} |
||||
|
||||
/** |
||||
* Set an array of basenames, each following {@link java.util.ResourceBundle} |
||||
* conventions. The default is a single basename "views". |
||||
* <p>{@code ResourceBundle} supports different locale suffixes. For example, |
||||
* a base name of "views" might map to {@code ResourceBundle} files |
||||
* "views", "views_en_au" and "views_de". |
||||
* <p>The associated resource bundles will be checked sequentially when resolving |
||||
* a message code. Note that message definitions in a <i>previous</i> resource |
||||
* bundle will override ones in a later bundle, due to the sequential lookup. |
||||
* <p>Note that ResourceBundle names are effectively classpath locations: As a |
||||
* consequence, the JDK's standard ResourceBundle treats dots as package separators. |
||||
* This means that "test.theme" is effectively equivalent to "test/theme", |
||||
* just like it is for programmatic {@code java.util.ResourceBundle} usage. |
||||
* @see #setBasename |
||||
* @see ResourceBundle#getBundle(String) |
||||
* @see ResourceBundle#getBundle(String, Locale) |
||||
*/ |
||||
public void setBasenames(String... basenames) { |
||||
this.basenames = basenames; |
||||
} |
||||
|
||||
/** |
||||
* Set the {@link ClassLoader} to load resource bundles with. |
||||
* Default is the thread context {@code ClassLoader}. |
||||
*/ |
||||
public void setBundleClassLoader(ClassLoader classLoader) { |
||||
this.bundleClassLoader = classLoader; |
||||
} |
||||
|
||||
/** |
||||
* Return the {@link ClassLoader} to load resource bundles with. |
||||
* <p>Default is the specified bundle {@code ClassLoader}, |
||||
* usually the thread context {@code ClassLoader}. |
||||
*/ |
||||
protected ClassLoader getBundleClassLoader() { |
||||
return this.bundleClassLoader; |
||||
} |
||||
|
||||
/** |
||||
* Set the default parent for views defined in the {@code ResourceBundle}. |
||||
* <p>This avoids repeated "yyy1.(parent)=xxx", "yyy2.(parent)=xxx" definitions |
||||
* in the bundle, especially if all defined views share the same parent. |
||||
* <p>The parent will typically define the view class and common attributes. |
||||
* Concrete views might simply consist of a URL definition then: |
||||
* a la "yyy1.url=/my.jsp", "yyy2.url=/your.jsp". |
||||
* <p>View definitions that define their own parent or carry their own |
||||
* class can still override this. Strictly speaking, the rule that a |
||||
* default parent setting does not apply to a bean definition that |
||||
* carries a class is there for backwards compatibility reasons. |
||||
* It still matches the typical use case. |
||||
*/ |
||||
public void setDefaultParentView(String defaultParentView) { |
||||
this.defaultParentView = defaultParentView; |
||||
} |
||||
|
||||
/** |
||||
* Specify Locales to initialize eagerly, rather than lazily when actually accessed. |
||||
* <p>Allows for pre-initialization of common Locales, eagerly checking |
||||
* the view configuration for those Locales. |
||||
*/ |
||||
public void setLocalesToInitialize(Locale... localesToInitialize) { |
||||
this.localesToInitialize = localesToInitialize; |
||||
} |
||||
|
||||
/** |
||||
* Specify the order value for this ViewResolver bean. |
||||
* <p>The default value is {@code Ordered.LOWEST_PRECEDENCE}, meaning non-ordered. |
||||
* @see org.springframework.core.Ordered#getOrder() |
||||
*/ |
||||
public void setOrder(int order) { |
||||
this.order = order; |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return this.order; |
||||
} |
||||
|
||||
/** |
||||
* Eagerly initialize Locales if necessary. |
||||
* @see #setLocalesToInitialize |
||||
*/ |
||||
@Override |
||||
public void afterPropertiesSet() throws BeansException { |
||||
if (this.localesToInitialize != null) { |
||||
for (Locale locale : this.localesToInitialize) { |
||||
initFactory(locale); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected @Nullable View loadView(String viewName, Locale locale) throws Exception { |
||||
BeanFactory factory = initFactory(locale); |
||||
try { |
||||
return factory.getBean(viewName, View.class); |
||||
} |
||||
catch (NoSuchBeanDefinitionException ex) { |
||||
// Allow for ViewResolver chaining...
|
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Initialize the View {@link BeanFactory} from the {@code ResourceBundle}, |
||||
* for the given {@link Locale locale}. |
||||
* <p>Synchronized because of access by parallel threads. |
||||
* @param locale the target {@code Locale} |
||||
* @return the View factory for the given Locale |
||||
* @throws BeansException in case of initialization errors |
||||
*/ |
||||
protected synchronized BeanFactory initFactory(Locale locale) throws BeansException { |
||||
// Try to find cached factory for Locale:
|
||||
// Have we already encountered that Locale before?
|
||||
if (isCache()) { |
||||
BeanFactory cachedFactory = this.localeCache.get(locale); |
||||
if (cachedFactory != null) { |
||||
return cachedFactory; |
||||
} |
||||
} |
||||
|
||||
// Build list of ResourceBundle references for Locale.
|
||||
List<ResourceBundle> bundles = new ArrayList<>(this.basenames.length); |
||||
for (String basename : this.basenames) { |
||||
bundles.add(getBundle(basename, locale)); |
||||
} |
||||
|
||||
// Try to find cached factory for ResourceBundle list:
|
||||
// even if Locale was different, same bundles might have been found.
|
||||
if (isCache()) { |
||||
BeanFactory cachedFactory = this.bundleCache.get(bundles); |
||||
if (cachedFactory != null) { |
||||
this.localeCache.put(locale, cachedFactory); |
||||
return cachedFactory; |
||||
} |
||||
} |
||||
|
||||
// Create child ApplicationContext for views.
|
||||
GenericWebApplicationContext factory = new GenericWebApplicationContext(); |
||||
factory.setParent(getApplicationContext()); |
||||
factory.setServletContext(getServletContext()); |
||||
|
||||
// Load bean definitions from resource bundle.
|
||||
org.springframework.beans.factory.support.PropertiesBeanDefinitionReader reader = |
||||
new org.springframework.beans.factory.support.PropertiesBeanDefinitionReader(factory); |
||||
reader.setDefaultParentBean(this.defaultParentView); |
||||
for (ResourceBundle bundle : bundles) { |
||||
reader.registerBeanDefinitions(bundle); |
||||
} |
||||
|
||||
factory.refresh(); |
||||
|
||||
// Cache factory for both Locale and ResourceBundle list.
|
||||
if (isCache()) { |
||||
this.localeCache.put(locale, factory); |
||||
this.bundleCache.put(bundles, factory); |
||||
} |
||||
|
||||
return factory; |
||||
} |
||||
|
||||
/** |
||||
* Obtain the resource bundle for the given basename and {@link Locale}. |
||||
* @param basename the basename to look for |
||||
* @param locale the {@code Locale} to look for |
||||
* @return the corresponding {@code ResourceBundle} |
||||
* @throws MissingResourceException if no matching bundle could be found |
||||
* @see ResourceBundle#getBundle(String, Locale, ClassLoader) |
||||
*/ |
||||
protected ResourceBundle getBundle(String basename, Locale locale) throws MissingResourceException { |
||||
return ResourceBundle.getBundle(basename, locale, getBundleClassLoader()); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Close the bundle View factories on context shutdown. |
||||
*/ |
||||
@Override |
||||
public void destroy() throws BeansException { |
||||
for (ConfigurableApplicationContext factory : this.bundleCache.values()) { |
||||
factory.close(); |
||||
} |
||||
this.localeCache.clear(); |
||||
this.bundleCache.clear(); |
||||
} |
||||
|
||||
} |
||||
@ -1,179 +0,0 @@
@@ -1,179 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2020 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 |
||||
* |
||||
* https://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; |
||||
|
||||
import java.util.Locale; |
||||
|
||||
import org.jspecify.annotations.Nullable; |
||||
|
||||
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; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.web.context.support.GenericWebApplicationContext; |
||||
import org.springframework.web.servlet.View; |
||||
|
||||
/** |
||||
* A {@link org.springframework.web.servlet.ViewResolver} implementation that uses |
||||
* bean definitions in a dedicated XML file for view definitions, specified by |
||||
* resource location. The file will typically be located in the WEB-INF directory; |
||||
* the default is "/WEB-INF/views.xml". |
||||
* |
||||
* <p>This {@code ViewResolver} does not support internationalization at the level |
||||
* of its definition resources. Consider {@link ResourceBundleViewResolver} if you |
||||
* need to apply different view resources per locale. |
||||
* |
||||
* <p>Note: This {@code ViewResolver} implements the {@link Ordered} interface
|
||||
* in order to allow for flexible participation in {@code ViewResolver} chaining. |
||||
* For example, some special views could be defined via this {@code ViewResolver} |
||||
* (giving it 0 as "order" value), while all remaining views could be resolved by |
||||
* a {@link UrlBasedViewResolver}. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 18.06.2003 |
||||
* @see org.springframework.context.ApplicationContext#getResource |
||||
* @see UrlBasedViewResolver |
||||
* @see BeanNameViewResolver |
||||
* @deprecated as of 5.3, in favor of Spring's common view resolver variants |
||||
* and/or custom resolver implementations |
||||
*/ |
||||
@Deprecated |
||||
public class XmlViewResolver extends AbstractCachingViewResolver |
||||
implements Ordered, InitializingBean, DisposableBean { |
||||
|
||||
/** Default if no other location is supplied. */ |
||||
public static final String DEFAULT_LOCATION = "/WEB-INF/views.xml"; |
||||
|
||||
|
||||
private @Nullable Resource location; |
||||
|
||||
private @Nullable ConfigurableApplicationContext cachedFactory; |
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
|
||||
|
||||
|
||||
/** |
||||
* Set the location of the XML file that defines the view beans. |
||||
* <p>The default is "/WEB-INF/views.xml". |
||||
* @param location the location of the XML file. |
||||
*/ |
||||
public void setLocation(Resource location) { |
||||
this.location = location; |
||||
} |
||||
|
||||
/** |
||||
* Specify the order value for this ViewResolver bean. |
||||
* <p>The default value is {@code Ordered.LOWEST_PRECEDENCE}, meaning non-ordered. |
||||
* @see org.springframework.core.Ordered#getOrder() |
||||
*/ |
||||
public void setOrder(int order) { |
||||
this.order = order; |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return this.order; |
||||
} |
||||
|
||||
/** |
||||
* Pre-initialize the factory from the XML file. |
||||
* Only effective if caching is enabled. |
||||
*/ |
||||
@Override |
||||
public void afterPropertiesSet() throws BeansException { |
||||
if (isCache()) { |
||||
initFactory(); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* This implementation returns just the view name, |
||||
* as XmlViewResolver doesn't support localized resolution. |
||||
*/ |
||||
@Override |
||||
protected Object getCacheKey(String viewName, Locale locale) { |
||||
return viewName; |
||||
} |
||||
|
||||
@Override |
||||
protected @Nullable View loadView(String viewName, Locale locale) throws BeansException { |
||||
BeanFactory factory = initFactory(); |
||||
try { |
||||
return factory.getBean(viewName, View.class); |
||||
} |
||||
catch (NoSuchBeanDefinitionException ex) { |
||||
// Allow for ViewResolver chaining...
|
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Initialize the view bean factory from the XML file. |
||||
* Synchronized because of access by parallel threads. |
||||
* @throws BeansException in case of initialization errors |
||||
*/ |
||||
protected synchronized BeanFactory initFactory() throws BeansException { |
||||
if (this.cachedFactory != null) { |
||||
return this.cachedFactory; |
||||
} |
||||
|
||||
ApplicationContext applicationContext = obtainApplicationContext(); |
||||
|
||||
Resource actualLocation = this.location; |
||||
if (actualLocation == null) { |
||||
actualLocation = applicationContext.getResource(DEFAULT_LOCATION); |
||||
} |
||||
|
||||
// Create child ApplicationContext for views.
|
||||
GenericWebApplicationContext factory = new GenericWebApplicationContext(); |
||||
factory.setParent(applicationContext); |
||||
factory.setServletContext(getServletContext()); |
||||
|
||||
// Load XML resource with context-aware entity resolver.
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); |
||||
reader.setEnvironment(applicationContext.getEnvironment()); |
||||
reader.setEntityResolver(new ResourceEntityResolver(applicationContext)); |
||||
reader.loadBeanDefinitions(actualLocation); |
||||
|
||||
factory.refresh(); |
||||
|
||||
if (isCache()) { |
||||
this.cachedFactory = factory; |
||||
} |
||||
return factory; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Close the view bean factory on context shutdown. |
||||
*/ |
||||
@Override |
||||
public void destroy() throws BeansException { |
||||
if (this.cachedFactory != null) { |
||||
this.cachedFactory.close(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,29 +0,0 @@
@@ -1,29 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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; |
||||
|
||||
/** |
||||
* @author Rod Johnson |
||||
*/ |
||||
class ResourceBundleViewResolverNoCacheTests extends ResourceBundleViewResolverTests { |
||||
|
||||
@Override |
||||
protected boolean getCache() { |
||||
return false; |
||||
} |
||||
|
||||
} |
||||
@ -1,177 +0,0 @@
@@ -1,177 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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; |
||||
|
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
import java.util.MissingResourceException; |
||||
|
||||
import jakarta.servlet.http.HttpServletRequest; |
||||
import jakarta.servlet.http.HttpServletResponse; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.BeanIsAbstractException; |
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.web.context.support.ServletContextResource; |
||||
import org.springframework.web.context.support.StaticWebApplicationContext; |
||||
import org.springframework.web.servlet.View; |
||||
import org.springframework.web.testfixture.servlet.MockServletContext; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue; |
||||
|
||||
/** |
||||
* @author Rod Johnson |
||||
* @author Juergen Hoeller |
||||
* @author Sam Brannen |
||||
*/ |
||||
@SuppressWarnings("deprecation") |
||||
public class ResourceBundleViewResolverTests { |
||||
|
||||
/** Comes from this package */ |
||||
private static final String PROPS_FILE = "org.springframework.web.servlet.view.testviews"; |
||||
|
||||
private final ResourceBundleViewResolver rb = new ResourceBundleViewResolver(); |
||||
|
||||
private final StaticWebApplicationContext wac = new StaticWebApplicationContext(); |
||||
|
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
rb.setBasename(PROPS_FILE); |
||||
rb.setCache(getCache()); |
||||
rb.setDefaultParentView("testParent"); |
||||
|
||||
wac.setServletContext(new MockServletContext()); |
||||
wac.refresh(); |
||||
|
||||
// This will be propagated to views, so we need it.
|
||||
rb.setApplicationContext(wac); |
||||
} |
||||
|
||||
/** |
||||
* Not a constant: allows overrides. |
||||
* Controls whether to cache views. |
||||
*/ |
||||
protected boolean getCache() { |
||||
return true; |
||||
} |
||||
|
||||
|
||||
@Test |
||||
void parentsAreAbstract() { |
||||
assertThatExceptionOfType(BeanIsAbstractException.class).isThrownBy(() -> |
||||
rb.resolveViewName("debug.Parent", Locale.ENGLISH)); |
||||
assertThatExceptionOfType(BeanIsAbstractException.class).isThrownBy(() -> |
||||
rb.resolveViewName("testParent", Locale.ENGLISH)); |
||||
} |
||||
|
||||
@Test |
||||
void debugViewEnglish() throws Exception { |
||||
View v = rb.resolveViewName("debugView", Locale.ENGLISH); |
||||
assertThat(v).isInstanceOf(InternalResourceView.class); |
||||
InternalResourceView jv = (InternalResourceView) v; |
||||
assertThat(jv.getUrl()).as("debugView must have correct URL").isEqualTo("jsp/debug/debug.jsp"); |
||||
|
||||
Map<String, Object> m = jv.getStaticAttributes(); |
||||
assertThat(m.size()).as("Must have 2 static attributes").isEqualTo(2); |
||||
assertThat(m.get("foo")).as("attribute foo").isEqualTo("bar"); |
||||
assertThat(m.get("postcode")).as("attribute postcode").isEqualTo("SE10 9JY"); |
||||
|
||||
assertThat(jv.getContentType()).as("Correct default content type").isEqualTo(AbstractView.DEFAULT_CONTENT_TYPE); |
||||
} |
||||
|
||||
@Test |
||||
void debugViewFrench() throws Exception { |
||||
View v = rb.resolveViewName("debugView", Locale.FRENCH); |
||||
assertThat(v).isInstanceOf(InternalResourceView.class); |
||||
InternalResourceView jv = (InternalResourceView) v; |
||||
assertThat(jv.getUrl()).as("French debugView must have correct URL").isEqualTo("jsp/debug/deboug.jsp"); |
||||
assertThat(jv.getContentType()).as("Correct overridden (XML) content type").isEqualTo("text/xml;charset=ISO-8859-1"); |
||||
} |
||||
|
||||
@Test |
||||
void eagerInitialization() throws Exception { |
||||
ResourceBundleViewResolver rb = new ResourceBundleViewResolver(); |
||||
rb.setBasename(PROPS_FILE); |
||||
rb.setCache(getCache()); |
||||
rb.setDefaultParentView("testParent"); |
||||
rb.setLocalesToInitialize(Locale.ENGLISH, Locale.FRENCH); |
||||
rb.setApplicationContext(wac); |
||||
|
||||
View v = rb.resolveViewName("debugView", Locale.FRENCH); |
||||
assertThat(v).isInstanceOf(InternalResourceView.class); |
||||
InternalResourceView jv = (InternalResourceView) v; |
||||
assertThat(jv.getUrl()).as("French debugView must have correct URL").isEqualTo("jsp/debug/deboug.jsp"); |
||||
assertThat(jv.getContentType()).as("Correct overridden (XML) content type").isEqualTo("text/xml;charset=ISO-8859-1"); |
||||
} |
||||
|
||||
@Test |
||||
void sameBundleOnlyCachedOnce() throws Exception { |
||||
assumeTrue(rb.isCache()); |
||||
|
||||
View v1 = rb.resolveViewName("debugView", Locale.ENGLISH); |
||||
View v2 = rb.resolveViewName("debugView", Locale.UK); |
||||
assertThat(v2).isSameAs(v1); |
||||
} |
||||
|
||||
@Test |
||||
void noSuchViewEnglish() throws Exception { |
||||
assertThat(rb.resolveViewName("xxxxxxweorqiwuopeir", Locale.ENGLISH)).isNull(); |
||||
} |
||||
|
||||
@Test |
||||
void onSetContextCalledOnce() throws Exception { |
||||
TestView tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH); |
||||
tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH); |
||||
tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH); |
||||
assertThat(tv.getBeanName()).as("test has correct name").isEqualTo("test"); |
||||
assertThat(tv.initCount).as("test should have been initialized once, not ").isEqualTo(1); |
||||
} |
||||
|
||||
@Test |
||||
void noSuchBasename() { |
||||
rb.setBasename("weoriwoierqupowiuer"); |
||||
assertThatExceptionOfType(MissingResourceException.class).isThrownBy(() -> |
||||
rb.resolveViewName("debugView", Locale.ENGLISH)); |
||||
} |
||||
|
||||
|
||||
static class TestView extends AbstractView { |
||||
|
||||
public int initCount; |
||||
|
||||
public void setLocation(Resource location) { |
||||
if (!(location instanceof ServletContextResource)) { |
||||
throw new IllegalArgumentException("Expecting ServletContextResource, not " + location.getClass().getName()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, |
||||
HttpServletResponse response) { |
||||
} |
||||
|
||||
@Override |
||||
protected void initApplicationContext() { |
||||
++initCount; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,16 +0,0 @@
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd"> |
||||
|
||||
<beans> |
||||
|
||||
<bean id="rob" class="org.springframework.beans.testfixture.beans.TestBean"> |
||||
<property name="name" value="dummy"/> |
||||
<property name="age" value="-1"/> |
||||
</bean> |
||||
|
||||
<bean id="rodProto" class="org.springframework.beans.testfixture.beans.TestBean" scope="prototype"> |
||||
<property name="name" value="dummy"/> |
||||
<property name="age" value="-1"/> |
||||
</bean> |
||||
|
||||
</beans> |
||||
Loading…
Reference in new issue