KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > StaticApplicationContextMulticasterTests


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.support;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.springframework.beans.MutablePropertyValues;
24 import org.springframework.beans.TestBean;
25 import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
26 import org.springframework.context.ACATest;
27 import org.springframework.context.AbstractApplicationContextTests;
28 import org.springframework.context.ApplicationEvent;
29 import org.springframework.context.BeanThatListens;
30 import org.springframework.context.ConfigurableApplicationContext;
31 import org.springframework.context.event.SimpleApplicationEventMulticaster;
32 import org.springframework.core.io.ClassPathResource;
33
34 /**
35  * Tests for static application context with custom application event multicaster.
36  *
37  * @author Juergen Hoeller
38  */

39 public class StaticApplicationContextMulticasterTests extends AbstractApplicationContextTests {
40
41     protected StaticApplicationContext sac;
42
43     /** Run for each test */
44     protected ConfigurableApplicationContext createContext() throws Exception JavaDoc {
45         StaticApplicationContext parent = new StaticApplicationContext();
46         Map JavaDoc m = new HashMap JavaDoc();
47         m.put("name", "Roderick");
48         parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
49         m.put("name", "Albert");
50         parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
51         parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
52                 TestApplicationEventMulticaster.class, null);
53         parent.refresh();
54         parent.addListener(parentListener) ;
55
56         parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");
57
58         this.sac = new StaticApplicationContext(parent);
59         sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
60         sac.registerSingleton("aca", ACATest.class, new MutablePropertyValues());
61         sac.registerPrototype("aca-prototype", ACATest.class, new MutablePropertyValues());
62         PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
63         reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
64         sac.refresh();
65         sac.addListener(listener);
66
67         sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");
68
69         return sac;
70     }
71
72     /** Overridden */
73     public void testCount() {
74         assertCount(15);
75     }
76
77     public void testEvents() throws Exception JavaDoc {
78         TestApplicationEventMulticaster.counter = 0;
79         super.testEvents();
80         assertEquals(1, TestApplicationEventMulticaster.counter);
81     }
82
83
84     public static class TestApplicationEventMulticaster extends SimpleApplicationEventMulticaster {
85
86         private static int counter = 0;
87
88         public void multicastEvent(ApplicationEvent event) {
89             super.multicastEvent(event);
90             counter++;
91         }
92     }
93
94 }
95
Popular Tags