001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config.schema.repository;
005:
006: import org.apache.xmlbeans.XmlObject;
007:
008: import com.tc.config.schema.MockXmlObject;
009: import com.tc.config.schema.listen.MockConfigurationChangeListener;
010: import com.tc.test.TCTestCase;
011: import com.tc.util.TCAssertionError;
012:
013: /**
014: * Unit test for {@link ChildBeanRepository}.
015: */
016: public class ChildBeanRepositoryTest extends TCTestCase {
017:
018: // A child class, just to test that we check the actual class of the bean we're returning.
019: private static class MyMockXmlObject extends MockXmlObject {
020: public MyMockXmlObject() {
021: super ();
022: }
023: }
024:
025: private MockBeanRepository parent;
026: private Class requiredClass;
027: private MockChildBeanFetcher childFetcher;
028:
029: private ChildBeanRepository repository;
030:
031: private MockConfigurationChangeListener listener1;
032: private MockConfigurationChangeListener listener2;
033:
034: public void setUp() throws Exception {
035: this .parent = new MockBeanRepository();
036: this .requiredClass = MyMockXmlObject.class;
037: this .childFetcher = new MockChildBeanFetcher();
038:
039: this .repository = new ChildBeanRepository(this .parent,
040: this .requiredClass, this .childFetcher);
041:
042: this .listener1 = new MockConfigurationChangeListener();
043: this .listener2 = new MockConfigurationChangeListener();
044: }
045:
046: public void testConstruction() throws Exception {
047: try {
048: new ChildBeanRepository(null, this .requiredClass,
049: this .childFetcher);
050: fail("Didn't get NPE on no parent");
051: } catch (NullPointerException npe) {
052: // ok
053: }
054:
055: try {
056: new ChildBeanRepository(this .parent, null,
057: this .childFetcher);
058: fail("Didn't get NPE on no required class");
059: } catch (NullPointerException npe) {
060: // ok
061: }
062:
063: try {
064: new ChildBeanRepository(this .parent, this .requiredClass,
065: null);
066: fail("Didn't get NPE on no child fetcher");
067: } catch (NullPointerException npe) {
068: // ok
069: }
070: }
071:
072: public void testRequiredBeanClass() throws Exception {
073: this .repository.ensureBeanIsOfClass(MyMockXmlObject.class);
074: this .repository.ensureBeanIsOfClass(MockXmlObject.class);
075: this .repository.ensureBeanIsOfClass(Object.class);
076:
077: try {
078: this .repository.ensureBeanIsOfClass(String.class);
079: fail("Didn't get TCAE on wrong class");
080: } catch (TCAssertionError tcae) {
081: // ok
082: }
083: }
084:
085: public void testInitiallyAddsListener() throws Exception {
086: assertEquals(1, this .parent.getNumAddListeners());
087: assertSame(this .repository, this .parent.getLastListener());
088: }
089:
090: public void testBean() throws Exception {
091: MockXmlObject theParent = new MockXmlObject();
092: MyMockXmlObject child = new MyMockXmlObject();
093:
094: this .parent.setReturnedBean(theParent);
095: this .childFetcher.setReturnedChild(child);
096:
097: assertSame(child, this .repository.bean());
098: assertEquals(1, this .parent.getNumBeans());
099: assertEquals(1, this .childFetcher.getNumGetChilds());
100: assertSame(theParent, this .childFetcher.getLastParent());
101:
102: this .parent.reset();
103: this .childFetcher.reset();
104:
105: MockXmlObject childOfWrongClass = new MockXmlObject();
106: this .childFetcher.setReturnedChild(childOfWrongClass);
107:
108: try {
109: this .repository.bean();
110: fail("Didn't get TCAE on child of wrong class");
111: } catch (TCAssertionError tcae) {
112: // ok
113: }
114: }
115:
116: public void testListeners() throws Exception {
117: this .repository.addListener(this .listener1);
118: this .repository.addListener(this .listener2);
119:
120: MockXmlObject parent1 = new MockXmlObject();
121: MockXmlObject parent2 = new MockXmlObject();
122: MyMockXmlObject child1 = new MyMockXmlObject();
123: MyMockXmlObject child2 = new MyMockXmlObject();
124:
125: this .parent.setReturnedBean(parent1);
126: this .childFetcher.setReturnedChildren(new XmlObject[] { child1,
127: child2 });
128:
129: this .repository.configurationChanged(parent1, parent2);
130:
131: assertEquals(1, this .listener1.getNumConfigurationChangeds());
132: assertSame(child1, this .listener1.getLastOldConfig());
133: assertSame(child2, this .listener2.getLastNewConfig());
134:
135: assertEquals(2, this .childFetcher.getNumGetChilds());
136: assertEqualsUnordered(new Object[] { parent1, parent2 },
137: this .childFetcher.getLastParents());
138:
139: this .listener1.reset();
140: this .listener2.reset();
141: this .childFetcher.reset();
142:
143: this .childFetcher.setReturnedChildren(new XmlObject[] { child1,
144: child1 });
145:
146: this .repository.configurationChanged(parent1, parent2);
147: assertEquals(2, this .childFetcher.getNumGetChilds());
148: assertEqualsUnordered(new Object[] { parent1, parent2 },
149: this .childFetcher.getLastParents());
150:
151: assertEquals(0, this .listener1.getNumConfigurationChangeds());
152: assertEquals(0, this .listener2.getNumConfigurationChangeds());
153:
154: this .childFetcher.setReturnedChildren(new XmlObject[] { child1,
155: new MockXmlObject() });
156:
157: try {
158: this .repository.configurationChanged(parent1, parent2);
159: fail("Didn't get TCAE on wrong new child class");
160: } catch (TCAssertionError tcae) {
161: // ok
162: }
163: }
164:
165: }
|