AbstractApplicationContextTests.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:6k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.context;
  17. import java.util.Locale;
  18. import org.springframework.beans.TestBean;
  19. import org.springframework.beans.factory.AbstractListableBeanFactoryTests;
  20. import org.springframework.beans.factory.BeanFactory;
  21. import org.springframework.beans.factory.LifecycleBean;
  22. /**
  23.  * @author Rod Johnson
  24.  * @author Juergen Hoeller
  25.  */
  26. public abstract class AbstractApplicationContextTests extends AbstractListableBeanFactoryTests {
  27. /** Must be supplied as XML */
  28. public static final String TEST_NAMESPACE = "testNamespace";
  29. protected ConfigurableApplicationContext applicationContext;
  30. /** Subclass must register this */
  31. protected TestListener listener = new TestListener();
  32. protected TestListener parentListener = new TestListener();
  33. protected void setUp() throws Exception {
  34. this.applicationContext = createContext();
  35. }
  36. protected BeanFactory getBeanFactory() {
  37. return applicationContext;
  38. }
  39. protected ApplicationContext getApplicationContext() {
  40. return applicationContext;
  41. }
  42. /**
  43.  * Must register a TestListener.
  44.  * Must register standard beans.
  45.  * Parent must register rod with name Roderick
  46.  * and father with name Albert.
  47.  */
  48. protected abstract ConfigurableApplicationContext createContext() throws Exception;
  49. public void testContextAwareSingletonWasCalledBack() throws Exception {
  50. ACATest aca = (ACATest) applicationContext.getBean("aca");
  51. assertTrue("has had context set", aca.getApplicationContext() == applicationContext);
  52. Object aca2 = applicationContext.getBean("aca");
  53. assertTrue("Same instance", aca == aca2);
  54. assertTrue("Says is singleton", applicationContext.isSingleton("aca"));
  55. }
  56. public void testContextAwarePrototypeWasCalledBack() throws Exception {
  57. ACATest aca = (ACATest) applicationContext.getBean("aca-prototype");
  58. assertTrue("has had context set", aca.getApplicationContext() == applicationContext);
  59. Object aca2 = applicationContext.getBean("aca-prototype");
  60. assertTrue("NOT Same instance", aca != aca2);
  61. assertTrue("Says is prototype", !applicationContext.isSingleton("aca-prototype"));
  62. }
  63. public void testParentNonNull() {
  64. assertTrue("parent isn't null", applicationContext.getParent() != null);
  65. }
  66. public void testGrandparentNull() {
  67. assertTrue("grandparent is null", applicationContext.getParent().getParent() == null);
  68. }
  69. public void testOverrideWorked() throws Exception {
  70. TestBean rod = (TestBean) applicationContext.getParent().getBean("rod");
  71. assertTrue("Parent's name differs", rod.getName().equals("Roderick"));
  72. }
  73. public void testGrandparentDefinitionFound() throws Exception {
  74. TestBean dad = (TestBean) applicationContext.getBean("father");
  75. assertTrue("Dad has correct name", dad.getName().equals("Albert"));
  76. }
  77. public void testGrandparentTypedDefinitionFound() throws Exception {
  78. TestBean dad = (TestBean) applicationContext.getBean("father", TestBean.class);
  79. assertTrue("Dad has correct name", dad.getName().equals("Albert"));
  80. }
  81. public void testCloseTriggersDestroy() {
  82. LifecycleBean lb = (LifecycleBean) applicationContext.getBean("lifecycle");
  83. assertTrue("Not destroyed", !lb.isDestroyed());
  84. applicationContext.close();
  85. if (applicationContext.getParent() != null) {
  86. ((ConfigurableApplicationContext) applicationContext.getParent()).close();
  87. }
  88. assertTrue("Destroyed", lb.isDestroyed());
  89. applicationContext.close();
  90. if (applicationContext.getParent() != null) {
  91. ((ConfigurableApplicationContext) applicationContext.getParent()).close();
  92. }
  93. assertTrue("Destroyed", lb.isDestroyed());
  94. }
  95. public void testMessageSource() throws NoSuchMessageException {
  96. assertEquals("message1", applicationContext.getMessage("code1", null, Locale.getDefault()));
  97. assertEquals("message2", applicationContext.getMessage("code2", null, Locale.getDefault()));
  98. try {
  99. applicationContext.getMessage("code0", null, Locale.getDefault());
  100. fail("looking for code0 should throw a NoSuchMessageException");
  101. }
  102. catch (NoSuchMessageException ex) {
  103. // that's how it should be
  104. }
  105. }
  106. public void testEvents() throws Exception {
  107. listener.zeroCounter();
  108. parentListener.zeroCounter();
  109. assertTrue("0 events before publication", listener.getEventCount() == 0);
  110. assertTrue("0 parent events before publication", parentListener.getEventCount() == 0);
  111. this.applicationContext.publishEvent(new MyEvent(this));
  112. assertTrue("1 events after publication, not " + listener.getEventCount(), listener.getEventCount() == 1);
  113. assertTrue("1 parent events after publication", parentListener.getEventCount() == 1);
  114. }
  115. public void testBeanAutomaticallyHearsEvents() throws Exception {
  116. //String[] listenerNames = ((ListableBeanFactory) applicationContext).getBeanDefinitionNames(ApplicationListener.class);
  117. //assertTrue("listeners include beanThatListens", Arrays.asList(listenerNames).contains("beanThatListens"));
  118. BeanThatListens b = (BeanThatListens) applicationContext.getBean("beanThatListens");
  119. b.zero();
  120. assertTrue("0 events before publication", b.getEventCount() == 0);
  121. this.applicationContext.publishEvent(new MyEvent(this));
  122. assertTrue("1 events after publication, not " + b.getEventCount(), b.getEventCount() == 1);
  123. }
  124. public static class MyEvent extends ApplicationEvent {
  125. public MyEvent(Object source) {
  126. super(source);
  127. }
  128. }
  129. }