KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > config > component > test > ComponentConfigRegistryTest


1 /*
2  * Copyright (c) 2003-2004, 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.component.test;
8
9
10 import java.util.MissingResourceException JavaDoc;
11
12 import junit.framework.TestCase;
13
14 import com.inversoft.config.ConfigurationException;
15 import com.inversoft.config.component.ComponentConfigBuilder;
16 import com.inversoft.config.component.ComponentConfigBuilderRegistry;
17
18
19 /**
20  * <p>
21  * Tests the component configuration registry.
22  * </p>
23  *
24  * @author Brian Pontarelli
25  */

26 public class ComponentConfigRegistryTest extends TestCase {
27
28     /**
29      * Constructs a new <code>ComponentConfigRegistryTest</code>.
30      */

31     public ComponentConfigRegistryTest(String JavaDoc name) {
32         super(name);
33     }
34
35
36     /**
37      * Tests the load from a bundle and lookup
38      */

39     public void testLoad() {
40         try {
41             ComponentConfigBuilderRegistry reg = new ComponentConfigBuilderRegistry(
42                 "com.inversoft.config.component.test.TestBuilders");
43             ComponentConfigBuilder builder = reg.lookup("test");
44             assertNotNull(builder);
45             assertEquals(TestComponentConfigBuilder.class, builder.getClass());
46             assertSame(reg, builder.getRegistry());
47
48             ComponentConfigBuilder builder2 = new TestComponentConfigBuilder();
49             reg.register("test2", builder2);
50             builder = reg.lookup("test2");
51             assertNotNull(builder);
52             assertEquals(TestComponentConfigBuilder.class, builder.getClass());
53             assertSame(builder, builder2);
54             assertSame(reg, builder.getRegistry());
55             assertSame(reg, builder2.getRegistry());
56
57             reg.unregister("test2");
58             builder = reg.lookup("test2");
59             assertNull(builder);
60         } catch (ConfigurationException ce) {
61             fail(ce.toString());
62         }
63     }
64
65     /**
66      * Tests failure with a bad bundle name.
67      */

68     public void testBadBundle() {
69         try {
70             /*ComponentConfigBuilderRegistry reg =*/ new ComponentConfigBuilderRegistry(
71                 "com.inversoft.config.component.test.NotARealBundle");
72             fail("Should have failed because bundle doesn't exist");
73         } catch (ConfigurationException ce) {
74             assertFalse(ce.getErrors().isEmpty());
75             assertEquals(MissingResourceException JavaDoc.class, ce.getCause().getClass());
76         }
77     }
78
79     /**
80      * Tests failure with a bad builder in the bundle.
81      */

82     public void testBadBuilder() {
83         try {
84             /*ComponentConfigBuilderRegistry reg =*/ new ComponentConfigBuilderRegistry(
85                 "com.inversoft.config.component.test.BadBundle");
86             fail("Should have failed because builder doesn't extend interface");
87         } catch (ConfigurationException ce) {
88             assertFalse(ce.getErrors().isEmpty());
89             assertEquals(2, ce.getErrors().getAllErrors().size());
90             assertEquals("com.inversoft.config.component.test.BadComponentConfigBuilder is not a ConfigBuilder",
91                 ce.getErrors().getError(1).getMessage());
92         }
93     }
94
95     /**
96      * Test that the component registry fails if the class can not be constructed.
97      */

98     public void testConstructionFailure() {
99         try {
100             /*ComponentConfigBuilderRegistry reg =*/ new ComponentConfigBuilderRegistry(
101                 "com.inversoft.config.component.test.FailConstruction");
102             fail("Should have failed because builder can't be constructed");
103         } catch (ConfigurationException ce) {
104             assertFalse(ce.getErrors().isEmpty());
105             assertTrue(ce.getErrors().getError(0).getMessage().indexOf("instantiating") != -1);
106         }
107     }
108 }
Popular Tags