1 16 17 package org.springframework.web.context; 18 19 import java.io.FileNotFoundException ; 20 import java.io.IOException ; 21 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletContextEvent ; 24 import javax.servlet.ServletContextListener ; 25 import javax.servlet.http.HttpServlet ; 26 27 import junit.framework.TestCase; 28 29 import org.springframework.beans.BeansException; 30 import org.springframework.beans.TestBean; 31 import org.springframework.beans.factory.BeanCreationException; 32 import org.springframework.beans.factory.BeanDefinitionStoreException; 33 import org.springframework.beans.factory.LifecycleBean; 34 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 35 import org.springframework.context.ApplicationContext; 36 import org.springframework.context.ApplicationContextException; 37 import org.springframework.context.support.ClassPathXmlApplicationContext; 38 import org.springframework.mock.web.MockServletConfig; 39 import org.springframework.mock.web.MockServletContext; 40 import org.springframework.web.context.support.XmlWebApplicationContext; 41 import org.springframework.web.servlet.DispatcherServlet; 42 import org.springframework.web.servlet.SimpleWebApplicationContext; 43 44 48 public class ContextLoaderTests extends TestCase { 49 50 public void testContextLoaderListenerWithDefaultContext() throws Exception { 51 MockServletContext sc = new MockServletContext(""); 52 sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, 53 "/org/springframework/web/context/WEB-INF/applicationContext.xml " + 54 "/org/springframework/web/context/WEB-INF/context-addition.xml"); 55 ServletContextListener listener = new ContextLoaderListener(); 56 ServletContextEvent event = new ServletContextEvent (sc); 57 listener.contextInitialized(event); 58 WebApplicationContext context = (WebApplicationContext) 59 sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 60 assertTrue("Correct WebApplicationContext exposed in ServletContext", 61 context instanceof XmlWebApplicationContext); 62 LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle"); 63 assertTrue("Has father", context.containsBean("father")); 64 assertTrue("Has rod", context.containsBean("rod")); 65 assertTrue("Has kerry", context.containsBean("kerry")); 66 assertTrue("Not destroyed", !lb.isDestroyed()); 67 listener.contextDestroyed(event); 68 assertTrue("Destroyed", lb.isDestroyed()); 69 } 70 71 public void testContextLoaderServletWithDefaultContext() throws Exception { 72 MockServletContext sc = new MockServletContext(""); 73 sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, 74 "/org/springframework/web/context/WEB-INF/applicationContext.xml"); 75 HttpServlet servlet = new ContextLoaderServlet(); 76 ServletConfig config = new MockServletConfig(sc, "test"); 77 servlet.init(config); 78 WebApplicationContext wc = (WebApplicationContext) 79 sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 80 assertTrue("Correct WebApplicationContext exposed in ServletContext", 81 wc instanceof XmlWebApplicationContext); 82 LifecycleBean lb = (LifecycleBean) wc.getBean("lifecycle"); 83 assertTrue("Not destroyed", !lb.isDestroyed()); 84 servlet.destroy(); 85 assertTrue("Destroyed", lb.isDestroyed()); 86 } 87 88 public void testContextLoaderWithCustomContext() throws Exception { 89 MockServletContext sc = new MockServletContext(""); 90 sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, 91 "org.springframework.web.servlet.SimpleWebApplicationContext"); 92 ServletContextListener listener = new ContextLoaderListener(); 93 ServletContextEvent event = new ServletContextEvent (sc); 94 listener.contextInitialized(event); 95 WebApplicationContext wc = (WebApplicationContext) 96 sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 97 assertTrue("Correct WebApplicationContext exposed in ServletContext", 98 wc instanceof SimpleWebApplicationContext); 99 } 100 101 public void testContextLoaderWithInvalidLocation() throws Exception { 102 MockServletContext sc = new MockServletContext(""); 103 sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml"); 104 ServletContextListener listener = new ContextLoaderListener(); 105 ServletContextEvent event = new ServletContextEvent (sc); 106 try { 107 listener.contextInitialized(event); 108 fail("Should have thrown ApplicationContextException"); 109 } 110 catch (BeanDefinitionStoreException ex) { 111 assertTrue(ex.getCause() instanceof FileNotFoundException ); 113 } 114 } 115 116 public void testContextLoaderWithInvalidContext() throws Exception { 117 MockServletContext sc = new MockServletContext(""); 118 sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, 119 "org.springframework.web.context.support.InvalidWebApplicationContext"); 120 ServletContextListener listener = new ContextLoaderListener(); 121 ServletContextEvent event = new ServletContextEvent (sc); 122 try { 123 listener.contextInitialized(event); 124 fail("Should have thrown ApplicationContextException"); 125 } 126 catch (ApplicationContextException ex) { 127 assertTrue(ex.getCause() instanceof ClassNotFoundException ); 129 } 130 } 131 132 public void testContextLoaderWithDefaultLocation() throws Exception { 133 MockServletContext sc = new MockServletContext(""); 134 ServletContextListener listener = new ContextLoaderListener(); 135 ServletContextEvent event = new ServletContextEvent (sc); 136 try { 137 listener.contextInitialized(event); 138 fail("Should have thrown BeanDefinitionStoreException"); 139 } 140 catch (BeanDefinitionStoreException ex) { 141 ex.printStackTrace(); 143 assertTrue(ex.getCause() instanceof IOException ); 144 assertTrue(ex.getCause().getMessage().indexOf("/WEB-INF/applicationContext.xml") != -1); 145 } 146 } 147 148 public void testFrameworkServletWithDefaultLocation() throws Exception { 149 DispatcherServlet servlet = new DispatcherServlet(); 150 servlet.setContextClass(XmlWebApplicationContext.class); 151 try { 152 servlet.init(new MockServletConfig(new MockServletContext(""), "test")); 153 fail("Should have thrown BeanDefinitionStoreException"); 154 } 155 catch (BeanDefinitionStoreException ex) { 156 assertTrue(ex.getCause() instanceof IOException ); 158 assertTrue(ex.getCause().getMessage().indexOf("/WEB-INF/test-servlet.xml") != -1); 159 } 160 } 161 162 public void testFrameworkServletWithCustomLocation() throws Exception { 163 DispatcherServlet servlet = new DispatcherServlet(); 164 servlet.setContextConfigLocation( 165 "/org/springframework/web/context/WEB-INF/testNamespace.xml " + 166 "/org/springframework/web/context/WEB-INF/context-addition.xml"); 167 servlet.init(new MockServletConfig(new MockServletContext(""), "test")); 168 assertTrue(servlet.getWebApplicationContext().containsBean("kerry")); 169 assertTrue(servlet.getWebApplicationContext().containsBean("kerryX")); 170 } 171 172 public void testClassPathXmlApplicationContext() throws IOException { 173 ApplicationContext context = new ClassPathXmlApplicationContext( 174 "/org/springframework/web/context/WEB-INF/applicationContext.xml"); 175 assertTrue("Has father", context.containsBean("father")); 176 assertTrue("Has rod", context.containsBean("rod")); 177 assertFalse("Hasn't kerry", context.containsBean("kerry")); 178 assertTrue("Doesn't have spouse", ((TestBean) context.getBean("rod")).getSpouse() == null); 179 assertTrue("myinit not evaluated", "Roderick".equals(((TestBean) context.getBean("rod")).getName())); 180 181 context = new ClassPathXmlApplicationContext(new String [] { 182 "/org/springframework/web/context/WEB-INF/applicationContext.xml", 183 "/org/springframework/web/context/WEB-INF/context-addition.xml"}); 184 assertTrue("Has father", context.containsBean("father")); 185 assertTrue("Has rod", context.containsBean("rod")); 186 assertTrue("Has kerry", context.containsBean("kerry")); 187 } 188 189 public void testSingletonDestructionOnStartupFailure() throws IOException { 190 try { 191 new ClassPathXmlApplicationContext(new String [] { 192 "/org/springframework/web/context/WEB-INF/applicationContext.xml", 193 "/org/springframework/web/context/WEB-INF/fail.xml"}) { 194 public void refresh() throws BeansException { 195 try { 196 super.refresh(); 197 } 198 catch (BeanCreationException ex) { 199 DefaultListableBeanFactory factory = (DefaultListableBeanFactory) getBeanFactory(); 200 assertEquals(0, factory.getSingletonCount()); 201 throw ex; 202 } 203 } 204 }; 205 fail("Should have thrown BeanCreationException"); 206 } 207 catch (BeanCreationException ex) { 208 } 210 } 211 212 } 213 | Popular Tags |