73b232c7c1df9012808818404f969ee4a327cbfb
[utils] / support / general / src / test / java / org / wamblee / ioc / BeanKernelTest.java
1 /*
2  * Copyright 2005-2010 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.wamblee.ioc;
17
18
19 import junit.framework.TestCase;
20
21 /**
22  * Tests the bean kernel. The lookup of the bean factory itself can be tested
23  * only partially. Using a global property file for all test cases would tie all
24  * test cases together therefore no global property file is used.
25  * 
26  * @author Erik Brakkee
27  */
28 public class BeanKernelTest extends TestCase {
29     /**
30      * Loads the bean factory based on a property file configuration. Verifies
31      * the correct bean factory is loaded.
32      * 
33      */
34     public void testLoadBeanFactoryFromProperties() {
35         BeanFactory factory = BeanKernel
36             .lookupBeanFactory("org/wamblee/general/beankernel.properties");
37         assertTrue(factory instanceof TestBeanFactory);
38     }
39
40     /**
41      * Loads the bean factory based on a non-existing property file. Verifies
42      * that BeanFactoryException is thrown.
43      * 
44      */
45     public void testNonExistentPropertyFile() {
46         try {
47             BeanKernel
48                 .lookupBeanFactory("org/wamblee/general/beankernel-nonexistent.properties");
49         } catch (BeanFactoryException e) {
50             return; // ok
51         }
52
53         fail();
54     }
55
56     /**
57      * Loads the bean factory based on a property file with a non-existing bean
58      * factory defined in it. Verifies that BeanFactoryException is thrown.
59      * 
60      */
61     public void testNonExistentBeanFactory() {
62         try {
63             BeanKernel
64                 .lookupBeanFactory("org/wamblee/general/beankernel-wrong.properties");
65         } catch (BeanFactoryException e) {
66             return; // ok
67         }
68
69         fail();
70     }
71
72     /**
73      * Retrieves a bean factory throug the bean kernel. Verifies that beans can
74      * be retrieved.
75      * 
76      */
77     public void testRetrieveFactory() {
78         BeanKernel.overrideBeanFactory(new TestBeanFactory()); // bypass
79         // default
80         // property
81         // lookup
82
83         BeanFactory factory = BeanKernel.getBeanFactory();
84         assertNotNull(factory);
85         assertEquals("hello", factory.find(String.class));
86     }
87 }