KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > XmlWebApplicationContextTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.context;
18
19 import java.util.Locale JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
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 /**
38  * @author Rod Johnson
39  * @author Juergen Hoeller
40  */

41 public class XmlWebApplicationContextTests extends AbstractApplicationContextTests {
42
43     private ConfigurableWebApplicationContext root;
44
45     protected ConfigurableApplicationContext createContext() throws Exception JavaDoc {
46         InitAndIB.constructed = false;
47         root = new XmlWebApplicationContext();
48         MockServletContext sc = new MockServletContext("");
49         root.setServletContext(sc);
50         root.setConfigLocations(new String JavaDoc[] {"/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 JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc name) throws BeansException {
55                         if (bean instanceof TestBean) {
56                             ((TestBean) bean).getFriends().add("myFriend");
57                         }
58                         return bean;
59                     }
60                     public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc 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 JavaDoc[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"});
72         wac.refresh();
73         return wac;
74     }
75
76     /**
77      * Overridden as we can't trust superclass method
78      * @see org.springframework.context.AbstractApplicationContextTests#testEvents()
79      */

80     public void testEvents() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc[] {"/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             // expected;
113
}
114         String JavaDoc 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 JavaDoc {
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         /** Init method */
166         public void customInit() throws ServletException JavaDoc {
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 JavaDoc("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 JavaDoc("Already customDestroyed");
186             }
187             this.customDestroyed = true;
188         }
189     }
190
191 }
192
Popular Tags