KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > ServletContextSupportTests


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.support;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.springframework.beans.MutablePropertyValues;
27 import org.springframework.beans.TestBean;
28 import org.springframework.beans.factory.BeanCreationException;
29 import org.springframework.beans.factory.config.BeanDefinitionHolder;
30 import org.springframework.beans.factory.config.ConstructorArgumentValues;
31 import org.springframework.beans.factory.config.RuntimeBeanReference;
32 import org.springframework.beans.factory.support.ChildBeanDefinition;
33 import org.springframework.beans.factory.support.ManagedList;
34 import org.springframework.beans.factory.support.ManagedMap;
35 import org.springframework.beans.factory.support.ManagedSet;
36 import org.springframework.beans.factory.support.RootBeanDefinition;
37 import org.springframework.mock.web.MockServletContext;
38
39 /**
40  * Tests for various ServletContext-related support classes.
41  *
42  * @author Juergen Hoeller
43  * @since 22.12.2004
44  */

45 public class ServletContextSupportTests extends TestCase {
46
47     public void testServletContextFactoryBean() {
48         MockServletContext sc = new MockServletContext();
49
50         StaticWebApplicationContext wac = new StaticWebApplicationContext();
51         wac.setServletContext(sc);
52         MutablePropertyValues pvs = new MutablePropertyValues();
53         wac.registerSingleton("servletContext", ServletContextFactoryBean.class, pvs);
54         wac.refresh();
55
56         Object JavaDoc value = wac.getBean("servletContext");
57         assertEquals(sc, value);
58     }
59
60     public void testServletContextAttributeFactoryBean() {
61         MockServletContext sc = new MockServletContext();
62         sc.setAttribute("myAttr", "myValue");
63
64         StaticWebApplicationContext wac = new StaticWebApplicationContext();
65         wac.setServletContext(sc);
66         MutablePropertyValues pvs = new MutablePropertyValues();
67         pvs.addPropertyValue("attributeName", "myAttr");
68         wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
69         wac.refresh();
70
71         Object JavaDoc value = wac.getBean("importedAttr");
72         assertEquals("myValue", value);
73     }
74
75     public void testServletContextAttributeFactoryBeanWithAttributeNotFound() {
76         MockServletContext sc = new MockServletContext();
77
78         StaticWebApplicationContext wac = new StaticWebApplicationContext();
79         wac.setServletContext(sc);
80         MutablePropertyValues pvs = new MutablePropertyValues();
81         pvs.addPropertyValue("attributeName", "myAttr");
82         wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
83
84         try {
85             wac.refresh();
86             fail("Should have thrown BeanCreationException");
87         }
88         catch (BeanCreationException ex) {
89             // expected
90
assertTrue(ex.getCause() instanceof IllegalStateException JavaDoc);
91             assertTrue(ex.getCause().getMessage().indexOf("myAttr") != -1);
92         }
93     }
94
95     public void testServletContextAttributeExporter() {
96         TestBean tb = new TestBean();
97         Map JavaDoc attributes = new HashMap JavaDoc();
98         attributes.put("attr1", "value1");
99         attributes.put("attr2", tb);
100
101         MockServletContext sc = new MockServletContext();
102         ServletContextAttributeExporter exporter = new ServletContextAttributeExporter();
103         exporter.setAttributes(attributes);
104         exporter.setServletContext(sc);
105
106         assertEquals("value1", sc.getAttribute("attr1"));
107         assertSame(tb, sc.getAttribute("attr2"));
108     }
109
110     public void testServletContextPropertyPlaceholderConfigurer() {
111         MockServletContext sc = new MockServletContext();
112         sc.addInitParameter("key4", "mykey4");
113
114         StaticWebApplicationContext wac = new StaticWebApplicationContext();
115         wac.setServletContext(sc);
116
117         MutablePropertyValues pvs = new MutablePropertyValues();
118         pvs.addPropertyValue("age", "${age}");
119         pvs.addPropertyValue("name", "${key4}name${var}${var}${");
120         pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
121         wac.registerSingleton("tb1", TestBean.class, pvs);
122
123         RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
124         wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
125
126         pvs = new MutablePropertyValues();
127         pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my");
128         wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
129
130         wac.refresh();
131
132         TestBean tb1 = (TestBean) wac.getBean("tb1");
133         TestBean tb2 = (TestBean) wac.getBean("tb2");
134         assertEquals(98, tb1.getAge());
135         assertEquals("mykey4namemyvarmyvar${", tb1.getName());
136         assertEquals(tb2, tb1.getSpouse());
137     }
138
139     public void testServletContextPropertyPlaceholderConfigurerWithLocalOverriding() {
140         MockServletContext sc = new MockServletContext();
141         sc.addInitParameter("key4", "mykey4");
142
143         StaticWebApplicationContext wac = new StaticWebApplicationContext();
144         wac.setServletContext(sc);
145
146         MutablePropertyValues pvs = new MutablePropertyValues();
147         pvs.addPropertyValue("age", "${age}");
148         pvs.addPropertyValue("name", "${key4}name${var}${var}${");
149         pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
150         wac.registerSingleton("tb1", TestBean.class, pvs);
151
152         RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
153         wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
154
155         pvs = new MutablePropertyValues();
156         pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
157         wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
158
159         wac.refresh();
160
161         TestBean tb1 = (TestBean) wac.getBean("tb1");
162         TestBean tb2 = (TestBean) wac.getBean("tb2");
163         assertEquals(98, tb1.getAge());
164         assertEquals("yourkey4namemyvarmyvar${", tb1.getName());
165         assertEquals(tb2, tb1.getSpouse());
166     }
167
168     public void testServletContextPropertyPlaceholderConfigurerWithContextOverride() {
169         MockServletContext sc = new MockServletContext();
170         sc.addInitParameter("key4", "mykey4");
171
172         StaticWebApplicationContext wac = new StaticWebApplicationContext();
173         wac.setServletContext(sc);
174
175         MutablePropertyValues pvs = new MutablePropertyValues();
176         pvs.addPropertyValue("age", "${age}");
177         pvs.addPropertyValue("name", "${key4}name${var}${var}${");
178         pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
179         wac.registerSingleton("tb1", TestBean.class, pvs);
180
181         RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
182         wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
183
184         pvs = new MutablePropertyValues();
185         pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
186         pvs.addPropertyValue("contextOverride", Boolean.TRUE);
187         wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
188
189         wac.refresh();
190
191         TestBean tb1 = (TestBean) wac.getBean("tb1");
192         TestBean tb2 = (TestBean) wac.getBean("tb2");
193         assertEquals(98, tb1.getAge());
194         assertEquals("mykey4namemyvarmyvar${", tb1.getName());
195         assertEquals(tb2, tb1.getSpouse());
196     }
197
198     public void testServletContextPropertyPlaceholderConfigurerWithContextOverrideAndAttributes() {
199         MockServletContext sc = new MockServletContext();
200         sc.addInitParameter("key4", "mykey4");
201         sc.setAttribute("key4", "attrkey4");
202
203         StaticWebApplicationContext wac = new StaticWebApplicationContext();
204         wac.setServletContext(sc);
205
206         MutablePropertyValues pvs = new MutablePropertyValues();
207         pvs.addPropertyValue("age", "${age}");
208         pvs.addPropertyValue("name", "${key4}name${var}${var}${");
209         pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
210         wac.registerSingleton("tb1", TestBean.class, pvs);
211
212         RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
213         wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
214
215         pvs = new MutablePropertyValues();
216         pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
217         pvs.addPropertyValue("contextOverride", Boolean.TRUE);
218         pvs.addPropertyValue("searchContextAttributes", Boolean.TRUE);
219         wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
220
221         wac.refresh();
222
223         TestBean tb1 = (TestBean) wac.getBean("tb1");
224         TestBean tb2 = (TestBean) wac.getBean("tb2");
225         assertEquals(98, tb1.getAge());
226         assertEquals("attrkey4namemyvarmyvar${", tb1.getName());
227         assertEquals(tb2, tb1.getSpouse());
228     }
229
230     public void testServletContextPropertyPlaceholderConfigurerWithAttributes() {
231         MockServletContext sc = new MockServletContext();
232         sc.addInitParameter("key4", "mykey4");
233
234         StaticWebApplicationContext wac = new StaticWebApplicationContext();
235         wac.setServletContext(sc);
236
237         MutablePropertyValues pvs = new MutablePropertyValues();
238         pvs.addPropertyValue("age", "${age}");
239         pvs.addPropertyValue("name", "name${var}${var}${");
240         pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
241         wac.registerSingleton("tb1", TestBean.class, pvs);
242
243         ConstructorArgumentValues cas = new ConstructorArgumentValues();
244         cas.addIndexedArgumentValue(1, "${age}");
245         cas.addGenericArgumentValue("${var}name${age}");
246
247         pvs = new MutablePropertyValues();
248         List JavaDoc friends = new ManagedList();
249         friends.add("na${age}me");
250         friends.add(new RuntimeBeanReference("${ref}"));
251         pvs.addPropertyValue("friends", friends);
252
253         Set JavaDoc someSet = new ManagedSet();
254         someSet.add("na${age}me");
255         someSet.add(new RuntimeBeanReference("${ref}"));
256         pvs.addPropertyValue("someSet", someSet);
257
258         Map JavaDoc someMap = new ManagedMap();
259         someMap.put("key1", new RuntimeBeanReference("${ref}"));
260         someMap.put("key2", "${age}name");
261         MutablePropertyValues innerPvs = new MutablePropertyValues();
262         innerPvs.addPropertyValue("touchy", "${os.name}");
263         someMap.put("key3", new RootBeanDefinition(TestBean.class, innerPvs));
264         MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs);
265         someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
266         pvs.addPropertyValue("someMap", someMap);
267
268         RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
269         wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
270
271         pvs = new MutablePropertyValues();
272         pvs.addPropertyValue("properties", "var=${m}var\nref=tb2\nm=my");
273         pvs.addPropertyValue("searchContextAttributes", Boolean.TRUE);
274         wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
275         sc.setAttribute("age", new Integer JavaDoc(98));
276
277         wac.refresh();
278
279         TestBean tb1 = (TestBean) wac.getBean("tb1");
280         TestBean tb2 = (TestBean) wac.getBean("tb2");
281         assertEquals(98, tb1.getAge());
282         assertEquals(98, tb2.getAge());
283         assertEquals("namemyvarmyvar${", tb1.getName());
284         assertEquals("myvarname98", tb2.getName());
285         assertEquals(tb2, tb1.getSpouse());
286         assertEquals(2, tb2.getFriends().size());
287         assertEquals("na98me", tb2.getFriends().iterator().next());
288         assertEquals(tb2, ((List JavaDoc) tb2.getFriends()).get(1));
289         assertEquals(2, tb2.getSomeSet().size());
290         assertTrue(tb2.getSomeSet().contains("na98me"));
291         assertTrue(tb2.getSomeSet().contains(tb2));
292         assertEquals(4, tb2.getSomeMap().size());
293         assertEquals(tb2, tb2.getSomeMap().get("key1"));
294         assertEquals("98name", tb2.getSomeMap().get("key2"));
295         TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
296         TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
297         assertEquals(0, inner1.getAge());
298         assertEquals(null, inner1.getName());
299         assertEquals(System.getProperty("os.name"), inner1.getTouchy());
300         assertEquals(98, inner2.getAge());
301         assertEquals("namemyvarmyvar${", inner2.getName());
302         assertEquals(System.getProperty("os.name"), inner2.getTouchy());
303     }
304
305 }
306
Popular Tags