KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > cluster > ClusterTest


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

5 package com.tc.cluster;
6
7 import com.tc.test.TCTestCase;
8
9 import java.util.Arrays JavaDoc;
10 import java.util.Map JavaDoc;
11
12 public class ClusterTest extends TCTestCase {
13
14   private Cluster cluster;
15   private TestEventListener cel;
16
17   protected void setUp() throws Exception JavaDoc {
18     cluster = new Cluster();
19     cel = new TestEventListener();
20   }
21
22   public void testGetThisNode() {
23     final String JavaDoc thisNodeId = "xxxyyyzzz";
24     cluster.thisNodeConnected(thisNodeId, new String JavaDoc[] { "someNode" });
25     assertNotNull(cluster.getThisNode());
26     assertEquals(thisNodeId, cluster.getThisNode().getNodeId());
27   }
28
29   public void testGetNodes() {
30     // 0
31
Map JavaDoc nodes = cluster.getNodes();
32     assertNotNull(nodes);
33     assertTrue(nodes.isEmpty());
34
35     // 1
36
final String JavaDoc thisNodeId = "1";
37     cluster.thisNodeConnected(thisNodeId, new String JavaDoc[] { thisNodeId });
38     nodes = cluster.getNodes();
39     assertNotNull(nodes);
40     assertEquals(1, nodes.size());
41     assertNotNull(nodes.get(thisNodeId));
42   }
43
44   public void testThisNodeConnected() {
45     final String JavaDoc thisNodeId = "1";
46     final String JavaDoc[] nodeIds = new String JavaDoc[] { thisNodeId };
47     cluster.thisNodeConnected(thisNodeId, nodeIds);
48
49     // if this node is connected, adding a cel should result in an immediate callback
50
cluster.addClusterEventListener(cel);
51     assertEquals("thisNodeConnected", cel.getLastMethodCalled());
52     assertEquals(thisNodeId, cel.getLastString());
53     assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
54     
55     // should not fire multiple thisNodeConnected events in a row...
56
cel.reset();
57     cluster.thisNodeConnected(thisNodeId, nodeIds);
58     assertNull(cel.getLastMethodCalled());
59     
60     // but it should be fired after thisNodeDisconnected...
61
cluster.thisNodeDisconnected();
62     cluster.thisNodeConnected(thisNodeId, nodeIds);
63     assertEquals("thisNodeConnected", cel.getLastMethodCalled());
64     assertEquals(thisNodeId, cel.getLastString());
65     assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
66     
67
68     // no callback if cel is added again
69
cel.reset();
70     cluster.addClusterEventListener(cel);
71     assertNull(cel.getLastMethodCalled());
72     assertNull(cel.getLastString());
73     assertNull(cel.getLastStringArray());
74   }
75
76   public void testThisNodeDisconnected() {
77     final String JavaDoc thisNodeId = "1";
78     final String JavaDoc[] nodeIds = new String JavaDoc[] { thisNodeId };
79     cluster.thisNodeConnected(thisNodeId, nodeIds);
80
81     cluster.addClusterEventListener(cel);
82
83     assertEquals("thisNodeConnected", cel.getLastMethodCalled());
84     assertEquals(thisNodeId, cel.getLastString());
85     assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
86
87     cluster.thisNodeDisconnected();
88     assertEquals("thisNodeDisconnected", cel.getLastMethodCalled());
89     cel.reset();
90     cluster.thisNodeDisconnected();
91     assertNull(cel.getLastMethodCalled());
92   }
93
94   public void testNodeConnected() {
95     // prime cluster
96
final String JavaDoc thisNodeId = "1";
97     final String JavaDoc[] nodeIds = new String JavaDoc[] { thisNodeId };
98     cluster.thisNodeConnected(thisNodeId, nodeIds);
99     cluster.addClusterEventListener(cel);
100     assertEquals("thisNodeConnected", cel.getLastMethodCalled());
101     assertEquals(thisNodeId, cel.getLastString());
102     assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
103     cel.reset();
104
105     // now cause nodeConnected event
106
final String JavaDoc newNodeId = "2";
107     cluster.nodeConnected(newNodeId);
108     assertEquals("nodeConnected", cel.getLastMethodCalled());
109     assertEquals(newNodeId, cel.getLastString());
110     assertNull(cel.getLastStringArray());
111     cel.reset();
112
113     // now cause node disconnected event
114
cluster.nodeDisconnected(newNodeId);
115     assertEquals("nodeDisconnected", cel.getLastMethodCalled());
116     assertEquals(newNodeId, cel.getLastString());
117     assertNull(cel.getLastStringArray());
118     cel.reset();
119
120   }
121
122   public void testAddSameListenerTwice() {
123     final String JavaDoc thisNodeId = "1";
124     final String JavaDoc[] nodesCurrentlyInCluster = new String JavaDoc[] { thisNodeId };
125     cluster.thisNodeConnected(thisNodeId, nodesCurrentlyInCluster);
126
127     TestEventListener listener = new TestEventListener();
128     cluster.addClusterEventListener(listener);
129
130     assertEquals("thisNodeConnected", listener.getLastMethodCalled());
131
132     listener.reset();
133     cluster.addClusterEventListener(listener);
134
135     assertNull(listener.getLastMethodCalled());
136   }
137
138   public void testCallbackOnluOnNewListener() {
139     final String JavaDoc thisNodeId = "1";
140     final String JavaDoc[] nodesCurrentlyInCluster = new String JavaDoc[] { thisNodeId };
141     cluster.thisNodeConnected(thisNodeId, nodesCurrentlyInCluster);
142
143     TestEventListener listener = new TestEventListener();
144     cluster.addClusterEventListener(listener);
145
146     assertEquals("thisNodeConnected", listener.getLastMethodCalled());
147
148     listener.reset();
149     TestEventListener listener2 = new TestEventListener();
150     cluster.addClusterEventListener(listener2);
151
152     assertNull(listener.getLastMethodCalled());
153     assertEquals("thisNodeConnected", listener2.getLastMethodCalled());
154   }
155
156   public void testClientExceptionSafety() {
157     final String JavaDoc thisNodeId = "1";
158     final String JavaDoc[] nodesCurrentlyInCluster = new String JavaDoc[] { thisNodeId };
159     cluster.thisNodeConnected(thisNodeId, nodesCurrentlyInCluster);
160
161     cluster.addClusterEventListener(new TestEventListenerWithExceptions());
162
163     cluster.thisNodeDisconnected();
164     cluster.nodeConnected("2");
165     cluster.nodeDisconnected(thisNodeId);
166   }
167
168   private static class TestEventListener implements ClusterEventListener {
169
170     private String JavaDoc lastString = null;
171     private String JavaDoc[] lastStringArray = null;
172     private String JavaDoc lastMethodCalled = null;
173
174     public void nodeConnected(String JavaDoc nodeId) {
175       lastString = nodeId;
176       lastMethodCalled = "nodeConnected";
177     }
178
179     public void nodeDisconnected(String JavaDoc nodeId) {
180       lastString = nodeId;
181       lastMethodCalled = "nodeDisconnected";
182     }
183
184     public void thisNodeConnected(String JavaDoc thisNodeId, String JavaDoc[] nodesCurrentlyInCluster) {
185       lastString = thisNodeId;
186       lastStringArray = nodesCurrentlyInCluster;
187       lastMethodCalled = "thisNodeConnected";
188     }
189
190     public void thisNodeDisconnected(String JavaDoc thisNodeId) {
191       lastString = thisNodeId;
192       lastMethodCalled = "thisNodeDisconnected";
193     }
194
195     public String JavaDoc getLastMethodCalled() {
196       return lastMethodCalled;
197     }
198
199     public String JavaDoc getLastString() {
200       return lastString;
201     }
202
203     public String JavaDoc[] getLastStringArray() {
204       return lastStringArray;
205     }
206
207     public void reset() {
208       lastString = null;
209       lastStringArray = null;
210       lastMethodCalled = null;
211     }
212
213   }
214
215   private static class TestEventListenerWithExceptions implements ClusterEventListener {
216
217     public void nodeConnected(String JavaDoc nodeId) {
218       throw new RuntimeException JavaDoc("nodeConnected");
219     }
220
221     public void nodeDisconnected(String JavaDoc nodeId) {
222       throw new RuntimeException JavaDoc("nodeDisconnected");
223     }
224
225     public void thisNodeConnected(String JavaDoc thisNodeId, String JavaDoc[] nodesCurrentlyInCluster) {
226       throw new RuntimeException JavaDoc("thisNodeConnected");
227     }
228
229     public void thisNodeDisconnected(String JavaDoc thisNodeId) {
230       throw new RuntimeException JavaDoc("thisNodeDisconnected");
231     }
232   }
233 }
Popular Tags