01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.schema.listen;
05:
06: import com.tc.config.schema.MockXmlObject;
07: import com.tc.test.TCTestCase;
08:
09: /**
10: * Unit test for {@link ConfigurationChangeListenerSet}.
11: */
12: public class ConfigurationChangeListenerSetTest extends TCTestCase {
13:
14: private MockConfigurationChangeListener listener1;
15: private MockConfigurationChangeListener listener2;
16:
17: private ConfigurationChangeListenerSet set;
18:
19: private MockXmlObject config1;
20: private MockXmlObject config2;
21:
22: public void setUp() throws Exception {
23: this .listener1 = new MockConfigurationChangeListener();
24: this .listener2 = new MockConfigurationChangeListener();
25:
26: this .set = new ConfigurationChangeListenerSet();
27:
28: this .config1 = new MockXmlObject();
29: this .config2 = new MockXmlObject();
30: }
31:
32: public void testAll() throws Exception {
33: try {
34: this .set.removeListener(null);
35: fail("Didn't get NPE on null listener");
36: } catch (NullPointerException npe) {
37: // ok
38: }
39:
40: try {
41: this .set.addListener(null);
42: fail("Didn't get NPE on null listener");
43: } catch (NullPointerException npe) {
44: // ok
45: }
46:
47: check(0, 0, null, null);
48:
49: this .set.configurationChanged(this .config1, this .config2);
50: check(0, 0, null, null);
51:
52: this .set.addListener(this .listener1);
53: check(0, 0, null, null);
54:
55: this .set.configurationChanged(this .config1, this .config2);
56: check(1, 0, this .config1, this .config2);
57:
58: this .set.addListener(this .listener1);
59: check(0, 0, null, null);
60:
61: this .set.configurationChanged(this .config1, this .config2);
62: check(1, 0, this .config1, this .config2);
63:
64: this .set.addListener(this .listener2);
65: check(0, 0, null, null);
66:
67: this .set.configurationChanged(this .config2, this .config1);
68: check(1, 1, this .config2, this .config1);
69:
70: this .set.removeListener(this .listener1);
71: check(0, 0, null, null);
72:
73: this .set.configurationChanged(this .config1, this .config2);
74: check(0, 1, this .config1, this .config2);
75: }
76:
77: private void check(int numOne, int numTwo, MockXmlObject oldConfig,
78: MockXmlObject newConfig) {
79: assertEquals(numOne, this .listener1
80: .getNumConfigurationChangeds());
81: if (numOne > 0) {
82: assertSame(oldConfig, this .listener1.getLastOldConfig());
83: assertSame(newConfig, this .listener1.getLastNewConfig());
84: }
85:
86: assertEquals(numTwo, this .listener2
87: .getNumConfigurationChangeds());
88: if (numTwo > 0) {
89: assertSame(oldConfig, this.listener2.getLastOldConfig());
90: assertSame(newConfig, this.listener2.getLastNewConfig());
91: }
92:
93: this.listener1.reset();
94: this.listener2.reset();
95: }
96:
97: }
|