KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > config > test > ConfigFactoryRegistryTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.config.test;
8
9
10 import java.util.ResourceBundle JavaDoc;
11
12 import junit.framework.TestCase;
13
14 import com.inversoft.config.ConfigFactory;
15 import com.inversoft.config.ConfigFactoryRegistry;
16
17
18 /**
19  * <p>
20  * This class contains the TestCases for the config factory
21  * registry
22  * </p>
23  *
24  * @author Brian Pontarelli
25  * @since 1.0
26  * @version 1.0
27  */

28 public class ConfigFactoryRegistryTest extends TestCase {
29
30     /**
31      * Constructs a new <code>ConfigFactoryRegistryTest</code> TestCase instance
32      */

33     public ConfigFactoryRegistryTest(String JavaDoc name) {
34         super(name);
35     }
36
37
38     /**
39      * Tests that without a property, the registry is empty. This test must run
40      * before any others, if it does not it will fail.
41      */

42     public void testEmpty() {
43
44         //System.setProperty(ConfigFactoryRegistry.PROPERTY_NAME, null);
45
//ConfigFactoryRegistry.initialize();
46

47         ConfigFactory factory = ConfigFactoryRegistry.lookup("good");
48         assertNull(factory);
49
50         factory = ConfigFactoryRegistry.lookup("bad");
51         assertNull(factory);
52     }
53
54     /**
55      * Tests that the registry loads the custom bundle correctly
56      */

57     public void testCustomBundle() {
58
59         ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
60             "com.inversoft.config.test.ConfigBundle");
61         ConfigFactoryRegistry.load(bundle);
62
63         ConfigFactory factory = ConfigFactoryRegistry.lookup("good");
64         assertTrue("Should not be null", factory != null);
65
66         factory = ConfigFactoryRegistry.lookup("bad");
67         assertTrue("Should be null", factory == null);
68     }
69
70     /**
71      * Tests that the registry methods work
72      */

73     public void testMethods() {
74
75         ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
76             "com.inversoft.config.test.ConfigBundle");
77         ConfigFactoryRegistry.load(bundle);
78
79         ConfigFactory factory1 = new TestConfigFactory();
80         ConfigFactory factory2 = new TestConfigFactory();
81         ConfigFactory factory3 = new TestConfigFactory();
82         ConfigFactoryRegistry.register("1", factory1);
83         ConfigFactoryRegistry.register("2", factory2);
84         ConfigFactoryRegistry.register("3", factory3);
85
86         assertTrue("Should be 1", ConfigFactoryRegistry.lookup("1") == factory1);
87         assertTrue("Should be 2", ConfigFactoryRegistry.lookup("2") == factory2);
88         assertTrue("Should be 3", ConfigFactoryRegistry.lookup("3") == factory3);
89
90         assertTrue("Should have unregistered 1", ConfigFactoryRegistry.unregister("1") == factory1);
91         assertTrue("Should have unregistered 2", ConfigFactoryRegistry.unregister("2") == factory2);
92         assertTrue("Should have unregistered 3", ConfigFactoryRegistry.unregister("3") == factory3);
93     }
94
95     /**
96      * Tests that the registry fails when factories don't extend the correct
97      * classes.
98      */

99     public void testWrongClass() {
100
101         ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
102             "com.inversoft.config.test.ConfigBundle");
103         ConfigFactoryRegistry.load(bundle);
104
105         ConfigFactory factory = ConfigFactoryRegistry.lookup("wrongClass");
106         assertNull(factory);
107         factory = ConfigFactoryRegistry.lookup("exception");
108         assertNull(factory);
109     }
110 }
Popular Tags