KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > UnicastEnableToTest


1 package org.jgroups.tests;
2
3 import junit.framework.TestCase;
4 import org.jgroups.*;
5 import org.jgroups.stack.IpAddress;
6 import org.jgroups.util.Util;
7
8 import java.util.LinkedList JavaDoc;
9 import java.util.List JavaDoc;
10
11 /**
12  * Tests sending of unicasts to members not in the group (http://jira.jboss.com/jira/browse/JGRP-357)
13  * @author Bela Ban
14  * @version $Id: UnicastEnableToTest.java,v 1.1 2007/03/09 20:27:05 belaban Exp $
15  */

16 public class UnicastEnableToTest extends TestCase {
17     JChannel channel=null, channel2=null;
18
19
20     public UnicastEnableToTest(String JavaDoc name) {
21         super(name);
22     }
23
24     protected void setUp() throws Exception JavaDoc {
25         super.setUp();
26         channel=new JChannel("udp.xml");
27         channel.connect("demo-group");
28     }
29
30     protected void tearDown() throws Exception JavaDoc {
31         super.tearDown();
32         if(channel2 != null) {
33             channel2.close();
34             channel2=null;
35         }
36         if(channel != null) {
37             channel.close();
38             channel=null;
39         }
40     }
41
42
43     public void testUnicastMessageToUnknownMember() throws Exception JavaDoc {
44         IpAddress addr=new IpAddress("127.0.0.1", 8976);
45         System.out.println("sending message to non-existing destination " + addr);
46         try {
47             channel.send(new Message(addr, null, "Hello world"));
48             fail("we should not get here; sending of message to " + addr + " should have failed");
49         }
50         catch(IllegalArgumentException JavaDoc ex) {
51             System.out.println("received exception as expected");
52         }
53     }
54
55
56     public void testUnicastMessageToExistingMember() throws Exception JavaDoc {
57         channel2=new JChannel("udp.xml");
58         channel2.connect("demo-group");
59         assertEquals(2, channel2.getView().size());
60         MyReceiver receiver=new MyReceiver();
61         channel2.setReceiver(receiver);
62         Address dest=channel2.getLocalAddress();
63         channel.send(new Message(dest, null, "hello"));
64         Util.sleep(500);
65         List JavaDoc list=receiver.getMsgs();
66         System.out.println("channel2 received the following msgs: " + list);
67         assertEquals(1, list.size());
68         receiver.reset();
69     }
70
71
72     public void testUnicastMessageToLeftMember() throws Exception JavaDoc {
73         channel2=new JChannel("udp.xml");
74         channel2.connect("demo-group");
75         assertEquals(2, channel2.getView().size());
76         Address dest=channel2.getLocalAddress();
77         channel2.close();
78         Util.sleep(100);
79         try {
80             channel.send(new Message(dest, null, "hello"));
81             fail("we should not come here as message to previous member " + dest + " should throw exception");
82         }
83         catch(IllegalArgumentException JavaDoc ex) {
84             System.out.println("got an exception, as expected");
85         }
86     }
87
88
89     public void testUnicastMessageToLeftMemberWithEnableUnicastToEvent() throws Exception JavaDoc {
90         channel2=new JChannel("udp.xml");
91         channel2.connect("demo-group");
92         assertEquals(2, channel2.getView().size());
93         Address dest=channel2.getLocalAddress();
94         channel2.close();
95         Util.sleep(100);
96         channel.down(new Event(Event.ENABLE_UNICASTS_TO, dest));
97         channel.send(new Message(dest, null, "hello"));
98     }
99
100
101     private static class MyReceiver extends ExtendedReceiverAdapter {
102         List JavaDoc<Message> msgs=new LinkedList JavaDoc<Message>();
103
104         public void receive(Message msg) {
105             msgs.add(msg);
106         }
107
108         List JavaDoc getMsgs() {
109             return msgs;
110         }
111
112         void reset() {msgs.clear();}
113     }
114
115
116     public static void main(String JavaDoc[] args) {
117         String JavaDoc[] testCaseName={UnicastEnableToTest.class.getName()};
118         junit.textui.TestRunner.main(testCaseName);
119     }
120 }
121
Popular Tags