1 5 package com.tc.cluster; 6 7 import com.tc.test.TCTestCase; 8 9 import java.util.Arrays ; 10 import java.util.Map ; 11 12 public class ClusterTest extends TCTestCase { 13 14 private Cluster cluster; 15 private TestEventListener cel; 16 17 protected void setUp() throws Exception { 18 cluster = new Cluster(); 19 cel = new TestEventListener(); 20 } 21 22 public void testGetThisNode() { 23 final String thisNodeId = "xxxyyyzzz"; 24 cluster.thisNodeConnected(thisNodeId, new String [] { "someNode" }); 25 assertNotNull(cluster.getThisNode()); 26 assertEquals(thisNodeId, cluster.getThisNode().getNodeId()); 27 } 28 29 public void testGetNodes() { 30 Map nodes = cluster.getNodes(); 32 assertNotNull(nodes); 33 assertTrue(nodes.isEmpty()); 34 35 final String thisNodeId = "1"; 37 cluster.thisNodeConnected(thisNodeId, new String [] { 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 thisNodeId = "1"; 46 final String [] nodeIds = new String [] { thisNodeId }; 47 cluster.thisNodeConnected(thisNodeId, nodeIds); 48 49 cluster.addClusterEventListener(cel); 51 assertEquals("thisNodeConnected", cel.getLastMethodCalled()); 52 assertEquals(thisNodeId, cel.getLastString()); 53 assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray())); 54 55 cel.reset(); 57 cluster.thisNodeConnected(thisNodeId, nodeIds); 58 assertNull(cel.getLastMethodCalled()); 59 60 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 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 thisNodeId = "1"; 78 final String [] nodeIds = new String [] { 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 final String thisNodeId = "1"; 97 final String [] nodeIds = new String [] { 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 final String 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 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 thisNodeId = "1"; 124 final String [] nodesCurrentlyInCluster = new String [] { 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 thisNodeId = "1"; 140 final String [] nodesCurrentlyInCluster = new String [] { 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 thisNodeId = "1"; 158 final String [] nodesCurrentlyInCluster = new String [] { 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 lastString = null; 171 private String [] lastStringArray = null; 172 private String lastMethodCalled = null; 173 174 public void nodeConnected(String nodeId) { 175 lastString = nodeId; 176 lastMethodCalled = "nodeConnected"; 177 } 178 179 public void nodeDisconnected(String nodeId) { 180 lastString = nodeId; 181 lastMethodCalled = "nodeDisconnected"; 182 } 183 184 public void thisNodeConnected(String thisNodeId, String [] nodesCurrentlyInCluster) { 185 lastString = thisNodeId; 186 lastStringArray = nodesCurrentlyInCluster; 187 lastMethodCalled = "thisNodeConnected"; 188 } 189 190 public void thisNodeDisconnected(String thisNodeId) { 191 lastString = thisNodeId; 192 lastMethodCalled = "thisNodeDisconnected"; 193 } 194 195 public String getLastMethodCalled() { 196 return lastMethodCalled; 197 } 198 199 public String getLastString() { 200 return lastString; 201 } 202 203 public String [] 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 nodeId) { 218 throw new RuntimeException ("nodeConnected"); 219 } 220 221 public void nodeDisconnected(String nodeId) { 222 throw new RuntimeException ("nodeDisconnected"); 223 } 224 225 public void thisNodeConnected(String thisNodeId, String [] nodesCurrentlyInCluster) { 226 throw new RuntimeException ("thisNodeConnected"); 227 } 228 229 public void thisNodeDisconnected(String thisNodeId) { 230 throw new RuntimeException ("thisNodeDisconnected"); 231 } 232 } 233 } | Popular Tags |