moving unit tests from .testsuite -> .web.servlet
This commit is contained in:
@@ -1,254 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.context;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.LifecycleBean;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.mock.web.MockServletConfig;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.SimpleWebApplicationContext;
|
||||
|
||||
/**
|
||||
* JUnit 3.8 based tests for {@link ContextLoader},
|
||||
* {@link ContextLoaderListener}, {@link ContextLoaderServlet}, and related
|
||||
* classes.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 12.08.2003
|
||||
*/
|
||||
public class ContextLoaderTests extends TestCase {
|
||||
|
||||
public void testContextLoaderListenerWithDefaultContext() throws Exception {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
|
||||
"/org/springframework/web/context/WEB-INF/applicationContext.xml "
|
||||
+ "/org/springframework/web/context/WEB-INF/context-addition.xml");
|
||||
ServletContextListener listener = new ContextLoaderListener();
|
||||
ServletContextEvent event = new ServletContextEvent(sc);
|
||||
listener.contextInitialized(event);
|
||||
WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
||||
assertTrue("Correct WebApplicationContext exposed in ServletContext",
|
||||
context instanceof XmlWebApplicationContext);
|
||||
LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
|
||||
assertTrue("Has father", context.containsBean("father"));
|
||||
assertTrue("Has rod", context.containsBean("rod"));
|
||||
assertTrue("Has kerry", context.containsBean("kerry"));
|
||||
assertTrue("Not destroyed", !lb.isDestroyed());
|
||||
assertFalse(context.containsBean("beans1.bean1"));
|
||||
assertFalse(context.containsBean("beans1.bean2"));
|
||||
listener.contextDestroyed(event);
|
||||
assertTrue("Destroyed", lb.isDestroyed());
|
||||
}
|
||||
|
||||
/**
|
||||
* Addresses the issues raised in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-4008"
|
||||
* target="_blank">SPR-4008</a>: <em>Supply an opportunity to customize
|
||||
* context before calling refresh in ContextLoaders</em>.
|
||||
*/
|
||||
public void testContextLoaderListenerWithCustomizedContextLoader() throws Exception {
|
||||
final StringBuffer buffer = new StringBuffer();
|
||||
final String expectedContents = "customizeContext() was called";
|
||||
final MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
|
||||
"/org/springframework/web/context/WEB-INF/applicationContext.xml");
|
||||
final ServletContextListener listener = new ContextLoaderListener() {
|
||||
protected ContextLoader createContextLoader() {
|
||||
return new ContextLoader() {
|
||||
protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
|
||||
assertNotNull("The ServletContext should not be null.", servletContext);
|
||||
assertEquals("Verifying that we received the expected ServletContext.", sc, servletContext);
|
||||
assertFalse("The ApplicationContext should not yet have been refreshed.", applicationContext.isActive());
|
||||
buffer.append(expectedContents);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
listener.contextInitialized(new ServletContextEvent(sc));
|
||||
assertEquals("customizeContext() should have been called.", expectedContents, buffer.toString());
|
||||
}
|
||||
|
||||
public void testContextLoaderWithDefaultContextAndParent() throws Exception {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
|
||||
"/org/springframework/web/context/WEB-INF/applicationContext.xml "
|
||||
+ "/org/springframework/web/context/WEB-INF/context-addition.xml");
|
||||
sc.addInitParameter(ContextLoader.LOCATOR_FACTORY_SELECTOR_PARAM,
|
||||
"classpath:org/springframework/beans/factory/access/ref1.xml");
|
||||
sc.addInitParameter(ContextLoader.LOCATOR_FACTORY_KEY_PARAM, "a.qualified.name.of.some.sort");
|
||||
ServletContextListener listener = new ContextLoaderListener();
|
||||
ServletContextEvent event = new ServletContextEvent(sc);
|
||||
listener.contextInitialized(event);
|
||||
WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
||||
assertTrue("Correct WebApplicationContext exposed in ServletContext",
|
||||
context instanceof XmlWebApplicationContext);
|
||||
LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
|
||||
assertTrue("Has father", context.containsBean("father"));
|
||||
assertTrue("Has rod", context.containsBean("rod"));
|
||||
assertTrue("Has kerry", context.containsBean("kerry"));
|
||||
assertTrue("Not destroyed", !lb.isDestroyed());
|
||||
assertTrue(context.containsBean("beans1.bean1"));
|
||||
assertTrue(context.isTypeMatch("beans1.bean1", org.springframework.beans.factory.access.TestBean.class));
|
||||
assertTrue(context.containsBean("beans1.bean2"));
|
||||
assertTrue(context.isTypeMatch("beans1.bean2", org.springframework.beans.factory.access.TestBean.class));
|
||||
listener.contextDestroyed(event);
|
||||
assertTrue("Destroyed", lb.isDestroyed());
|
||||
}
|
||||
|
||||
public void testContextLoaderWithCustomContext() throws Exception {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
|
||||
"org.springframework.web.servlet.SimpleWebApplicationContext");
|
||||
ServletContextListener listener = new ContextLoaderListener();
|
||||
ServletContextEvent event = new ServletContextEvent(sc);
|
||||
listener.contextInitialized(event);
|
||||
WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
||||
assertTrue("Correct WebApplicationContext exposed in ServletContext", wc instanceof SimpleWebApplicationContext);
|
||||
}
|
||||
|
||||
public void testContextLoaderWithInvalidLocation() throws Exception {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
|
||||
ServletContextListener listener = new ContextLoaderListener();
|
||||
ServletContextEvent event = new ServletContextEvent(sc);
|
||||
try {
|
||||
listener.contextInitialized(event);
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof FileNotFoundException);
|
||||
}
|
||||
}
|
||||
|
||||
public void testContextLoaderWithInvalidContext() throws Exception {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
|
||||
"org.springframework.web.context.support.InvalidWebApplicationContext");
|
||||
ServletContextListener listener = new ContextLoaderListener();
|
||||
ServletContextEvent event = new ServletContextEvent(sc);
|
||||
try {
|
||||
listener.contextInitialized(event);
|
||||
fail("Should have thrown ApplicationContextException");
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof ClassNotFoundException);
|
||||
}
|
||||
}
|
||||
|
||||
public void testContextLoaderWithDefaultLocation() throws Exception {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
ServletContextListener listener = new ContextLoaderListener();
|
||||
ServletContextEvent event = new ServletContextEvent(sc);
|
||||
try {
|
||||
listener.contextInitialized(event);
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof IOException);
|
||||
assertTrue(ex.getCause().getMessage().indexOf("/WEB-INF/applicationContext.xml") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFrameworkServletWithDefaultLocation() throws Exception {
|
||||
DispatcherServlet servlet = new DispatcherServlet();
|
||||
servlet.setContextClass(XmlWebApplicationContext.class);
|
||||
try {
|
||||
servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof IOException);
|
||||
assertTrue(ex.getCause().getMessage().indexOf("/WEB-INF/test-servlet.xml") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFrameworkServletWithCustomLocation() throws Exception {
|
||||
DispatcherServlet servlet = new DispatcherServlet();
|
||||
servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml "
|
||||
+ "/org/springframework/web/context/WEB-INF/context-addition.xml");
|
||||
servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
|
||||
assertTrue(servlet.getWebApplicationContext().containsBean("kerry"));
|
||||
assertTrue(servlet.getWebApplicationContext().containsBean("kerryX"));
|
||||
}
|
||||
|
||||
public void testClassPathXmlApplicationContext() throws IOException {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"/org/springframework/web/context/WEB-INF/applicationContext.xml");
|
||||
assertTrue("Has father", context.containsBean("father"));
|
||||
assertTrue("Has rod", context.containsBean("rod"));
|
||||
assertFalse("Hasn't kerry", context.containsBean("kerry"));
|
||||
assertTrue("Doesn't have spouse", ((TestBean) context.getBean("rod")).getSpouse() == null);
|
||||
assertTrue("myinit not evaluated", "Roderick".equals(((TestBean) context.getBean("rod")).getName()));
|
||||
|
||||
context = new ClassPathXmlApplicationContext(new String[] {
|
||||
"/org/springframework/web/context/WEB-INF/applicationContext.xml",
|
||||
"/org/springframework/web/context/WEB-INF/context-addition.xml" });
|
||||
assertTrue("Has father", context.containsBean("father"));
|
||||
assertTrue("Has rod", context.containsBean("rod"));
|
||||
assertTrue("Has kerry", context.containsBean("kerry"));
|
||||
}
|
||||
|
||||
public void testSingletonDestructionOnStartupFailure() throws IOException {
|
||||
try {
|
||||
new ClassPathXmlApplicationContext(new String[] {
|
||||
"/org/springframework/web/context/WEB-INF/applicationContext.xml",
|
||||
"/org/springframework/web/context/WEB-INF/fail.xml" }) {
|
||||
|
||||
public void refresh() throws BeansException {
|
||||
try {
|
||||
super.refresh();
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) getBeanFactory();
|
||||
assertEquals(0, factory.getSingletonCount());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
};
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,279 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.context;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.context.AbstractApplicationContextTests;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
import org.springframework.context.support.AbstractMessageSource;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.ui.context.Theme;
|
||||
import org.springframework.ui.context.ThemeSource;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
import org.springframework.web.servlet.theme.AbstractThemeResolver;
|
||||
|
||||
/**
|
||||
* Creates a WebApplicationContext that points to a "web.xml" file that
|
||||
* contains the entry for what file to use for the applicationContext
|
||||
* (file "org/springframework/web/context/WEB-INF/applicationContext.xml").
|
||||
* That file then has an entry for a bean called "messageSource".
|
||||
* Whatever the basename property is set to for this bean is what the name of
|
||||
* a properties file in the classpath must be (in our case the name is
|
||||
* "messages" - note no package names).
|
||||
* Thus the catalog filename will be in the root of where the classes are compiled
|
||||
* to and will be called "messages_XX_YY.properties" where "XX" and "YY" are the
|
||||
* language and country codes known by the ResourceBundle class.
|
||||
*
|
||||
* <p>NOTE: The main method of this class is the "createWebApplicationContext(...)" method,
|
||||
* and it was copied from org.springframework.web.context.XmlWebApplicationContextTests.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Jean-Pierre Pawlak
|
||||
*/
|
||||
public class ResourceBundleMessageSourceTests extends AbstractApplicationContextTests {
|
||||
|
||||
/**
|
||||
* We use ticket WAR root for file structure.
|
||||
* We don't attempt to read web.xml.
|
||||
*/
|
||||
public static final String WAR_ROOT = "/org/springframework/web/context";
|
||||
|
||||
private ConfigurableWebApplicationContext root;
|
||||
|
||||
private MessageSource themeMsgSource;
|
||||
|
||||
protected ConfigurableApplicationContext createContext() throws Exception {
|
||||
root = new XmlWebApplicationContext();
|
||||
MockServletContext sc = new MockServletContext();
|
||||
root.setServletContext(sc);
|
||||
root.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/applicationContext.xml"});
|
||||
root.refresh();
|
||||
|
||||
ConfigurableWebApplicationContext wac = new XmlWebApplicationContext();
|
||||
wac.setParent(root);
|
||||
wac.setServletContext(sc);
|
||||
wac.setNamespace("test-servlet");
|
||||
wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"});
|
||||
wac.refresh();
|
||||
|
||||
Theme theme = ((ThemeSource) wac).getTheme(AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME);
|
||||
assertNotNull(theme);
|
||||
assertTrue("Theme name has to be the default theme name", AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME.equals(theme.getName()));
|
||||
themeMsgSource = theme.getMessageSource();
|
||||
assertNotNull(themeMsgSource);
|
||||
return wac;
|
||||
}
|
||||
|
||||
public void testCount() {
|
||||
assertTrue("should have 14 beans, not " +
|
||||
this.applicationContext.getBeanDefinitionCount(),
|
||||
this.applicationContext.getBeanDefinitionCount() == 14);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden as we can't trust superclass method.
|
||||
* @see org.springframework.context.AbstractApplicationContextTests#testEvents()
|
||||
*/
|
||||
public void testEvents() throws Exception {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public void testRootMessageSourceWithUseCodeAsDefaultMessage() throws NoSuchMessageException {
|
||||
AbstractMessageSource messageSource = (AbstractMessageSource) root.getBean("messageSource");
|
||||
messageSource.setUseCodeAsDefaultMessage(true);
|
||||
|
||||
assertEquals("message1", applicationContext.getMessage("code1", null, Locale.getDefault()));
|
||||
assertEquals("message2", applicationContext.getMessage("code2", null, Locale.getDefault()));
|
||||
|
||||
try {
|
||||
applicationContext.getMessage("code0", null, Locale.getDefault());
|
||||
fail("looking for code0 should throw a NoSuchMessageException");
|
||||
}
|
||||
catch (NoSuchMessageException ex) {
|
||||
// that's how it should be
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.context.support.AbstractMessageSource for more details.
|
||||
* NOTE: Messages are contained within the "test/org/springframework/web/context/WEB-INF/messagesXXX.properties" files.
|
||||
*/
|
||||
public void testGetMessageWithDefaultPassedInAndFoundInMsgCatalog() {
|
||||
assertTrue("valid msg from resourcebundle with default msg passed in returned default msg. Expected msg from catalog.",
|
||||
getApplicationContext().getMessage("message.format.example2", null, "This is a default msg if not found in msg.cat.", Locale.US
|
||||
)
|
||||
.equals("This is a test message in the message catalog with no args."));
|
||||
// getApplicationContext().getTheme("theme").getMessageSource().getMessage()
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.context.support.AbstractMessageSource for more details.
|
||||
* NOTE: Messages are contained within the "test/org/springframework/web/context/WEB-INF/messagesXXX.properties" files.
|
||||
*/
|
||||
public void testGetMessageWithDefaultPassedInAndNotFoundInMsgCatalog() {
|
||||
assertTrue("bogus msg from resourcebundle with default msg passed in returned default msg",
|
||||
getApplicationContext().getMessage("bogus.message", null, "This is a default msg if not found in msg.cat.", Locale.UK
|
||||
)
|
||||
.equals("This is a default msg if not found in msg.cat."));
|
||||
}
|
||||
|
||||
/**
|
||||
* The underlying implementation uses a hashMap to cache messageFormats
|
||||
* once a message has been asked for. This test is an attempt to
|
||||
* make sure the cache is being used properly.
|
||||
* NOTE: Messages are contained within the "test/org/springframework/web/context/WEB-INF/messagesXXX.properties" files.
|
||||
* @see org.springframework.context.support.AbstractMessageSource for more details.
|
||||
*/
|
||||
public void testGetMessageWithMessageAlreadyLookedFor() throws Exception {
|
||||
Object[] arguments = {
|
||||
new Integer(7), new Date(System.currentTimeMillis()),
|
||||
"a disturbance in the Force"
|
||||
};
|
||||
|
||||
// The first time searching, we don't care about for this test
|
||||
getApplicationContext().getMessage("message.format.example1", arguments, Locale.US);
|
||||
|
||||
// Now msg better be as expected
|
||||
assertTrue("2nd search within MsgFormat cache returned expected message for Locale.US",
|
||||
getApplicationContext().getMessage("message.format.example1", arguments, Locale.US
|
||||
)
|
||||
.indexOf("there was \"a disturbance in the Force\" on planet 7.") != -1);
|
||||
|
||||
Object[] newArguments = {
|
||||
new Integer(8), new Date(System.currentTimeMillis()),
|
||||
"a disturbance in the Force"
|
||||
};
|
||||
|
||||
// Now msg better be as expected even with different args
|
||||
assertTrue("2nd search within MsgFormat cache with different args returned expected message for Locale.US",
|
||||
getApplicationContext().getMessage("message.format.example1", newArguments, Locale.US
|
||||
)
|
||||
.indexOf("there was \"a disturbance in the Force\" on planet 8.") != -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.context.support.AbstractMessageSource for more details.
|
||||
* NOTE: Messages are contained within the "test/org/springframework/web/context/WEB-INF/messagesXXX.properties" files.
|
||||
* Example taken from the javadocs for the java.text.MessageFormat class
|
||||
*/
|
||||
public void testGetMessageWithNoDefaultPassedInAndFoundInMsgCatalog() throws Exception {
|
||||
Object[] arguments = {
|
||||
new Integer(7), new Date(System.currentTimeMillis()),
|
||||
"a disturbance in the Force"
|
||||
};
|
||||
|
||||
/*
|
||||
Try with Locale.US
|
||||
Since the msg has a time value in it, we will use String.indexOf(...)
|
||||
to just look for a substring without the time. This is because it is
|
||||
possible that by the time we store a time variable in this method
|
||||
and the time the ResourceBundleMessageSource resolves the msg the
|
||||
minutes of the time might not be the same.
|
||||
*/
|
||||
assertTrue("msg from resourcebundle for Locale.US substituting args for placeholders is as expected",
|
||||
getApplicationContext().getMessage("message.format.example1", arguments, Locale.US
|
||||
)
|
||||
.indexOf("there was \"a disturbance in the Force\" on planet 7.") != -1);
|
||||
|
||||
// Try with Locale.UK
|
||||
assertTrue("msg from resourcebundle for Locale.UK substituting args for placeholders is as expected",
|
||||
getApplicationContext().getMessage("message.format.example1", arguments, Locale.UK
|
||||
)
|
||||
.indexOf("there was \"a disturbance in the Force\" on station number 7.") != -1);
|
||||
|
||||
// Try with Locale.US - different test msg that requires no args
|
||||
assertTrue("msg from resourcebundle that requires no args for Locale.US is as expected",
|
||||
getApplicationContext().getMessage("message.format.example2", null, Locale.US)
|
||||
.equals("This is a test message in the message catalog with no args."));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.context.support.AbstractMessageSource for more details.
|
||||
* NOTE: Messages are contained within the "test/org/springframework/web/context/WEB-INF/messagesXXX.properties" files.
|
||||
*/
|
||||
public void testGetMessageWithNoDefaultPassedInAndNotFoundInMsgCatalog() {
|
||||
// Expecting an exception
|
||||
try {
|
||||
getApplicationContext().getMessage("bogus.message", null, Locale.UK);
|
||||
fail("bogus msg from resourcebundle without default msg should have thrown exception");
|
||||
}
|
||||
catch (NoSuchMessageException tExcept) {
|
||||
assertTrue("bogus msg from resourcebundle without default msg threw expected exception",
|
||||
true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetMultipleBasenamesForMessageSource() throws NoSuchMessageException {
|
||||
assertEquals("message1", getApplicationContext().getMessage("code1", null, Locale.UK));
|
||||
assertEquals("message2", getApplicationContext().getMessage("code2", null, Locale.UK));
|
||||
assertEquals("message3", getApplicationContext().getMessage("code3", null, Locale.UK));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.context.support.AbstractMessageSource for more details.
|
||||
* NOTE: Messages are contained within the "test/org/springframework/web/context/WEB-INF/themeXXX.properties" files.
|
||||
*/
|
||||
public void testGetMessageWithDefaultPassedInAndFoundInThemeCatalog() {
|
||||
// Try with Locale.US
|
||||
String msg = getThemeMessage("theme.example1", null, "This is a default theme msg if not found in theme cat.", Locale.US);
|
||||
assertTrue("valid msg from theme resourcebundle with default msg passed in returned default msg. Expected msg from catalog. Received: " + msg,
|
||||
msg.equals("This is a test message in the theme message catalog."));
|
||||
// Try with Locale.UK
|
||||
msg = getThemeMessage("theme.example1", null, "This is a default theme msg if not found in theme cat.", Locale.UK);
|
||||
assertTrue("valid msg from theme resourcebundle with default msg passed in returned default msg. Expected msg from catalog.",
|
||||
msg.equals("This is a test message in the theme message catalog with no args."));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.context.support.AbstractMessageSource for more details.
|
||||
* NOTE: Messages are contained within the "test/org/springframework/web/context/WEB-INF/themeXXX.properties" files.
|
||||
*/
|
||||
public void testGetMessageWithDefaultPassedInAndNotFoundInThemeCatalog() {
|
||||
assertTrue("bogus msg from theme resourcebundle with default msg passed in returned default msg",
|
||||
getThemeMessage("bogus.message", null, "This is a default msg if not found in theme cat.", Locale.UK
|
||||
)
|
||||
.equals("This is a default msg if not found in theme cat."));
|
||||
}
|
||||
|
||||
public void testThemeSourceNesting() throws NoSuchMessageException {
|
||||
String overriddenMsg = getThemeMessage("theme.example2", null, null, Locale.UK);
|
||||
MessageSource ms = ((ThemeSource) root).getTheme(AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME).getMessageSource();
|
||||
String originalMsg = ms.getMessage("theme.example2", null, Locale.UK);
|
||||
assertTrue("correct overridden msg", "test-message2".equals(overriddenMsg));
|
||||
assertTrue("correct original msg", "message2".equals(originalMsg));
|
||||
}
|
||||
|
||||
public void testThemeSourceNestingWithParentDefault() throws NoSuchMessageException {
|
||||
StaticWebApplicationContext leaf = new StaticWebApplicationContext();
|
||||
leaf.setParent(getApplicationContext());
|
||||
leaf.refresh();
|
||||
assertNotNull("theme still found", leaf.getTheme("theme"));
|
||||
MessageSource ms = leaf.getTheme("theme").getMessageSource();
|
||||
String msg = ms.getMessage("theme.example2", null, null, Locale.UK);
|
||||
assertEquals("correct overridden msg", "test-message2", msg);
|
||||
}
|
||||
|
||||
private String getThemeMessage(String code, Object args[], String defaultMessage, Locale locale) {
|
||||
return themeMsgSource.getMessage(code, args, defaultMessage, locale);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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.context;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ServletConfigAwareBean implements ServletConfigAware {
|
||||
|
||||
private ServletConfig servletConfig;
|
||||
|
||||
public void setServletConfig(ServletConfig servletConfig) {
|
||||
this.servletConfig = servletConfig;
|
||||
}
|
||||
|
||||
public ServletConfig getServletConfig() {
|
||||
return servletConfig;
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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.context;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ServletContextAwareBean implements ServletContextAware {
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
public void setServletContext(ServletContext servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
public ServletContext getServletContext() {
|
||||
return servletContext;
|
||||
}
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
* 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.context;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.mock.web.MockServletConfig;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.ServletContextAwareProcessor;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ServletContextAwareProcessorTests extends TestCase {
|
||||
|
||||
public void testServletContextAwareWithServletContext() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext);
|
||||
ServletContextAwareBean bean = new ServletContextAwareBean();
|
||||
assertNull(bean.getServletContext());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletContext should have been set", bean.getServletContext());
|
||||
assertEquals(servletContext, bean.getServletContext());
|
||||
}
|
||||
|
||||
public void testServletContextAwareWithServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletConfig servletConfig = new MockServletConfig(servletContext);
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletConfig);
|
||||
ServletContextAwareBean bean = new ServletContextAwareBean();
|
||||
assertNull(bean.getServletContext());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletContext should have been set", bean.getServletContext());
|
||||
assertEquals(servletContext, bean.getServletContext());
|
||||
}
|
||||
|
||||
public void testServletContextAwareWithServletContextAndServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletConfig servletConfig = new MockServletConfig(servletContext);
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, servletConfig);
|
||||
ServletContextAwareBean bean = new ServletContextAwareBean();
|
||||
assertNull(bean.getServletContext());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletContext should have been set", bean.getServletContext());
|
||||
assertEquals(servletContext, bean.getServletContext());
|
||||
}
|
||||
|
||||
public void testServletContextAwareWithNullServletContextAndNonNullServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletConfig servletConfig = new MockServletConfig(servletContext);
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
|
||||
ServletContextAwareBean bean = new ServletContextAwareBean();
|
||||
assertNull(bean.getServletContext());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletContext should have been set", bean.getServletContext());
|
||||
assertEquals(servletContext, bean.getServletContext());
|
||||
}
|
||||
|
||||
public void testServletContextAwareWithNonNullServletContextAndNullServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, null);
|
||||
ServletContextAwareBean bean = new ServletContextAwareBean();
|
||||
assertNull(bean.getServletContext());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletContext should have been set", bean.getServletContext());
|
||||
assertEquals(servletContext, bean.getServletContext());
|
||||
}
|
||||
|
||||
public void testServletContextAwareWithNullServletContext() {
|
||||
ServletContext servletContext = null;
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext);
|
||||
ServletContextAwareBean bean = new ServletContextAwareBean();
|
||||
assertNull(bean.getServletContext());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNull(bean.getServletContext());
|
||||
}
|
||||
|
||||
public void testServletConfigAwareWithServletContextOnly() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext);
|
||||
ServletConfigAwareBean bean = new ServletConfigAwareBean();
|
||||
assertNull(bean.getServletConfig());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNull(bean.getServletConfig());
|
||||
}
|
||||
|
||||
public void testServletConfigAwareWithServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletConfig servletConfig = new MockServletConfig(servletContext);
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletConfig);
|
||||
ServletConfigAwareBean bean = new ServletConfigAwareBean();
|
||||
assertNull(bean.getServletConfig());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletConfig should have been set", bean.getServletConfig());
|
||||
assertEquals(servletConfig, bean.getServletConfig());
|
||||
}
|
||||
|
||||
public void testServletConfigAwareWithServletContextAndServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletConfig servletConfig = new MockServletConfig(servletContext);
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, servletConfig);
|
||||
ServletConfigAwareBean bean = new ServletConfigAwareBean();
|
||||
assertNull(bean.getServletConfig());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletConfig should have been set", bean.getServletConfig());
|
||||
assertEquals(servletConfig, bean.getServletConfig());
|
||||
}
|
||||
|
||||
public void testServletConfigAwareWithNullServletContextAndNonNullServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletConfig servletConfig = new MockServletConfig(servletContext);
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
|
||||
ServletConfigAwareBean bean = new ServletConfigAwareBean();
|
||||
assertNull(bean.getServletConfig());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNotNull("ServletConfig should have been set", bean.getServletConfig());
|
||||
assertEquals(servletConfig, bean.getServletConfig());
|
||||
}
|
||||
|
||||
public void testServletConfigAwareWithNonNullServletContextAndNullServletConfig() {
|
||||
ServletContext servletContext = new MockServletContext();
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, null);
|
||||
ServletConfigAwareBean bean = new ServletConfigAwareBean();
|
||||
assertNull(bean.getServletConfig());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNull(bean.getServletConfig());
|
||||
}
|
||||
|
||||
public void testServletConfigAwareWithNullServletContext() {
|
||||
ServletContext servletContext = null;
|
||||
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext);
|
||||
ServletConfigAwareBean bean = new ServletConfigAwareBean();
|
||||
assertNull(bean.getServletConfig());
|
||||
processor.postProcessBeforeInitialization(bean, "testBean");
|
||||
assertNull(bean.getServletConfig());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="aca" class="org.springframework.context.ACATester"/>
|
||||
|
||||
<bean id="aca-prototype" class="org.springframework.context.ACATester" scope="prototype"/>
|
||||
|
||||
<bean id="testListener" class="org.springframework.context.TestListener"/>
|
||||
|
||||
<bean id="roderick" parent="rod">
|
||||
<property name="name"><value>Roderick</value></property>
|
||||
<property name="age"><value>31</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="kathy" class="org.springframework.beans.TestBean" scope="prototype"/>
|
||||
|
||||
<bean id="kerry" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value>Kerry</value></property>
|
||||
<property name="age"><value>34</value></property>
|
||||
<property name="spouse"><ref bean="rod"/></property>
|
||||
</bean>
|
||||
|
||||
<bean id="typeMismatch" class="org.springframework.beans.TestBean" scope="prototype">
|
||||
<property name="name"><value>typeMismatch</value></property>
|
||||
<property name="age"><value>34x</value></property>
|
||||
<property name="spouse"><ref bean="rod"/></property>
|
||||
</bean>
|
||||
|
||||
<!-- Factory beans are automatically treated
|
||||
differently -->
|
||||
<bean id="singletonFactory"
|
||||
class="org.springframework.beans.factory.DummyFactory">
|
||||
</bean>
|
||||
|
||||
<bean id="prototypeFactory"
|
||||
class="org.springframework.beans.factory.DummyFactory">
|
||||
<property name="singleton"><value>false</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="listenerVeto" class="org.springframework.beans.TestBean">
|
||||
<!-- <listener property="age" beanRef="agistListener" /> -->
|
||||
<property name="name"><value>listenerVeto</value></property>
|
||||
<property name="age"><value>66</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="validEmpty" class="org.springframework.beans.TestBean"/>
|
||||
|
||||
</beans>
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
|
||||
|
||||
<bean id="servletContextAwareBean" class="org.springframework.web.context.ServletContextAwareBean"/>
|
||||
|
||||
<bean id="servletConfigAwareBean" class="org.springframework.web.context.ServletConfigAwareBean"/>
|
||||
|
||||
</beans>
|
||||
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="fail" class="java.lang.Integer"/>
|
||||
|
||||
</beans>
|
||||
@@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
|
||||
<property name="useCodeAsDefaultMessage">
|
||||
<value>${useCodeAsDefaultMessage}</value>
|
||||
</property>
|
||||
<property name="basenames">
|
||||
<list>
|
||||
<value>org/springframework/web/context/WEB-INF/${message-file}</value>
|
||||
<value>org/springframework/web/context/WEB-INF/more-context-messages</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="messageSourceString" factory-bean="messageSource" factory-method="toString"/>
|
||||
|
||||
<bean id="currentTimeMillis" class="javax.management.ObjectName" factory-method="getInstance">
|
||||
<constructor-arg value="${objectName}"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
|
||||
<property name="basenamePrefix">
|
||||
<value>${theme-base}</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="rob" class="org.springframework.beans.TestBean">
|
||||
<property name="name" value="dummy"/>
|
||||
<property name="age" value="-1"/>
|
||||
</bean>
|
||||
|
||||
<bean id="rodProto" class="org.springframework.beans.TestBean" scope="prototype">
|
||||
<property name="name" value="dummy"/>
|
||||
<property name="age" value="-1"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -1 +0,0 @@
|
||||
code2=message2
|
||||
@@ -1 +0,0 @@
|
||||
theme.example2=test-message2
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="rod" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value>Rod</value></property>
|
||||
<property name="age"><value>31</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="kerryX" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value>Kerry</value></property>
|
||||
<property name="age"><value>34</value></property>
|
||||
<property name="spouse"><ref local="rod"/></property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -1,2 +0,0 @@
|
||||
theme.example1=This is a test message in the theme message catalog with no args.
|
||||
theme.example2=message2
|
||||
@@ -1 +0,0 @@
|
||||
theme.example1=This is a test message in the theme message catalog.
|
||||
@@ -1,151 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
|
||||
<web-app>
|
||||
<display-name>ticket-webapp</display-name>
|
||||
<description>Web interface for ticket application</description>
|
||||
|
||||
<distributable/>
|
||||
|
||||
<!--
|
||||
<listener>
|
||||
<listener-class>com.wrox.j2eedd.ticket.web.SessionCleanupListener</listener-class>
|
||||
</listener>
|
||||
-->
|
||||
|
||||
|
||||
<!-- This servlet must be loaded first to configure the log4j
|
||||
system and create the WebApplicationContext
|
||||
-->
|
||||
<servlet>
|
||||
<servlet-name>config</servlet-name>
|
||||
<servlet-class>org.springframework.framework.web.context.ContextLoaderServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>org.springframework.framework.web.context.XMLWebApplicationContext</param-value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<param-name>log4jPropertiesUrl</param-name>
|
||||
<param-value>/WEB-INF/log4j_PRODUCTION.properties</param-value>
|
||||
</init-param>
|
||||
<!-- This is essential -->
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
|
||||
<servlet>
|
||||
<servlet-name>boxOffice</servlet-name>
|
||||
<servlet-class>org.springframework.framework.web.workflow.CommandServlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
|
||||
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>boxOffice</servlet-name>
|
||||
<url-pattern>/*.html</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>config</servlet-name>
|
||||
<url-pattern>/context/context.html</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
|
||||
<session-config>
|
||||
<session-timeout>60</session-timeout>
|
||||
</session-config>
|
||||
|
||||
<welcome-file-list>
|
||||
<!-- Simply doesn't work to a servlet -->
|
||||
<welcome-file>/welcome.jsp</welcome-file>
|
||||
</welcome-file-list>
|
||||
|
||||
<error-page>
|
||||
<error-code>403</error-code>
|
||||
<location>/jsp/layout/twocolumn/403-Forbidden.jsp</location>
|
||||
</error-page>
|
||||
|
||||
<error-page>
|
||||
<error-code>404</error-code>
|
||||
<location>/jsp/layout/twocolumn/404-NotFound.jsp</location>
|
||||
</error-page>
|
||||
|
||||
<error-page>
|
||||
<exception-type>java.lang.Throwable</exception-type>
|
||||
<location>/jsp/layout/twocolumn/uncaughtException.jsp</location>
|
||||
</error-page>
|
||||
|
||||
<taglib>
|
||||
<taglib-uri>/bind</taglib-uri>
|
||||
<taglib-location>/WEB-INF/tlds/b11.tld</taglib-location>
|
||||
</taglib>
|
||||
|
||||
<taglib>
|
||||
<taglib-uri>/events</taglib-uri>
|
||||
<taglib-location>/WEB-INF/tlds/event11.tld</taglib-location>
|
||||
</taglib>
|
||||
|
||||
<security-constraint>
|
||||
<web-resource-collection>
|
||||
<web-resource-name>SalesInfo
|
||||
</web-resource-name>
|
||||
<url-pattern>purchase.html</url-pattern>
|
||||
<http-method>GET</http-method>
|
||||
<http-method>POST</http-method>
|
||||
</web-resource-collection>
|
||||
<auth-constraint>
|
||||
<role-name>registeredUsers</role-name>
|
||||
</auth-constraint>
|
||||
<user-data-constraint>
|
||||
<transport-guarantee>NONE</transport-guarantee>
|
||||
</user-data-constraint>
|
||||
</security-constraint>
|
||||
|
||||
|
||||
<login-config>
|
||||
<auth-method>FORM</auth-method>
|
||||
|
||||
<!-- Used only for BASIC authentication: only included because ServletUnit needs it -->
|
||||
<realm-name>ticket</realm-name>
|
||||
|
||||
<form-login-config>
|
||||
<form-login-page>login.jsp</form-login-page>
|
||||
<form-error-page>loginError.jsp</form-error-page>
|
||||
</form-login-config>
|
||||
</login-config>
|
||||
|
||||
|
||||
<security-role>
|
||||
<role-name>registeredUsers</role-name>
|
||||
</security-role>
|
||||
|
||||
|
||||
|
||||
|
||||
<ejb-ref>
|
||||
<ejb-ref-name>ejb/FixtureManager</ejb-ref-name>
|
||||
<ejb-ref-type>Session</ejb-ref-type>
|
||||
<home>com.wrox.j2eedd.ticket.ejb.fixturemanager.FixtureManagerRemoteHome</home>
|
||||
<remote>com.wrox.j2eedd.ticket.ejb.fixturemanager.FixtureManagerRemote</remote>
|
||||
<ejb-link>FixtureManager</ejb-link>
|
||||
</ejb-ref>
|
||||
|
||||
|
||||
<ejb-ref>
|
||||
<ejb-ref-name>ejb/BoxOffice</ejb-ref-name>
|
||||
<ejb-ref-type>Session</ejb-ref-type>
|
||||
<home>com.wrox.j2eedd.ticket.ejb.booking.BoxOfficeRemoteHome</home>
|
||||
<remote>com.wrox.j2eedd.ticket.ejb.booking.BoxOfficeRemote</remote>
|
||||
<ejb-link>BookingManager</ejb-link>
|
||||
</ejb-ref>
|
||||
|
||||
<ejb-ref>
|
||||
<ejb-ref-name>ejb/CustomerManager</ejb-ref-name>
|
||||
<ejb-ref-type>Session</ejb-ref-type>
|
||||
<home>com.wrox.j2eedd.ticket.ejb.customer.CustomerManagerRemoteHoms</home>
|
||||
<remote>com.wrox.j2eedd.ticket.ejb.customer.CustomerManagerRemote</remote>
|
||||
<ejb-link>CustomerManager</ejb-link>
|
||||
</ejb-ref>
|
||||
|
||||
</web-app>
|
||||
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2006 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.context.support;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletConfig;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.0
|
||||
*/
|
||||
public class HttpRequestHandlerTests extends TestCase {
|
||||
|
||||
public void testHttpRequestHandlerServletPassThrough() throws Exception {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
final MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.getBeanFactory().registerSingleton("myHandler", new HttpRequestHandler() {
|
||||
public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
||||
assertSame(request, req);
|
||||
assertSame(response, res);
|
||||
String exception = request.getParameter("exception");
|
||||
if ("ServletException".equals(exception)) {
|
||||
throw new ServletException("test");
|
||||
}
|
||||
if ("IOException".equals(exception)) {
|
||||
throw new IOException("test");
|
||||
}
|
||||
res.getWriter().write("myResponse");
|
||||
}
|
||||
});
|
||||
wac.setServletContext(servletContext);
|
||||
wac.refresh();
|
||||
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
Servlet servlet = new HttpRequestHandlerServlet();
|
||||
servlet.init(new MockServletConfig(servletContext, "myHandler"));
|
||||
|
||||
servlet.service(request, response);
|
||||
assertEquals("myResponse", response.getContentAsString());
|
||||
|
||||
try {
|
||||
request.setParameter("exception", "ServletException");
|
||||
servlet.service(request, response);
|
||||
fail("Should have thrown ServletException");
|
||||
}
|
||||
catch (ServletException ex) {
|
||||
assertEquals("test", ex.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
request.setParameter("exception", "IOException");
|
||||
servlet.service(request, response);
|
||||
fail("Should have thrown IOException");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
assertEquals("test", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,474 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.context.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.ChildBeanDefinition;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.support.ManagedMap;
|
||||
import org.springframework.beans.factory.support.ManagedSet;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
||||
/**
|
||||
* Tests for various ServletContext-related support classes.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 22.12.2004
|
||||
*/
|
||||
public class ServletContextSupportTests extends TestCase {
|
||||
|
||||
public void testServletContextFactoryBean() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
wac.registerSingleton("servletContext", ServletContextFactoryBean.class, pvs);
|
||||
wac.refresh();
|
||||
|
||||
Object value = wac.getBean("servletContext");
|
||||
assertEquals(sc, value);
|
||||
}
|
||||
|
||||
public void testServletContextAttributeFactoryBean() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.setAttribute("myAttr", "myValue");
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("attributeName", "myAttr");
|
||||
wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
|
||||
wac.refresh();
|
||||
|
||||
Object value = wac.getBean("importedAttr");
|
||||
assertEquals("myValue", value);
|
||||
}
|
||||
|
||||
public void testServletContextAttributeFactoryBeanWithAttributeNotFound() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("attributeName", "myAttr");
|
||||
wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
|
||||
|
||||
try {
|
||||
wac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof IllegalStateException);
|
||||
assertTrue(ex.getCause().getMessage().indexOf("myAttr") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
public void testServletContextParameterFactoryBean() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.addInitParameter("myParam", "myValue");
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("initParamName", "myParam");
|
||||
wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
|
||||
wac.refresh();
|
||||
|
||||
Object value = wac.getBean("importedParam");
|
||||
assertEquals("myValue", value);
|
||||
}
|
||||
|
||||
public void testServletContextParameterFactoryBeanWithAttributeNotFound() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("initParamName", "myParam");
|
||||
wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
|
||||
|
||||
try {
|
||||
wac.refresh();
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof IllegalStateException);
|
||||
assertTrue(ex.getCause().getMessage().indexOf("myParam") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
public void testServletContextAttributeExporter() {
|
||||
TestBean tb = new TestBean();
|
||||
Map attributes = new HashMap();
|
||||
attributes.put("attr1", "value1");
|
||||
attributes.put("attr2", tb);
|
||||
|
||||
MockServletContext sc = new MockServletContext();
|
||||
ServletContextAttributeExporter exporter = new ServletContextAttributeExporter();
|
||||
exporter.setAttributes(attributes);
|
||||
exporter.setServletContext(sc);
|
||||
|
||||
assertEquals("value1", sc.getAttribute("attr1"));
|
||||
assertSame(tb, sc.getAttribute("attr2"));
|
||||
}
|
||||
|
||||
public void testServletContextPropertyPlaceholderConfigurer() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.addInitParameter("key4", "mykey4");
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("age", "${age}");
|
||||
pvs.addPropertyValue("name", "${key4}name${var}${var}${");
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
wac.registerSingleton("tb1", TestBean.class, pvs);
|
||||
|
||||
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
|
||||
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my");
|
||||
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
|
||||
|
||||
wac.refresh();
|
||||
|
||||
TestBean tb1 = (TestBean) wac.getBean("tb1");
|
||||
TestBean tb2 = (TestBean) wac.getBean("tb2");
|
||||
assertEquals(98, tb1.getAge());
|
||||
assertEquals("mykey4namemyvarmyvar${", tb1.getName());
|
||||
assertEquals(tb2, tb1.getSpouse());
|
||||
}
|
||||
|
||||
public void testServletContextPropertyPlaceholderConfigurerWithLocalOverriding() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.addInitParameter("key4", "mykey4");
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("age", "${age}");
|
||||
pvs.addPropertyValue("name", "${key4}name${var}${var}${");
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
wac.registerSingleton("tb1", TestBean.class, pvs);
|
||||
|
||||
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
|
||||
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
|
||||
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
|
||||
|
||||
wac.refresh();
|
||||
|
||||
TestBean tb1 = (TestBean) wac.getBean("tb1");
|
||||
TestBean tb2 = (TestBean) wac.getBean("tb2");
|
||||
assertEquals(98, tb1.getAge());
|
||||
assertEquals("yourkey4namemyvarmyvar${", tb1.getName());
|
||||
assertEquals(tb2, tb1.getSpouse());
|
||||
}
|
||||
|
||||
public void testServletContextPropertyPlaceholderConfigurerWithContextOverride() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.addInitParameter("key4", "mykey4");
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("age", "${age}");
|
||||
pvs.addPropertyValue("name", "${key4}name${var}${var}${");
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
wac.registerSingleton("tb1", TestBean.class, pvs);
|
||||
|
||||
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
|
||||
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
|
||||
pvs.addPropertyValue("contextOverride", Boolean.TRUE);
|
||||
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
|
||||
|
||||
wac.refresh();
|
||||
|
||||
TestBean tb1 = (TestBean) wac.getBean("tb1");
|
||||
TestBean tb2 = (TestBean) wac.getBean("tb2");
|
||||
assertEquals(98, tb1.getAge());
|
||||
assertEquals("mykey4namemyvarmyvar${", tb1.getName());
|
||||
assertEquals(tb2, tb1.getSpouse());
|
||||
}
|
||||
|
||||
public void testServletContextPropertyPlaceholderConfigurerWithContextOverrideAndAttributes() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.addInitParameter("key4", "mykey4");
|
||||
sc.setAttribute("key4", "attrkey4");
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("age", "${age}");
|
||||
pvs.addPropertyValue("name", "${key4}name${var}${var}${");
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
wac.registerSingleton("tb1", TestBean.class, pvs);
|
||||
|
||||
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
|
||||
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
|
||||
pvs.addPropertyValue("contextOverride", Boolean.TRUE);
|
||||
pvs.addPropertyValue("searchContextAttributes", Boolean.TRUE);
|
||||
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
|
||||
|
||||
wac.refresh();
|
||||
|
||||
TestBean tb1 = (TestBean) wac.getBean("tb1");
|
||||
TestBean tb2 = (TestBean) wac.getBean("tb2");
|
||||
assertEquals(98, tb1.getAge());
|
||||
assertEquals("attrkey4namemyvarmyvar${", tb1.getName());
|
||||
assertEquals(tb2, tb1.getSpouse());
|
||||
}
|
||||
|
||||
public void testServletContextPropertyPlaceholderConfigurerWithAttributes() {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.addInitParameter("key4", "mykey4");
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("age", "${age}");
|
||||
pvs.addPropertyValue("name", "name${var}${var}${");
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
wac.registerSingleton("tb1", TestBean.class, pvs);
|
||||
|
||||
ConstructorArgumentValues cas = new ConstructorArgumentValues();
|
||||
cas.addIndexedArgumentValue(1, "${age}");
|
||||
cas.addGenericArgumentValue("${var}name${age}");
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
List friends = new ManagedList();
|
||||
friends.add("na${age}me");
|
||||
friends.add(new RuntimeBeanReference("${ref}"));
|
||||
pvs.addPropertyValue("friends", friends);
|
||||
|
||||
Set someSet = new ManagedSet();
|
||||
someSet.add("na${age}me");
|
||||
someSet.add(new RuntimeBeanReference("${ref}"));
|
||||
pvs.addPropertyValue("someSet", someSet);
|
||||
|
||||
Map someMap = new ManagedMap();
|
||||
someMap.put("key1", new RuntimeBeanReference("${ref}"));
|
||||
someMap.put("key2", "${age}name");
|
||||
MutablePropertyValues innerPvs = new MutablePropertyValues();
|
||||
innerPvs.addPropertyValue("touchy", "${os.name}");
|
||||
someMap.put("key3", new RootBeanDefinition(TestBean.class, innerPvs));
|
||||
MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs);
|
||||
someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
|
||||
pvs.addPropertyValue("someMap", someMap);
|
||||
|
||||
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
|
||||
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("properties", "var=${m}var\nref=tb2\nm=my");
|
||||
pvs.addPropertyValue("searchContextAttributes", Boolean.TRUE);
|
||||
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
|
||||
sc.setAttribute("age", new Integer(98));
|
||||
|
||||
wac.refresh();
|
||||
|
||||
TestBean tb1 = (TestBean) wac.getBean("tb1");
|
||||
TestBean tb2 = (TestBean) wac.getBean("tb2");
|
||||
assertEquals(98, tb1.getAge());
|
||||
assertEquals(98, tb2.getAge());
|
||||
assertEquals("namemyvarmyvar${", tb1.getName());
|
||||
assertEquals("myvarname98", tb2.getName());
|
||||
assertEquals(tb2, tb1.getSpouse());
|
||||
assertEquals(2, tb2.getFriends().size());
|
||||
assertEquals("na98me", tb2.getFriends().iterator().next());
|
||||
assertEquals(tb2, tb2.getFriends().toArray()[1]);
|
||||
assertEquals(2, tb2.getSomeSet().size());
|
||||
assertTrue(tb2.getSomeSet().contains("na98me"));
|
||||
assertTrue(tb2.getSomeSet().contains(tb2));
|
||||
assertEquals(4, tb2.getSomeMap().size());
|
||||
assertEquals(tb2, tb2.getSomeMap().get("key1"));
|
||||
assertEquals("98name", tb2.getSomeMap().get("key2"));
|
||||
TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
|
||||
TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
|
||||
assertEquals(0, inner1.getAge());
|
||||
assertEquals(null, inner1.getName());
|
||||
assertEquals(System.getProperty("os.name"), inner1.getTouchy());
|
||||
assertEquals(98, inner2.getAge());
|
||||
assertEquals("namemyvarmyvar${", inner2.getName());
|
||||
assertEquals(System.getProperty("os.name"), inner2.getTouchy());
|
||||
}
|
||||
|
||||
public void testServletContextResourceLoader() {
|
||||
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context");
|
||||
ServletContextResourceLoader rl = new ServletContextResourceLoader(sc);
|
||||
assertTrue(rl.getResource("/WEB-INF/web.xml").exists());
|
||||
assertTrue(rl.getResource("WEB-INF/web.xml").exists());
|
||||
assertTrue(rl.getResource("../context/WEB-INF/web.xml").exists());
|
||||
assertTrue(rl.getResource("/../context/WEB-INF/web.xml").exists());
|
||||
}
|
||||
|
||||
public void testServletContextResourcePatternResolver() throws IOException {
|
||||
final Set paths = new HashSet();
|
||||
paths.add("/WEB-INF/context1.xml");
|
||||
paths.add("/WEB-INF/context2.xml");
|
||||
|
||||
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {
|
||||
public Set getResourcePaths(String path) {
|
||||
if ("/WEB-INF/".equals(path)) {
|
||||
return paths;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
|
||||
Resource[] found = rpr.getResources("/WEB-INF/*.xml");
|
||||
Set foundPaths = new HashSet();
|
||||
for (int i = 0; i < found.length; i++) {
|
||||
foundPaths.add(((ServletContextResource) found[i]).getPath());
|
||||
}
|
||||
assertEquals(2, foundPaths.size());
|
||||
assertTrue(foundPaths.contains("/WEB-INF/context1.xml"));
|
||||
assertTrue(foundPaths.contains("/WEB-INF/context2.xml"));
|
||||
}
|
||||
|
||||
public void testServletContextResourcePatternResolverWithPatternPath() throws IOException {
|
||||
final Set dirs = new HashSet();
|
||||
dirs.add("/WEB-INF/mydir1/");
|
||||
dirs.add("/WEB-INF/mydir2/");
|
||||
|
||||
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {
|
||||
public Set getResourcePaths(String path) {
|
||||
if ("/WEB-INF/".equals(path)) {
|
||||
return dirs;
|
||||
}
|
||||
if ("/WEB-INF/mydir1/".equals(path)) {
|
||||
return Collections.singleton("/WEB-INF/mydir1/context1.xml");
|
||||
}
|
||||
if ("/WEB-INF/mydir2/".equals(path)) {
|
||||
return Collections.singleton("/WEB-INF/mydir2/context2.xml");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
|
||||
Resource[] found = rpr.getResources("/WEB-INF/*/*.xml");
|
||||
Set foundPaths = new HashSet();
|
||||
for (int i = 0; i < found.length; i++) {
|
||||
foundPaths.add(((ServletContextResource) found[i]).getPath());
|
||||
}
|
||||
assertEquals(2, foundPaths.size());
|
||||
assertTrue(foundPaths.contains("/WEB-INF/mydir1/context1.xml"));
|
||||
assertTrue(foundPaths.contains("/WEB-INF/mydir2/context2.xml"));
|
||||
}
|
||||
|
||||
public void testServletContextResourcePatternResolverWithUnboundedPatternPath() throws IOException {
|
||||
final Set dirs = new HashSet();
|
||||
dirs.add("/WEB-INF/mydir1/");
|
||||
dirs.add("/WEB-INF/mydir2/");
|
||||
|
||||
final Set paths = new HashSet();
|
||||
paths.add("/WEB-INF/mydir2/context2.xml");
|
||||
paths.add("/WEB-INF/mydir2/mydir3/");
|
||||
|
||||
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {
|
||||
public Set getResourcePaths(String path) {
|
||||
if ("/WEB-INF/".equals(path)) {
|
||||
return dirs;
|
||||
}
|
||||
if ("/WEB-INF/mydir1/".equals(path)) {
|
||||
return Collections.singleton("/WEB-INF/mydir1/context1.xml");
|
||||
}
|
||||
if ("/WEB-INF/mydir2/".equals(path)) {
|
||||
return paths;
|
||||
}
|
||||
if ("/WEB-INF/mydir2/mydir3/".equals(path)) {
|
||||
return Collections.singleton("/WEB-INF/mydir2/mydir3/context3.xml");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
|
||||
Resource[] found = rpr.getResources("/WEB-INF/**/*.xml");
|
||||
Set foundPaths = new HashSet();
|
||||
for (int i = 0; i < found.length; i++) {
|
||||
foundPaths.add(((ServletContextResource) found[i]).getPath());
|
||||
}
|
||||
assertEquals(3, foundPaths.size());
|
||||
assertTrue(foundPaths.contains("/WEB-INF/mydir1/context1.xml"));
|
||||
assertTrue(foundPaths.contains("/WEB-INF/mydir2/context2.xml"));
|
||||
assertTrue(foundPaths.contains("/WEB-INF/mydir2/mydir3/context3.xml"));
|
||||
}
|
||||
|
||||
public void testServletContextResourcePatternResolverWithAbsolutePaths() throws IOException {
|
||||
final Set paths = new HashSet();
|
||||
paths.add("C:/webroot/WEB-INF/context1.xml");
|
||||
paths.add("C:/webroot/WEB-INF/context2.xml");
|
||||
paths.add("C:/webroot/someOtherDirThatDoesntContainPath");
|
||||
|
||||
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {
|
||||
public Set getResourcePaths(String path) {
|
||||
if ("/WEB-INF/".equals(path)) {
|
||||
return paths;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
|
||||
Resource[] found = rpr.getResources("/WEB-INF/*.xml");
|
||||
Set foundPaths = new HashSet();
|
||||
for (int i = 0; i < found.length; i++) {
|
||||
foundPaths.add(((ServletContextResource) found[i]).getPath());
|
||||
}
|
||||
assertEquals(2, foundPaths.size());
|
||||
assertTrue(foundPaths.contains("/WEB-INF/context1.xml"));
|
||||
assertTrue(foundPaths.contains("/WEB-INF/context2.xml"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.context.support;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 28.08.2003
|
||||
*/
|
||||
public class WebApplicationObjectSupportTests extends TestCase {
|
||||
|
||||
public void testWebApplicationObjectSupport() {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
File tempDir = new File("");
|
||||
wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, tempDir);
|
||||
wac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class));
|
||||
wac.refresh();
|
||||
WebApplicationObjectSupport wao = (WebApplicationObjectSupport) wac.getBean("test");
|
||||
assertEquals(wao.getServletContext(), wac.getServletContext());
|
||||
assertEquals(wao.getTempDir(), tempDir);
|
||||
}
|
||||
|
||||
public void testWebApplicationObjectSupportWithWrongContext() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
ac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class));
|
||||
WebApplicationObjectSupport wao = (WebApplicationObjectSupport) ac.getBean("test");
|
||||
try {
|
||||
wao.getWebApplicationContext();
|
||||
fail("Should have thrown IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TestWebApplicationObject extends WebApplicationObjectSupport {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.context;
|
||||
package org.springframework.web.portlet.context;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -32,13 +32,19 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
import org.springframework.context.TestListener;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
|
||||
/**
|
||||
* Should ideally be eliminated. Copied when splitting .testsuite up into individual bundles.
|
||||
*
|
||||
* @see org.springframework.web.context.XmlWebApplicationContextTests
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class XmlWebApplicationContextTests extends AbstractApplicationContextTests {
|
||||
public abstract class AbstractXmlWebApplicationContextTests extends AbstractApplicationContextTests {
|
||||
|
||||
private ConfigurableWebApplicationContext root;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
|
||||
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
|
||||
<!ENTITY contextInclude SYSTEM "org/springframework/web/portlet/context/WEB-INF/contextInclude.xml">
|
||||
]>
|
||||
|
||||
<beans>
|
||||
@@ -58,21 +58,21 @@
|
||||
|
||||
<bean id="myOverride" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
|
||||
<property name="location">
|
||||
<value>/org/springframework/web/context/WEB-INF/myoverride.properties</value>
|
||||
<value>/org/springframework/web/portlet/context/WEB-INF/myoverride.properties</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="myPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="locations">
|
||||
<list>
|
||||
<value>classpath:/org/springframework/web/context/WEB-INF/myplace*.properties</value>
|
||||
<value>classpath:/org/springframework/web/context/WEB-INF/myover*.properties</value>
|
||||
<value>classpath:/org/springframework/web/portlet/context/WEB-INF/myplace*.properties</value>
|
||||
<value>classpath:/org/springframework/web/portlet/context/WEB-INF/myover*.properties</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="init-and-ib"
|
||||
class="org.springframework.web.context.XmlWebApplicationContextTests$InitAndIB"
|
||||
class="org.springframework.web.portlet.context.AbstractXmlWebApplicationContextTests$InitAndIB"
|
||||
lazy-init="true"
|
||||
init-method="customInit"
|
||||
destroy-method="customDestroy"
|
||||
@@ -1,4 +1,4 @@
|
||||
useCodeAsDefaultMessage=false
|
||||
message-file=context-messages
|
||||
objectName=test:service=myservice
|
||||
theme-base=org/springframework/web/context/WEB-INF/
|
||||
theme-base=org/springframework/web/portlet/context/WEB-INF/
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<beans>
|
||||
|
||||
<import resource="classpath:/org/springframework/web/context/WEB-INF/test-servlet.xml"/>
|
||||
<import resource="classpath:/org/springframework/web/portlet/context/WEB-INF/test-servlet.xml"/>
|
||||
|
||||
<bean id="portletContextAwareBean" class="org.springframework.web.portlet.context.PortletContextAwareBean"/>
|
||||
|
||||
|
||||
@@ -30,14 +30,14 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
import org.springframework.mock.web.portlet.MockPortletConfig;
|
||||
import org.springframework.mock.web.portlet.MockPortletContext;
|
||||
import org.springframework.web.context.XmlWebApplicationContextTests;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Mark Fisher
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class XmlPortletApplicationContextTests extends XmlWebApplicationContextTests {
|
||||
public class XmlPortletApplicationContextTests extends AbstractXmlWebApplicationContextTests {
|
||||
|
||||
private ConfigurablePortletApplicationContext root;
|
||||
|
||||
@@ -46,7 +46,7 @@ public class XmlPortletApplicationContextTests extends XmlWebApplicationContextT
|
||||
PortletContext portletContext = new MockPortletContext();
|
||||
PortletConfig portletConfig = new MockPortletConfig(portletContext);
|
||||
root.setPortletConfig(portletConfig);
|
||||
root.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/applicationContext.xml"});
|
||||
root.setConfigLocations(new String[] {"/org/springframework/web/portlet/context/WEB-INF/applicationContext.xml"});
|
||||
root.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
|
||||
|
||||
@@ -1,493 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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;
|
||||
|
||||
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;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.jsp.jstl.core.Config;
|
||||
import javax.servlet.jsp.jstl.fmt.LocalizationContext;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockRequestDispatcher;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.ServletContextResource;
|
||||
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.i18n.FixedLocaleResolver;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.theme.FixedThemeResolver;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 18.06.2003
|
||||
*/
|
||||
public class ViewResolverTests extends TestCase {
|
||||
|
||||
public void testBeanNameViewResolver() throws ServletException {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
MutablePropertyValues pvs1 = new MutablePropertyValues();
|
||||
pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
|
||||
wac.registerSingleton("example1", InternalResourceView.class, pvs1);
|
||||
MutablePropertyValues pvs2 = new MutablePropertyValues();
|
||||
pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
|
||||
wac.registerSingleton("example2", JstlView.class, pvs2);
|
||||
BeanNameViewResolver vr = new BeanNameViewResolver();
|
||||
vr.setApplicationContext(wac);
|
||||
wac.refresh();
|
||||
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertEquals("Correct view class", InternalResourceView.class, view.getClass());
|
||||
assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl());
|
||||
|
||||
view = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
|
||||
}
|
||||
|
||||
public void testUrlBasedViewResolverWithNullViewClass() {
|
||||
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
|
||||
try {
|
||||
resolver.setViewClass(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testUrlBasedViewResolverWithoutPrefixes() throws Exception {
|
||||
UrlBasedViewResolver vr = new UrlBasedViewResolver();
|
||||
vr.setViewClass(JstlView.class);
|
||||
doTestUrlBasedViewResolverWithoutPrefixes(vr);
|
||||
}
|
||||
|
||||
public void testUrlBasedViewResolverWithPrefixes() throws Exception {
|
||||
UrlBasedViewResolver vr = new UrlBasedViewResolver();
|
||||
vr.setViewClass(JstlView.class);
|
||||
doTestUrlBasedViewResolverWithPrefixes(vr);
|
||||
}
|
||||
|
||||
public void testInternalResourceViewResolverWithoutPrefixes() throws Exception {
|
||||
doTestUrlBasedViewResolverWithoutPrefixes(new InternalResourceViewResolver());
|
||||
}
|
||||
|
||||
public void testInternalResourceViewResolverWithPrefixes() throws Exception {
|
||||
doTestUrlBasedViewResolverWithPrefixes(new InternalResourceViewResolver());
|
||||
}
|
||||
|
||||
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
vr.setApplicationContext(wac);
|
||||
vr.setContentType("myContentType");
|
||||
vr.setRequestContextAttribute("rc");
|
||||
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
|
||||
assertEquals("Correct contentType", "myContentType", ((InternalResourceView) view).getContentType());
|
||||
|
||||
view = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
|
||||
assertEquals("Correct contentType", "myContentType", ((InternalResourceView) view).getContentType());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
|
||||
Map model = new HashMap();
|
||||
TestBean tb = new TestBean();
|
||||
model.put("tb", tb);
|
||||
view.render(model, request, response);
|
||||
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
|
||||
assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext);
|
||||
|
||||
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 void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
vr.setPrefix("/WEB-INF/");
|
||||
vr.setSuffix(".jsp");
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "/WEB-INF/example1.jsp", ((InternalResourceView) view).getUrl());
|
||||
|
||||
view = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "/WEB-INF/example2.jsp", ((InternalResourceView) view).getUrl());
|
||||
|
||||
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 testInternalResourceViewResolverWithAttributes() throws Exception {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.refresh();
|
||||
InternalResourceViewResolver vr = new InternalResourceViewResolver();
|
||||
Properties props = new Properties();
|
||||
props.setProperty("key1", "value1");
|
||||
vr.setAttributes(props);
|
||||
Map map = new HashMap();
|
||||
map.put("key2", new Integer(2));
|
||||
vr.setAttributesMap(map);
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
|
||||
Map attributes = ((InternalResourceView) view).getStaticAttributes();
|
||||
assertEquals("value1", attributes.get("key1"));
|
||||
assertEquals(new Integer(2), attributes.get("key2"));
|
||||
|
||||
view = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
|
||||
attributes = ((InternalResourceView) view).getStaticAttributes();
|
||||
assertEquals("value1", attributes.get("key1"));
|
||||
assertEquals(new Integer(2), attributes.get("key2"));
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(sc);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
Map model = new HashMap();
|
||||
TestBean tb = new TestBean();
|
||||
model.put("tb", tb);
|
||||
view.render(model, request, response);
|
||||
|
||||
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
|
||||
assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
|
||||
assertEquals("value1", request.getAttribute("key1"));
|
||||
assertEquals(new Integer(2), request.getAttribute("key2"));
|
||||
}
|
||||
|
||||
public void testInternalResourceViewResolverWithContextBeans() throws Exception {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
final StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.registerSingleton("myBean", TestBean.class);
|
||||
wac.registerSingleton("myBean2", TestBean.class);
|
||||
wac.setServletContext(sc);
|
||||
wac.refresh();
|
||||
InternalResourceViewResolver vr = new InternalResourceViewResolver();
|
||||
Properties props = new Properties();
|
||||
props.setProperty("key1", "value1");
|
||||
vr.setAttributes(props);
|
||||
Map map = new HashMap();
|
||||
map.put("key2", new Integer(2));
|
||||
vr.setAttributesMap(map);
|
||||
vr.setExposeContextBeansAsAttributes(true);
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(sc) {
|
||||
public RequestDispatcher getRequestDispatcher(String path) {
|
||||
return new MockRequestDispatcher(path) {
|
||||
public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
|
||||
assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
|
||||
assertEquals("value1", forwardRequest.getAttribute("key1"));
|
||||
assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
|
||||
assertSame(wac.getBean("myBean"), forwardRequest.getAttribute("myBean"));
|
||||
assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
view.render(new HashMap(), request, response);
|
||||
}
|
||||
|
||||
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
final StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.registerSingleton("myBean", TestBean.class);
|
||||
wac.registerSingleton("myBean2", TestBean.class);
|
||||
wac.setServletContext(sc);
|
||||
wac.refresh();
|
||||
InternalResourceViewResolver vr = new InternalResourceViewResolver();
|
||||
Properties props = new Properties();
|
||||
props.setProperty("key1", "value1");
|
||||
vr.setAttributes(props);
|
||||
Map map = new HashMap();
|
||||
map.put("key2", new Integer(2));
|
||||
vr.setAttributesMap(map);
|
||||
vr.setExposedContextBeanNames(new String[] {"myBean2"});
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(sc) {
|
||||
public RequestDispatcher getRequestDispatcher(String path) {
|
||||
return new MockRequestDispatcher(path) {
|
||||
public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
|
||||
assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
|
||||
assertEquals("value1", forwardRequest.getAttribute("key1"));
|
||||
assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
|
||||
assertNull(forwardRequest.getAttribute("myBean"));
|
||||
assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
view.render(new HashMap(), request, response);
|
||||
}
|
||||
|
||||
public void testInternalResourceViewResolverWithJstl() throws Exception {
|
||||
Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
|
||||
|
||||
MockServletContext sc = new MockServletContext();
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.addMessage("code1", locale, "messageX");
|
||||
wac.refresh();
|
||||
InternalResourceViewResolver vr = new InternalResourceViewResolver();
|
||||
vr.setViewClass(JstlView.class);
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
|
||||
|
||||
view = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(sc);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
|
||||
Map model = new HashMap();
|
||||
TestBean tb = new TestBean();
|
||||
model.put("tb", tb);
|
||||
view.render(model, request, response);
|
||||
|
||||
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
|
||||
assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
|
||||
|
||||
assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
|
||||
LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
|
||||
assertEquals("messageX", lc.getResourceBundle().getString("code1"));
|
||||
}
|
||||
|
||||
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
|
||||
Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
|
||||
|
||||
MockServletContext sc = new MockServletContext();
|
||||
sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.addMessage("code1", locale, "messageX");
|
||||
wac.refresh();
|
||||
InternalResourceViewResolver vr = new InternalResourceViewResolver();
|
||||
vr.setViewClass(JstlView.class);
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
|
||||
|
||||
view = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(sc);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
|
||||
Map model = new HashMap();
|
||||
TestBean tb = new TestBean();
|
||||
model.put("tb", tb);
|
||||
view.render(model, request, response);
|
||||
|
||||
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
|
||||
assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
|
||||
|
||||
assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
|
||||
LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
|
||||
assertEquals("message1", lc.getResourceBundle().getString("code1"));
|
||||
assertEquals("message2", lc.getResourceBundle().getString("code2"));
|
||||
}
|
||||
|
||||
public void testXmlViewResolver() throws Exception {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.registerSingleton("testBean", TestBean.class);
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
TestBean testBean = (TestBean) wac.getBean("testBean");
|
||||
XmlViewResolver vr = new XmlViewResolver();
|
||||
vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view1 = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertTrue("Correct view class", TestView.class.equals(view1.getClass()));
|
||||
assertTrue("Correct URL", "/example1.jsp".equals(((InternalResourceView) view1).getUrl()));
|
||||
|
||||
View view2 = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertTrue("Correct view class", JstlView.class.equals(view2.getClass()));
|
||||
assertTrue("Correct URL", "/example2new.jsp".equals(((InternalResourceView) view2).getUrl()));
|
||||
|
||||
ServletContext sc = new MockServletContext();
|
||||
Map model = new HashMap();
|
||||
TestBean tb = new TestBean();
|
||||
model.put("tb", tb);
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest(sc);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
|
||||
view1.render(model, request, response);
|
||||
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
|
||||
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
|
||||
assertTrue("Correct test2 attribute", testBean.equals(request.getAttribute("test2")));
|
||||
|
||||
request = new MockHttpServletRequest(sc);
|
||||
response = new MockHttpServletResponse();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
|
||||
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
|
||||
view2.render(model, request, response);
|
||||
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
|
||||
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
|
||||
assertTrue("Correct test2 attribute", "testvalue2".equals(request.getAttribute("test2")));
|
||||
}
|
||||
|
||||
public void testXmlViewResolverDefaultLocation() {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext() {
|
||||
protected Resource getResourceByPath(String path) {
|
||||
assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
|
||||
return super.getResourceByPath(path);
|
||||
}
|
||||
};
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
XmlViewResolver vr = new XmlViewResolver();
|
||||
try {
|
||||
vr.setApplicationContext(wac);
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testXmlViewResolverWithoutCache() throws Exception {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext() {
|
||||
protected Resource getResourceByPath(String path) {
|
||||
assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
|
||||
return super.getResourceByPath(path);
|
||||
}
|
||||
};
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
XmlViewResolver vr = new XmlViewResolver();
|
||||
vr.setCache(false);
|
||||
try {
|
||||
vr.setApplicationContext(wac);
|
||||
}
|
||||
catch (ApplicationContextException ex) {
|
||||
fail("Should not have thrown ApplicationContextException: " + ex.getMessage());
|
||||
}
|
||||
try {
|
||||
vr.resolveViewName("example1", Locale.getDefault());
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testCacheRemoval() throws Exception {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
InternalResourceViewResolver vr = new InternalResourceViewResolver();
|
||||
vr.setViewClass(JstlView.class);
|
||||
vr.setApplicationContext(wac);
|
||||
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
View cached = vr.resolveViewName("example1", Locale.getDefault());
|
||||
if (view != cached) {
|
||||
fail("Caching doesn't work");
|
||||
}
|
||||
|
||||
vr.removeFromCache("example1", Locale.getDefault());
|
||||
cached = vr.resolveViewName("example1", Locale.getDefault());
|
||||
if (view == cached) {
|
||||
// the chance of having the same reference (hashCode) twice if negligable).
|
||||
fail("View wasn't removed from cache");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TestView extends InternalResourceView {
|
||||
|
||||
public void setLocation(Resource location) {
|
||||
if (!(location instanceof ServletContextResource)) {
|
||||
throw new IllegalArgumentException("Expecting ClassPathResource, not " + location.getClass().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="example1" class="org.springframework.web.servlet.view.ViewResolverTests$TestView">
|
||||
<property name="url"><value>/example1.jsp</value></property>
|
||||
<property name="attributesMap">
|
||||
<map>
|
||||
<entry key="test1"><value>testvalue1</value></entry>
|
||||
<entry key="test2"><ref bean="testBean"/></entry>
|
||||
</map>
|
||||
</property>
|
||||
<property name="location"><value>test</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="example2" class="org.springframework.web.servlet.view.JstlView">
|
||||
<property name="url"><value>/example2new.jsp</value></property>
|
||||
<property name="attributes">
|
||||
<value>
|
||||
test1=testvalue1
|
||||
test2=testvalue2
|
||||
</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user