Source Code Cross Referenced for ClusterTest.java in  » Net » Terracotta » com » tc » cluster » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.cluster 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003:         * notice. All rights reserved.
004:         */
005:        package com.tc.cluster;
006:
007:        import com.tc.test.TCTestCase;
008:
009:        import java.util.Arrays;
010:        import java.util.Map;
011:
012:        public class ClusterTest extends TCTestCase {
013:
014:            private Cluster cluster;
015:            private TestEventListener cel;
016:
017:            protected void setUp() throws Exception {
018:                cluster = new Cluster();
019:                cel = new TestEventListener();
020:            }
021:
022:            public void testGetThisNode() {
023:                final String this NodeId = "xxxyyyzzz";
024:                cluster.this NodeConnected(this NodeId,
025:                        new String[] { "someNode" });
026:                assertNotNull(cluster.getThisNode());
027:                assertEquals(this NodeId, cluster.getThisNode().getNodeId());
028:            }
029:
030:            public void testGetNodes() {
031:                // 0
032:                Map nodes = cluster.getNodes();
033:                assertNotNull(nodes);
034:                assertTrue(nodes.isEmpty());
035:
036:                // 1
037:                final String this NodeId = "1";
038:                cluster.this NodeConnected(this NodeId,
039:                        new String[] { this NodeId });
040:                nodes = cluster.getNodes();
041:                assertNotNull(nodes);
042:                assertEquals(1, nodes.size());
043:                assertNotNull(nodes.get(this NodeId));
044:            }
045:
046:            public void testThisNodeConnected() {
047:                final String this NodeId = "1";
048:                final String[] nodeIds = new String[] { this NodeId };
049:                cluster.this NodeConnected(this NodeId, nodeIds);
050:
051:                // if this node is connected, adding a cel should result in an immediate callback
052:                cluster.addClusterEventListener(cel);
053:                assertEquals("thisNodeConnected", cel.getLastMethodCalled());
054:                assertEquals(this NodeId, cel.getLastString());
055:                assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
056:
057:                // should not fire multiple thisNodeConnected events in a row...
058:                cel.reset();
059:                cluster.this NodeConnected(this NodeId, nodeIds);
060:                assertNull(cel.getLastMethodCalled());
061:
062:                // but it should be fired after thisNodeDisconnected...
063:                cluster.this NodeDisconnected();
064:                cluster.this NodeConnected(this NodeId, nodeIds);
065:                assertEquals("thisNodeConnected", cel.getLastMethodCalled());
066:                assertEquals(this NodeId, cel.getLastString());
067:                assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
068:
069:                // no callback if cel is added again
070:                cel.reset();
071:                cluster.addClusterEventListener(cel);
072:                assertNull(cel.getLastMethodCalled());
073:                assertNull(cel.getLastString());
074:                assertNull(cel.getLastStringArray());
075:            }
076:
077:            public void testThisNodeDisconnected() {
078:                final String this NodeId = "1";
079:                final String[] nodeIds = new String[] { this NodeId };
080:                cluster.this NodeConnected(this NodeId, nodeIds);
081:
082:                cluster.addClusterEventListener(cel);
083:
084:                assertEquals("thisNodeConnected", cel.getLastMethodCalled());
085:                assertEquals(this NodeId, cel.getLastString());
086:                assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
087:
088:                cluster.this NodeDisconnected();
089:                assertEquals("thisNodeDisconnected", cel.getLastMethodCalled());
090:                cel.reset();
091:                cluster.this NodeDisconnected();
092:                assertNull(cel.getLastMethodCalled());
093:            }
094:
095:            public void testNodeConnected() {
096:                // prime cluster
097:                final String this NodeId = "1";
098:                final String[] nodeIds = new String[] { this NodeId };
099:                cluster.this NodeConnected(this NodeId, nodeIds);
100:                cluster.addClusterEventListener(cel);
101:                assertEquals("thisNodeConnected", cel.getLastMethodCalled());
102:                assertEquals(this NodeId, cel.getLastString());
103:                assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
104:                cel.reset();
105:
106:                // now cause nodeConnected event
107:                final String newNodeId = "2";
108:                cluster.nodeConnected(newNodeId);
109:                assertEquals("nodeConnected", cel.getLastMethodCalled());
110:                assertEquals(newNodeId, cel.getLastString());
111:                assertNull(cel.getLastStringArray());
112:                cel.reset();
113:
114:                // now cause node disconnected event
115:                cluster.nodeDisconnected(newNodeId);
116:                assertEquals("nodeDisconnected", cel.getLastMethodCalled());
117:                assertEquals(newNodeId, cel.getLastString());
118:                assertNull(cel.getLastStringArray());
119:                cel.reset();
120:
121:            }
122:
123:            public void testAddSameListenerTwice() {
124:                final String this NodeId = "1";
125:                final String[] nodesCurrentlyInCluster = new String[] { this NodeId };
126:                cluster.this NodeConnected(this NodeId, nodesCurrentlyInCluster);
127:
128:                TestEventListener listener = new TestEventListener();
129:                cluster.addClusterEventListener(listener);
130:
131:                assertEquals("thisNodeConnected", listener
132:                        .getLastMethodCalled());
133:
134:                listener.reset();
135:                cluster.addClusterEventListener(listener);
136:
137:                assertNull(listener.getLastMethodCalled());
138:            }
139:
140:            public void testCallbackOnluOnNewListener() {
141:                final String this NodeId = "1";
142:                final String[] nodesCurrentlyInCluster = new String[] { this NodeId };
143:                cluster.this NodeConnected(this NodeId, nodesCurrentlyInCluster);
144:
145:                TestEventListener listener = new TestEventListener();
146:                cluster.addClusterEventListener(listener);
147:
148:                assertEquals("thisNodeConnected", listener
149:                        .getLastMethodCalled());
150:
151:                listener.reset();
152:                TestEventListener listener2 = new TestEventListener();
153:                cluster.addClusterEventListener(listener2);
154:
155:                assertNull(listener.getLastMethodCalled());
156:                assertEquals("thisNodeConnected", listener2
157:                        .getLastMethodCalled());
158:            }
159:
160:            public void testClientExceptionSafety() {
161:                final String this NodeId = "1";
162:                final String[] nodesCurrentlyInCluster = new String[] { this NodeId };
163:                cluster.this NodeConnected(this NodeId, nodesCurrentlyInCluster);
164:
165:                cluster
166:                        .addClusterEventListener(new TestEventListenerWithExceptions());
167:
168:                cluster.this NodeDisconnected();
169:                cluster.nodeConnected("2");
170:                cluster.nodeDisconnected(this NodeId);
171:            }
172:
173:            private static class TestEventListener implements 
174:                    ClusterEventListener {
175:
176:                private String lastString = null;
177:                private String[] lastStringArray = null;
178:                private String lastMethodCalled = null;
179:
180:                public void nodeConnected(String nodeId) {
181:                    lastString = nodeId;
182:                    lastMethodCalled = "nodeConnected";
183:                }
184:
185:                public void nodeDisconnected(String nodeId) {
186:                    lastString = nodeId;
187:                    lastMethodCalled = "nodeDisconnected";
188:                }
189:
190:                public void this NodeConnected(String this NodeId,
191:                        String[] nodesCurrentlyInCluster) {
192:                    lastString = this NodeId;
193:                    lastStringArray = nodesCurrentlyInCluster;
194:                    lastMethodCalled = "thisNodeConnected";
195:                }
196:
197:                public void this NodeDisconnected(String this NodeId) {
198:                    lastString = this NodeId;
199:                    lastMethodCalled = "thisNodeDisconnected";
200:                }
201:
202:                public String getLastMethodCalled() {
203:                    return lastMethodCalled;
204:                }
205:
206:                public String getLastString() {
207:                    return lastString;
208:                }
209:
210:                public String[] getLastStringArray() {
211:                    return lastStringArray;
212:                }
213:
214:                public void reset() {
215:                    lastString = null;
216:                    lastStringArray = null;
217:                    lastMethodCalled = null;
218:                }
219:
220:            }
221:
222:            private static class TestEventListenerWithExceptions implements 
223:                    ClusterEventListener {
224:
225:                public void nodeConnected(String nodeId) {
226:                    throw new RuntimeException("nodeConnected");
227:                }
228:
229:                public void nodeDisconnected(String nodeId) {
230:                    throw new RuntimeException("nodeDisconnected");
231:                }
232:
233:                public void this NodeConnected(String this NodeId,
234:                        String[] nodesCurrentlyInCluster) {
235:                    throw new RuntimeException("thisNodeConnected");
236:                }
237:
238:                public void this NodeDisconnected(String this NodeId) {
239:                    throw new RuntimeException("thisNodeDisconnected");
240:                }
241:            }
242:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.