KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > groups > TribesGroupManagerTest


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.net.groups;
6
7 import com.tc.test.TCTestCase;
8 import com.tc.util.concurrent.NoExceptionLinkedQueue;
9
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInput JavaDoc;
12 import java.io.ObjectOutput JavaDoc;
13
14 public class TribesGroupManagerTest extends TCTestCase {
15
16   public void testIfTribesGroupManagerLoads() throws Exception JavaDoc {
17     GroupManager gm = GroupManagerFactory.createGroupManager();
18     assertNotNull(gm);
19     assertEquals(TribesGroupManager.class.getName(), gm.getClass().getName());
20   }
21
22   public void testSendingReceivingMessages() throws Exception JavaDoc {
23     TribesGroupManager gm1 = new TribesGroupManager();
24     MyListener l1 = new MyListener();
25     gm1.registerForMessages(TestMessage.class, l1);
26     NodeID n1 = gm1.join();
27
28     TribesGroupManager gm2 = new TribesGroupManager();
29     MyListener l2 = new MyListener();
30     gm2.registerForMessages(TestMessage.class, l2);
31     NodeID n2 = gm2.join();
32     
33     assertNotEquals(n1, n2);
34
35     TestMessage m1 = new TestMessage("Hello there");
36     gm1.sendAll(m1);
37
38     TestMessage m2 = (TestMessage) l2.take();
39     System.err.println(m2);
40     
41     assertEquals(m1, m2);
42     
43     TestMessage m3 = new TestMessage("Hello back");
44     gm2.sendAll(m3);
45
46     TestMessage m4 = (TestMessage) l1.take();
47     System.err.println(m4);
48     
49     assertEquals(m3, m4);
50     
51   }
52
53   private static final class MyListener implements GroupMessageListener {
54
55     NoExceptionLinkedQueue queue = new NoExceptionLinkedQueue();
56
57     public void messageReceived(NodeID fromNode, GroupMessage msg) {
58       queue.put(msg);
59     }
60
61     public GroupMessage take() {
62       return (GroupMessage) queue.take();
63     }
64
65   }
66
67   private static final class TestMessage extends AbstractGroupMessage {
68
69     // to make serialization sane
70
public TestMessage() {
71       super(0);
72     }
73     
74     public TestMessage(String JavaDoc message) {
75       super(0);
76       this.msg = message;
77     }
78
79     String JavaDoc msg;
80
81     @Override JavaDoc
82     protected void basicReadExternal(int msgType, ObjectInput JavaDoc in) throws IOException JavaDoc {
83       msg = in.readUTF();
84
85     }
86
87     @Override JavaDoc
88     protected void basicWriteExternal(int msgType, ObjectOutput JavaDoc out) throws IOException JavaDoc {
89       out.writeUTF(msg);
90
91     }
92     
93     public int hashCode() {
94       return msg.hashCode();
95     }
96     
97     public boolean equals(Object JavaDoc o) {
98       if (o instanceof TestMessage) {
99         TestMessage other = (TestMessage) o;
100         return this.msg.equals(other.msg);
101       }
102       return false;
103     }
104
105     public String JavaDoc toString() {
106       return "TestMessage [ " + msg + "]";
107     }
108   }
109 }
110
Popular Tags