KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > AbstractListableBeanFactoryTests


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.beans.factory;
18
19 import org.springframework.beans.TestBean;
20
21 /**
22  * @author Rod Johnson
23  */

24 public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFactoryTests {
25
26     /** Subclasses must initialize this */
27     protected ListableBeanFactory getListableBeanFactory() {
28         BeanFactory bf = getBeanFactory();
29         if (!(bf instanceof ListableBeanFactory)) {
30             throw new IllegalStateException JavaDoc("ListableBeanFactory required");
31         }
32         return (ListableBeanFactory) bf;
33     }
34     
35     /**
36      * Subclasses can override this.
37      */

38     public void testCount() {
39         assertCount(13);
40     }
41     
42     protected final void assertCount(int count) {
43         String JavaDoc[] defnames = getListableBeanFactory().getBeanDefinitionNames();
44         assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count);
45     }
46
47     public void testTestBeanCount() {
48         assertTestBeanCount(7);
49     }
50
51     public void assertTestBeanCount(int count) {
52         String JavaDoc[] defNames = getListableBeanFactory().getBeanDefinitionNames(TestBean.class);
53         assertTrue("We should have " + count + " beans for class org.springframework.beans.TestBean, not " +
54                 defNames.length, defNames.length == count);
55
56         int countIncludingFactoryBeans = count + 2;
57         String JavaDoc[] names = getListableBeanFactory().getBeanNamesForType(TestBean.class);
58         assertTrue("We should have " + countIncludingFactoryBeans +
59                 " beans for class org.springframework.beans.TestBean, not " + names.length,
60                 names.length == countIncludingFactoryBeans);
61     }
62
63     public void testGetDefinitionsForNoSuchClass() {
64         String JavaDoc[] defnames = getListableBeanFactory().getBeanDefinitionNames(String JavaDoc.class);
65         assertTrue("No string definitions", defnames.length == 0);
66     }
67     
68     /**
69      * Check that count refers to factory class, not bean class. (We don't know
70      * what type factories may return, and it may even change over time.)
71      */

72     public void testGetCountForFactoryClass() {
73         assertTrue("Should have 2 factories, not " +
74                 getListableBeanFactory().getBeanDefinitionNames(FactoryBean.class).length,
75                 getListableBeanFactory().getBeanDefinitionNames(FactoryBean.class).length == 2);
76
77         assertTrue("Should have 2 factories, not " +
78                 getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length,
79                 getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2);
80     }
81
82     public void testContainsBeanDefinition() {
83         assertTrue(getListableBeanFactory().containsBeanDefinition("rod"));
84         assertTrue(getListableBeanFactory().containsBeanDefinition("roderick"));
85     }
86
87 }
88
Popular Tags