1 16 17 package org.springframework.context; 18 19 import java.util.Locale ; 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 30 public abstract class AbstractApplicationContextTests extends AbstractListableBeanFactoryTests { 31 32 33 public static final String TEST_NAMESPACE = "testNamespace"; 34 35 protected ConfigurableApplicationContext applicationContext; 36 37 38 protected TestListener listener = new TestListener(); 39 40 protected TestListener parentListener = new TestListener(); 41 42 protected void setUp() throws Exception { 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 60 protected abstract ConfigurableApplicationContext createContext() throws Exception ; 61 62 public void testContextAwareSingletonWasCalledBack() throws Exception { 63 ACATest aca = (ACATest) applicationContext.getBean("aca"); 64 assertTrue("has had context set", aca.getApplicationContext() == applicationContext); 65 Object 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 { 71 ACATest aca = (ACATest) applicationContext.getBean("aca-prototype"); 72 assertTrue("has had context set", aca.getApplicationContext() == applicationContext); 73 Object 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 { 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 { 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 { 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 } 127 } 128 129 public void testEvents() throws Exception { 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 { 140 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 source) { 153 super(source); 154 } 155 } 156 157 } 158 | Popular Tags |