1 16 17 package org.springframework.context.support; 18 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 import java.util.Map ; 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 39 public class StaticApplicationContextMulticasterTests extends AbstractApplicationContextTests { 40 41 protected StaticApplicationContext sac; 42 43 44 protected ConfigurableApplicationContext createContext() throws Exception { 45 StaticApplicationContext parent = new StaticApplicationContext(); 46 Map m = new HashMap (); 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 73 public void testCount() { 74 assertCount(15); 75 } 76 77 public void testEvents() throws Exception { 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 |