AbstractListableBeanFactoryTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

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;
  17. import org.springframework.beans.TestBean;
  18. /**
  19.  * @author Rod Johnson
  20.  */
  21. public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFactoryTests {
  22. /** Subclasses must initialize this */
  23. protected ListableBeanFactory getListableBeanFactory() {
  24. BeanFactory bf = getBeanFactory();
  25. if (!(bf instanceof ListableBeanFactory)) {
  26. throw new RuntimeException("ListableBeanFactory required");
  27. }
  28. return (ListableBeanFactory) bf;
  29. }
  30. /**
  31.  * Subclasses can override this.
  32.  */
  33. public void testCount() {
  34. assertCount(13);
  35. }
  36. protected final void assertCount(int count) {
  37. String[] defnames = getListableBeanFactory().getBeanDefinitionNames();
  38. assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count);
  39. }
  40. public void testTestBeanCount() {
  41. assertTestBeanCount(7);
  42. }
  43. public void assertTestBeanCount(int count) {
  44. String[] defnames = getListableBeanFactory().getBeanDefinitionNames(TestBean.class);
  45. assertTrue("We should have " + count + " beans for class org.springframework.beans.TestBean, not " +
  46.            defnames.length, defnames.length == count);
  47. }
  48. public void testGetDefinitionsForNoSuchClass() {
  49. String[] defnames = getListableBeanFactory().getBeanDefinitionNames(String.class);
  50. assertTrue("No string definitions", defnames.length == 0);
  51. }
  52. /**
  53.  * Check that count refers to factory class, not bean class. (We don't know
  54.  * what type factories may return, and it may even change over time.)
  55.  */
  56. public void testGetCountForFactoryClass() {
  57. assertTrue("Should have 2 factories, not " +
  58.  getListableBeanFactory().getBeanDefinitionNames(FactoryBean.class).length,
  59.  getListableBeanFactory().getBeanDefinitionNames(FactoryBean.class).length == 2);
  60. }
  61. public void testContainsBeanDefinition() {
  62. assertTrue(getListableBeanFactory().containsBeanDefinition("rod"));
  63. assertTrue(getListableBeanFactory().containsBeanDefinition("roderick"));
  64. }
  65. }