XmlListableBeanFactoryTests.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.beans.factory.xml;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.springframework.beans.BeansException;
  20. import org.springframework.beans.ITestBean;
  21. import org.springframework.beans.MutablePropertyValues;
  22. import org.springframework.beans.TestBean;
  23. import org.springframework.beans.factory.AbstractListableBeanFactoryTests;
  24. import org.springframework.beans.factory.BeanFactory;
  25. import org.springframework.beans.factory.DummyFactory;
  26. import org.springframework.beans.factory.LifecycleBean;
  27. import org.springframework.beans.factory.config.BeanPostProcessor;
  28. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  29. import org.springframework.beans.factory.support.RootBeanDefinition;
  30. import org.springframework.core.io.ClassPathResource;
  31. /**
  32.  * @author Juergen Hoeller
  33.  * @since 09.11.2003
  34.  */
  35. public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTests {
  36. private DefaultListableBeanFactory parent;
  37. private XmlBeanFactory factory;
  38. protected void setUp() {
  39. parent = new DefaultListableBeanFactory();
  40. Map m = new HashMap();
  41. m.put("name", "Albert");
  42. parent.registerBeanDefinition("father",
  43. new RootBeanDefinition(TestBean.class, new MutablePropertyValues(m)));
  44. m = new HashMap();
  45. m.put("name", "Roderick");
  46. parent.registerBeanDefinition("rod",
  47. new RootBeanDefinition(TestBean.class, new MutablePropertyValues(m)));
  48. // Load from classpath, NOT a file path
  49. this.factory = new XmlBeanFactory(new ClassPathResource("test.xml", getClass()), parent);
  50. this.factory.addBeanPostProcessor(new BeanPostProcessor() {
  51. public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
  52. if (bean instanceof TestBean) {
  53. ((TestBean) bean).setPostProcessed(true);
  54. }
  55. if (bean instanceof DummyFactory) {
  56. ((DummyFactory) bean).setPostProcessed(true);
  57. }
  58. return bean;
  59. }
  60. public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
  61. return bean;
  62. }
  63. });
  64. this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
  65. this.factory.preInstantiateSingletons();
  66. }
  67. protected BeanFactory getBeanFactory() {
  68. return factory;
  69. }
  70. public void testCount() {
  71. assertCount(14);
  72. }
  73. public void testTestBeanCount() {
  74. assertTestBeanCount(8);
  75. }
  76. public void testFactoryNesting() {
  77. ITestBean father = (ITestBean) getBeanFactory().getBean("father");
  78. assertTrue("Bean from root context", father != null);
  79. TestBean rod = (TestBean) getBeanFactory().getBean("rod");
  80. assertTrue("Bean from child context", "Rod".equals(rod.getName()));
  81. assertTrue("Bean has external reference", rod.getSpouse() == father);
  82. rod = (TestBean) parent.getBean("rod");
  83. assertTrue("Bean from root context", "Roderick".equals(rod.getName()));
  84. }
  85. public void testFactoryReferences() {
  86. DummyReferencer ref = (DummyReferencer) getBeanFactory().getBean("factoryReferencer");
  87. DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
  88. assertTrue(ref.getTestBean1() == ref.getTestBean2());
  89. assertTrue(ref.getDummyFactory() == factory);
  90. }
  91. public void testPrototypeReferences() {
  92. // check that not broken by circular reference resolution mechanism
  93. DummyReferencer ref1 = (DummyReferencer) getBeanFactory().getBean("prototypeReferencer");
  94. assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref1.getTestBean2());
  95. DummyReferencer ref2 = (DummyReferencer) getBeanFactory().getBean("prototypeReferencer");
  96. assertTrue("Not the same referencer", ref1 != ref2);
  97. assertTrue("Not referencing same bean twice", ref2.getTestBean1() != ref2.getTestBean2());
  98. assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean1());
  99. assertTrue("Not referencing same bean twice", ref1.getTestBean2() != ref2.getTestBean2());
  100. assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean2());
  101. }
  102. public void testBeanPostProcessor() throws Exception {
  103. TestBean kerry = (TestBean) getBeanFactory().getBean("kerry");
  104. TestBean kathy = (TestBean) getBeanFactory().getBean("kathy");
  105. DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
  106. TestBean factoryCreated = (TestBean) getBeanFactory().getBean("singletonFactory");
  107. assertTrue(kerry.isPostProcessed());
  108. assertTrue(kathy.isPostProcessed());
  109. assertTrue(factory.isPostProcessed());
  110. assertTrue(factoryCreated.isPostProcessed());
  111. }
  112. public void testEmptyValues() {
  113. TestBean rod = (TestBean) getBeanFactory().getBean("rod");
  114. TestBean kerry = (TestBean) getBeanFactory().getBean("kerry");
  115. assertTrue("Touchy is empty", "".equals(rod.getTouchy()));
  116. assertTrue("Touchy is empty", "".equals(kerry.getTouchy()));
  117. }
  118. public void testCommentsAndCdataInValue() {
  119. TestBean bean = (TestBean) getBeanFactory().getBean("commentsInValue");
  120. assertEquals("Failed to handle comments and CDATA properly",
  121.  "this is a <!--comment-->", bean.getName());
  122. }
  123. }