1 16 17 package org.springframework.web.context; 18 19 import java.util.Locale ; 20 21 import javax.servlet.ServletException ; 22 23 import org.springframework.beans.BeansException; 24 import org.springframework.beans.TestBean; 25 import org.springframework.beans.factory.DisposableBean; 26 import org.springframework.beans.factory.InitializingBean; 27 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 28 import org.springframework.beans.factory.config.BeanPostProcessor; 29 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 30 import org.springframework.context.AbstractApplicationContextTests; 31 import org.springframework.context.ConfigurableApplicationContext; 32 import org.springframework.context.NoSuchMessageException; 33 import org.springframework.context.TestListener; 34 import org.springframework.mock.web.MockServletContext; 35 import org.springframework.web.context.support.XmlWebApplicationContext; 36 37 41 public class XmlWebApplicationContextTests extends AbstractApplicationContextTests { 42 43 private ConfigurableWebApplicationContext root; 44 45 protected ConfigurableApplicationContext createContext() throws Exception { 46 InitAndIB.constructed = false; 47 root = new XmlWebApplicationContext(); 48 MockServletContext sc = new MockServletContext(""); 49 root.setServletContext(sc); 50 root.setConfigLocations(new String [] {"/org/springframework/web/context/WEB-INF/applicationContext.xml"}); 51 root.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { 52 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { 53 beanFactory.addBeanPostProcessor(new BeanPostProcessor() { 54 public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { 55 if (bean instanceof TestBean) { 56 ((TestBean) bean).getFriends().add("myFriend"); 57 } 58 return bean; 59 } 60 public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { 61 return bean; 62 } 63 }); 64 } 65 }); 66 root.refresh(); 67 XmlWebApplicationContext wac = new XmlWebApplicationContext(); 68 wac.setParent(root); 69 wac.setServletContext(sc); 70 wac.setNamespace("test-servlet"); 71 wac.setConfigLocations(new String [] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); 72 wac.refresh(); 73 return wac; 74 } 75 76 80 public void testEvents() throws Exception { 81 TestListener listener = (TestListener) this.applicationContext.getBean("testListener"); 82 listener.zeroCounter(); 83 TestListener parentListener = (TestListener) this.applicationContext.getParent().getBean("parentListener"); 84 parentListener.zeroCounter(); 85 86 parentListener.zeroCounter(); 87 assertTrue("0 events before publication", listener.getEventCount() == 0); 88 assertTrue("0 parent events before publication", parentListener.getEventCount() == 0); 89 this.applicationContext.publishEvent(new MyEvent(this)); 90 assertTrue("1 events after publication, not " + listener.getEventCount(), listener.getEventCount() == 1); 91 assertTrue("1 parent events after publication", parentListener.getEventCount() == 1); 92 } 93 94 public void testCount() { 95 assertTrue("should have 14 beans, not "+ this.applicationContext.getBeanDefinitionCount(), 96 this.applicationContext.getBeanDefinitionCount() == 14); 97 } 98 99 public void testWithoutMessageSource() throws Exception { 100 MockServletContext sc = new MockServletContext(""); 101 XmlWebApplicationContext wac = new XmlWebApplicationContext(); 102 wac.setParent(root); 103 wac.setServletContext(sc); 104 wac.setNamespace("testNamespace"); 105 wac.setConfigLocations(new String [] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); 106 wac.refresh(); 107 try { 108 wac.getMessage("someMessage", null, Locale.getDefault()); 109 fail("Should have thrown NoSuchMessageException"); 110 } 111 catch (NoSuchMessageException ex) { 112 } 114 String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); 115 assertTrue("Default message returned", "default".equals(msg)); 116 } 117 118 public void testContextNesting() { 119 TestBean father = (TestBean) this.applicationContext.getBean("father"); 120 assertTrue("Bean from root context", father != null); 121 assertTrue("Custom BeanPostProcessor applied", father.getFriends().contains("myFriend")); 122 123 TestBean rod = (TestBean) this.applicationContext.getBean("rod"); 124 assertTrue("Bean from child context", "Rod".equals(rod.getName())); 125 assertTrue("Bean has external reference", rod.getSpouse() == father); 126 assertTrue("Custom BeanPostProcessor not applied", !rod.getFriends().contains("myFriend")); 127 128 rod = (TestBean) this.root.getBean("rod"); 129 assertTrue("Bean from root context", "Roderick".equals(rod.getName())); 130 assertTrue("Custom BeanPostProcessor applied", rod.getFriends().contains("myFriend")); 131 } 132 133 public void testInitializingBeanAndInitMethod() throws Exception { 134 assertFalse(InitAndIB.constructed); 135 InitAndIB iib = (InitAndIB) this.applicationContext.getBean("init-and-ib"); 136 assertTrue(InitAndIB.constructed); 137 assertTrue(iib.afterPropertiesSetInvoked && iib.initMethodInvoked); 138 assertTrue(!iib.destroyed && !iib.customDestroyed); 139 this.applicationContext.close(); 140 assertTrue(!iib.destroyed && !iib.customDestroyed); 141 ConfigurableApplicationContext parent = (ConfigurableApplicationContext) this.applicationContext.getParent(); 142 parent.close(); 143 assertTrue(iib.destroyed && iib.customDestroyed); 144 parent.close(); 145 assertTrue(iib.destroyed && iib.customDestroyed); 146 } 147 148 149 public static class InitAndIB implements InitializingBean, DisposableBean { 150 151 public static boolean constructed; 152 153 public boolean afterPropertiesSetInvoked, initMethodInvoked, destroyed, customDestroyed; 154 155 public InitAndIB() { 156 constructed = true; 157 } 158 159 public void afterPropertiesSet() { 160 if (this.initMethodInvoked) 161 fail(); 162 this.afterPropertiesSetInvoked = true; 163 } 164 165 166 public void customInit() throws ServletException { 167 if (!this.afterPropertiesSetInvoked) 168 fail(); 169 this.initMethodInvoked = true; 170 } 171 172 public void destroy() { 173 if (this.customDestroyed) 174 fail(); 175 if (this.destroyed) { 176 throw new IllegalStateException ("Already destroyed"); 177 } 178 this.destroyed = true; 179 } 180 181 public void customDestroy() { 182 if (!this.destroyed) 183 fail(); 184 if (this.customDestroyed) { 185 throw new IllegalStateException ("Already customDestroyed"); 186 } 187 this.customDestroyed = true; 188 } 189 } 190 191 } 192 | Popular Tags |