KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileNotFoundException JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletContextEvent JavaDoc;
24 import javax.servlet.ServletContextListener JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
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 /**
45  * @author Juergen Hoeller
46  * @since 12.08.2003
47  */

48 public class ContextLoaderTests extends TestCase {
49
50     public void testContextLoaderListenerWithDefaultContext() throws Exception JavaDoc {
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 JavaDoc listener = new ContextLoaderListener();
56         ServletContextEvent JavaDoc event = new ServletContextEvent JavaDoc(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 JavaDoc {
72         MockServletContext sc = new MockServletContext("");
73         sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
74                 "/org/springframework/web/context/WEB-INF/applicationContext.xml");
75         HttpServlet JavaDoc servlet = new ContextLoaderServlet();
76         ServletConfig JavaDoc 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 JavaDoc {
89         MockServletContext sc = new MockServletContext("");
90         sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
91                 "org.springframework.web.servlet.SimpleWebApplicationContext");
92         ServletContextListener JavaDoc listener = new ContextLoaderListener();
93         ServletContextEvent JavaDoc event = new ServletContextEvent JavaDoc(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 JavaDoc {
102         MockServletContext sc = new MockServletContext("");
103         sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
104         ServletContextListener JavaDoc listener = new ContextLoaderListener();
105         ServletContextEvent JavaDoc event = new ServletContextEvent JavaDoc(sc);
106         try {
107             listener.contextInitialized(event);
108             fail("Should have thrown ApplicationContextException");
109         }
110         catch (BeanDefinitionStoreException ex) {
111             // expected
112
assertTrue(ex.getCause() instanceof FileNotFoundException JavaDoc);
113         }
114     }
115
116     public void testContextLoaderWithInvalidContext() throws Exception JavaDoc {
117         MockServletContext sc = new MockServletContext("");
118         sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
119                 "org.springframework.web.context.support.InvalidWebApplicationContext");
120         ServletContextListener JavaDoc listener = new ContextLoaderListener();
121         ServletContextEvent JavaDoc event = new ServletContextEvent JavaDoc(sc);
122         try {
123             listener.contextInitialized(event);
124             fail("Should have thrown ApplicationContextException");
125         }
126         catch (ApplicationContextException ex) {
127             // expected
128
assertTrue(ex.getCause() instanceof ClassNotFoundException JavaDoc);
129         }
130     }
131
132     public void testContextLoaderWithDefaultLocation() throws Exception JavaDoc {
133         MockServletContext sc = new MockServletContext("");
134         ServletContextListener JavaDoc listener = new ContextLoaderListener();
135         ServletContextEvent JavaDoc event = new ServletContextEvent JavaDoc(sc);
136         try {
137             listener.contextInitialized(event);
138             fail("Should have thrown BeanDefinitionStoreException");
139         }
140         catch (BeanDefinitionStoreException ex) {
141             // expected
142
ex.printStackTrace();
143             assertTrue(ex.getCause() instanceof IOException JavaDoc);
144             assertTrue(ex.getCause().getMessage().indexOf("/WEB-INF/applicationContext.xml") != -1);
145         }
146     }
147
148     public void testFrameworkServletWithDefaultLocation() throws Exception JavaDoc {
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             // expected
157
assertTrue(ex.getCause() instanceof IOException JavaDoc);
158             assertTrue(ex.getCause().getMessage().indexOf("/WEB-INF/test-servlet.xml") != -1);
159         }
160     }
161
162     public void testFrameworkServletWithCustomLocation() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc[] {
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 JavaDoc {
190         try {
191             new ClassPathXmlApplicationContext(new String JavaDoc[] {
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             // expected
209
}
210     }
211
212 }
213
Popular Tags