KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > listen > ConfigurationChangeListenerSetTest


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.listen;
5
6 import com.tc.config.schema.MockXmlObject;
7 import com.tc.test.TCTestCase;
8
9 /**
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 JavaDoc {
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 JavaDoc {
33     try {
34       this.set.removeListener(null);
35       fail("Didn't get NPE on null listener");
36     } catch (NullPointerException JavaDoc npe) {
37       // ok
38
}
39
40     try {
41       this.set.addListener(null);
42       fail("Didn't get NPE on null listener");
43     } catch (NullPointerException JavaDoc 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, MockXmlObject newConfig) {
78     assertEquals(numOne, this.listener1.getNumConfigurationChangeds());
79     if (numOne > 0) {
80       assertSame(oldConfig, this.listener1.getLastOldConfig());
81       assertSame(newConfig, this.listener1.getLastNewConfig());
82     }
83
84     assertEquals(numTwo, this.listener2.getNumConfigurationChangeds());
85     if (numTwo > 0) {
86       assertSame(oldConfig, this.listener2.getLastOldConfig());
87       assertSame(newConfig, this.listener2.getLastNewConfig());
88     }
89
90     this.listener1.reset();
91     this.listener2.reset();
92   }
93
94 }
95
Popular Tags