KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > AbstractApplicationContextTests


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.context;
18
19 import java.util.Locale JavaDoc;
20
21 import org.springframework.beans.TestBean;
22 import org.springframework.beans.factory.AbstractListableBeanFactoryTests;
23 import org.springframework.beans.factory.BeanFactory;
24 import org.springframework.beans.factory.LifecycleBean;
25
26 /**
27  * @author Rod Johnson
28  * @author Juergen Hoeller
29  */

30 public abstract class AbstractApplicationContextTests extends AbstractListableBeanFactoryTests {
31
32     /** Must be supplied as XML */
33     public static final String JavaDoc TEST_NAMESPACE = "testNamespace";
34
35     protected ConfigurableApplicationContext applicationContext;
36
37     /** Subclass must register this */
38     protected TestListener listener = new TestListener();
39
40     protected TestListener parentListener = new TestListener();
41
42     protected void setUp() throws Exception JavaDoc {
43         this.applicationContext = createContext();
44     }
45
46     protected BeanFactory getBeanFactory() {
47         return applicationContext;
48     }
49
50     protected ApplicationContext getApplicationContext() {
51         return applicationContext;
52     }
53
54     /**
55      * Must register a TestListener.
56      * Must register standard beans.
57      * Parent must register rod with name Roderick
58      * and father with name Albert.
59      */

60     protected abstract ConfigurableApplicationContext createContext() throws Exception JavaDoc;
61
62     public void testContextAwareSingletonWasCalledBack() throws Exception JavaDoc {
63         ACATest aca = (ACATest) applicationContext.getBean("aca");
64         assertTrue("has had context set", aca.getApplicationContext() == applicationContext);
65         Object JavaDoc aca2 = applicationContext.getBean("aca");
66         assertTrue("Same instance", aca == aca2);
67         assertTrue("Says is singleton", applicationContext.isSingleton("aca"));
68     }
69
70     public void testContextAwarePrototypeWasCalledBack() throws Exception JavaDoc {
71         ACATest aca = (ACATest) applicationContext.getBean("aca-prototype");
72         assertTrue("has had context set", aca.getApplicationContext() == applicationContext);
73         Object JavaDoc aca2 = applicationContext.getBean("aca-prototype");
74         assertTrue("NOT Same instance", aca != aca2);
75         assertTrue("Says is prototype", !applicationContext.isSingleton("aca-prototype"));
76     }
77
78     public void testParentNonNull() {
79         assertTrue("parent isn't null", applicationContext.getParent() != null);
80     }
81
82     public void testGrandparentNull() {
83         assertTrue("grandparent is null", applicationContext.getParent().getParent() == null);
84     }
85
86     public void testOverrideWorked() throws Exception JavaDoc {
87         TestBean rod = (TestBean) applicationContext.getParent().getBean("rod");
88         assertTrue("Parent's name differs", rod.getName().equals("Roderick"));
89     }
90
91     public void testGrandparentDefinitionFound() throws Exception JavaDoc {
92         TestBean dad = (TestBean) applicationContext.getBean("father");
93         assertTrue("Dad has correct name", dad.getName().equals("Albert"));
94     }
95
96     public void testGrandparentTypedDefinitionFound() throws Exception JavaDoc {
97         TestBean dad = (TestBean) applicationContext.getBean("father", TestBean.class);
98         assertTrue("Dad has correct name", dad.getName().equals("Albert"));
99     }
100
101     public void testCloseTriggersDestroy() {
102         LifecycleBean lb = (LifecycleBean) applicationContext.getBean("lifecycle");
103         assertTrue("Not destroyed", !lb.isDestroyed());
104         applicationContext.close();
105         if (applicationContext.getParent() != null) {
106             ((ConfigurableApplicationContext) applicationContext.getParent()).close();
107         }
108         assertTrue("Destroyed", lb.isDestroyed());
109         applicationContext.close();
110         if (applicationContext.getParent() != null) {
111             ((ConfigurableApplicationContext) applicationContext.getParent()).close();
112         }
113         assertTrue("Destroyed", lb.isDestroyed());
114     }
115
116     public void testMessageSource() throws NoSuchMessageException {
117         assertEquals("message1", applicationContext.getMessage("code1", null, Locale.getDefault()));
118         assertEquals("message2", applicationContext.getMessage("code2", null, Locale.getDefault()));
119
120         try {
121             applicationContext.getMessage("code0", null, Locale.getDefault());
122             fail("looking for code0 should throw a NoSuchMessageException");
123         }
124         catch (NoSuchMessageException ex) {
125             // that's how it should be
126
}
127     }
128
129     public void testEvents() throws Exception JavaDoc {
130         listener.zeroCounter();
131         parentListener.zeroCounter();
132         assertTrue("0 events before publication", listener.getEventCount() == 0);
133         assertTrue("0 parent events before publication", parentListener.getEventCount() == 0);
134         this.applicationContext.publishEvent(new MyEvent(this));
135         assertTrue("1 events after publication, not " + listener.getEventCount(), listener.getEventCount() == 1);
136         assertTrue("1 parent events after publication", parentListener.getEventCount() == 1);
137     }
138
139     public void testBeanAutomaticallyHearsEvents() throws Exception JavaDoc {
140         //String[] listenerNames = ((ListableBeanFactory) applicationContext).getBeanDefinitionNames(ApplicationListener.class);
141
//assertTrue("listeners include beanThatListens", Arrays.asList(listenerNames).contains("beanThatListens"));
142
BeanThatListens b = (BeanThatListens) applicationContext.getBean("beanThatListens");
143         b.zero();
144         assertTrue("0 events before publication", b.getEventCount() == 0);
145         this.applicationContext.publishEvent(new MyEvent(this));
146         assertTrue("1 events after publication, not " + b.getEventCount(), b.getEventCount() == 1);
147     }
148
149
150     public static class MyEvent extends ApplicationEvent {
151
152         public MyEvent(Object JavaDoc source) {
153             super(source);
154         }
155     }
156
157 }
158
Popular Tags