KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > repository > ChildBeanRepositoryTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.repository;
5
6 import org.apache.xmlbeans.XmlObject;
7
8 import com.tc.config.schema.MockXmlObject;
9 import com.tc.config.schema.listen.MockConfigurationChangeListener;
10 import com.tc.test.TCTestCase;
11 import com.tc.util.TCAssertionError;
12
13 /**
14  * Unit test for {@link ChildBeanRepository}.
15  */

16 public class ChildBeanRepositoryTest extends TCTestCase {
17
18   // A child class, just to test that we check the actual class of the bean we're returning.
19
private static class MyMockXmlObject extends MockXmlObject {
20     public MyMockXmlObject() {
21       super();
22     }
23   }
24
25   private MockBeanRepository parent;
26   private Class JavaDoc requiredClass;
27   private MockChildBeanFetcher childFetcher;
28
29   private ChildBeanRepository repository;
30
31   private MockConfigurationChangeListener listener1;
32   private MockConfigurationChangeListener listener2;
33
34   public void setUp() throws Exception JavaDoc {
35     this.parent = new MockBeanRepository();
36     this.requiredClass = MyMockXmlObject.class;
37     this.childFetcher = new MockChildBeanFetcher();
38
39     this.repository = new ChildBeanRepository(this.parent, this.requiredClass, this.childFetcher);
40
41     this.listener1 = new MockConfigurationChangeListener();
42     this.listener2 = new MockConfigurationChangeListener();
43   }
44
45   public void testConstruction() throws Exception JavaDoc {
46     try {
47       new ChildBeanRepository(null, this.requiredClass, this.childFetcher);
48       fail("Didn't get NPE on no parent");
49     } catch (NullPointerException JavaDoc npe) {
50       // ok
51
}
52
53     try {
54       new ChildBeanRepository(this.parent, null, this.childFetcher);
55       fail("Didn't get NPE on no required class");
56     } catch (NullPointerException JavaDoc npe) {
57       // ok
58
}
59
60     try {
61       new ChildBeanRepository(this.parent, this.requiredClass, null);
62       fail("Didn't get NPE on no child fetcher");
63     } catch (NullPointerException JavaDoc npe) {
64       // ok
65
}
66   }
67
68   public void testRequiredBeanClass() throws Exception JavaDoc {
69     this.repository.ensureBeanIsOfClass(MyMockXmlObject.class);
70     this.repository.ensureBeanIsOfClass(MockXmlObject.class);
71     this.repository.ensureBeanIsOfClass(Object JavaDoc.class);
72
73     try {
74       this.repository.ensureBeanIsOfClass(String JavaDoc.class);
75       fail("Didn't get TCAE on wrong class");
76     } catch (TCAssertionError tcae) {
77       // ok
78
}
79   }
80
81   public void testInitiallyAddsListener() throws Exception JavaDoc {
82     assertEquals(1, this.parent.getNumAddListeners());
83     assertSame(this.repository, this.parent.getLastListener());
84   }
85
86   public void testBean() throws Exception JavaDoc {
87     MockXmlObject theParent = new MockXmlObject();
88     MyMockXmlObject child = new MyMockXmlObject();
89
90     this.parent.setReturnedBean(theParent);
91     this.childFetcher.setReturnedChild(child);
92
93     assertSame(child, this.repository.bean());
94     assertEquals(1, this.parent.getNumBeans());
95     assertEquals(1, this.childFetcher.getNumGetChilds());
96     assertSame(theParent, this.childFetcher.getLastParent());
97
98     this.parent.reset();
99     this.childFetcher.reset();
100
101     MockXmlObject childOfWrongClass = new MockXmlObject();
102     this.childFetcher.setReturnedChild(childOfWrongClass);
103
104     try {
105       this.repository.bean();
106       fail("Didn't get TCAE on child of wrong class");
107     } catch (TCAssertionError tcae) {
108       // ok
109
}
110   }
111
112   public void testListeners() throws Exception JavaDoc {
113     this.repository.addListener(this.listener1);
114     this.repository.addListener(this.listener2);
115
116     MockXmlObject parent1 = new MockXmlObject();
117     MockXmlObject parent2 = new MockXmlObject();
118     MyMockXmlObject child1 = new MyMockXmlObject();
119     MyMockXmlObject child2 = new MyMockXmlObject();
120
121     this.parent.setReturnedBean(parent1);
122     this.childFetcher.setReturnedChildren(new XmlObject[] { child1, child2 });
123
124     this.repository.configurationChanged(parent1, parent2);
125
126     assertEquals(1, this.listener1.getNumConfigurationChangeds());
127     assertSame(child1, this.listener1.getLastOldConfig());
128     assertSame(child2, this.listener2.getLastNewConfig());
129
130     assertEquals(2, this.childFetcher.getNumGetChilds());
131     assertEqualsUnordered(new Object JavaDoc[] { parent1, parent2 }, this.childFetcher.getLastParents());
132
133     this.listener1.reset();
134     this.listener2.reset();
135     this.childFetcher.reset();
136
137     this.childFetcher.setReturnedChildren(new XmlObject[] { child1, child1 });
138
139     this.repository.configurationChanged(parent1, parent2);
140     assertEquals(2, this.childFetcher.getNumGetChilds());
141     assertEqualsUnordered(new Object JavaDoc[] { parent1, parent2 }, this.childFetcher.getLastParents());
142
143     assertEquals(0, this.listener1.getNumConfigurationChangeds());
144     assertEquals(0, this.listener2.getNumConfigurationChangeds());
145
146     this.childFetcher.setReturnedChildren(new XmlObject[] { child1, new MockXmlObject() });
147
148     try {
149       this.repository.configurationChanged(parent1, parent2);
150       fail("Didn't get TCAE on wrong new child class");
151     } catch (TCAssertionError tcae) {
152       // ok
153
}
154   }
155
156 }
157
Popular Tags