KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > BeanFactoryPostProcessorTests


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.beans.factory.config;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.beans.MutablePropertyValues;
22 import org.springframework.beans.TestBean;
23 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
24 import org.springframework.beans.factory.support.RootBeanDefinition;
25 import org.springframework.context.support.StaticApplicationContext;
26
27 /**
28  * @author Colin Sampaleanu
29  * @author Juergen Hoeller
30  * @since 02.10.2003
31  */

32 public class BeanFactoryPostProcessorTests extends TestCase {
33
34     public void testRegisteredBeanFactoryPostProcessor() {
35         StaticApplicationContext ac = new StaticApplicationContext();
36         ac.registerSingleton("tb1", TestBean.class);
37         ac.registerSingleton("tb2", TestBean.class);
38         TestBeanFactoryPostProcessor bfpp = new TestBeanFactoryPostProcessor();
39         ac.addBeanFactoryPostProcessor(bfpp);
40         assertFalse(bfpp.wasCalled);
41         ac.refresh();
42         assertTrue(bfpp.wasCalled);
43     }
44     
45     public void testDefinedBeanFactoryPostProcessor() {
46         StaticApplicationContext ac = new StaticApplicationContext();
47         ac.registerSingleton("tb1", TestBean.class);
48         ac.registerSingleton("tb2", TestBean.class);
49         ac.registerSingleton("bfpp", TestBeanFactoryPostProcessor.class);
50         ac.refresh();
51         TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp");
52         assertTrue(bfpp.wasCalled);
53     }
54
55     public void testMultipleDefinedBeanFactoryPostProcessors() {
56         StaticApplicationContext ac = new StaticApplicationContext();
57         ac.registerSingleton("tb1", TestBean.class);
58         ac.registerSingleton("tb2", TestBean.class);
59         MutablePropertyValues pvs1 = new MutablePropertyValues();
60         pvs1.addPropertyValue("initValue", "${key}");
61         ac.registerSingleton("bfpp1", TestBeanFactoryPostProcessor.class, pvs1);
62         MutablePropertyValues pvs2 = new MutablePropertyValues();
63         pvs2.addPropertyValue("properties", "key=value");
64         ac.registerSingleton("bfpp2", PropertyPlaceholderConfigurer.class, pvs2);
65         ac.refresh();
66         TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp1");
67         assertEquals("value", bfpp.initValue);
68         assertTrue(bfpp.wasCalled);
69     }
70
71     public void testBeanFactoryPostProcessorNotExecutedByBeanFactory() {
72         DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
73         bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class));
74         bf.registerBeanDefinition("tb2", new RootBeanDefinition(TestBean.class));
75         bf.registerBeanDefinition("bfpp", new RootBeanDefinition(TestBeanFactoryPostProcessor.class));
76         TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) bf.getBean("bfpp");
77         assertFalse(bfpp.wasCalled);
78     }
79
80     
81     public static class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
82
83         public String JavaDoc initValue;
84
85         public void setInitValue(String JavaDoc initValue) {
86             this.initValue = initValue;
87         }
88
89         public boolean wasCalled = false;
90         
91         public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
92             wasCalled = true;
93         }
94     }
95     
96 }
97
Popular Tags