KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > VotingAdapterTest


1 package org.jgroups.blocks;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5
6 import org.jgroups.Channel;
7 import org.jgroups.tests.ChannelTestBase;
8
9 public class VotingAdapterTest extends ChannelTestBase {
10
11     public VotingAdapterTest(String JavaDoc testName) {
12             super(testName);
13     }
14
15     public static Test suite() {
16             return new TestSuite(VotingAdapterTest.class);
17     }
18
19     
20     private Channel channel1;
21     private Channel channel2;
22
23     protected VotingAdapter adapter1;
24     protected VotingAdapter adapter2;
25
26     protected TestVoteChannelListener listener1;
27     protected TestVoteChannelListener listener2;
28     protected TestVoteChannelListener listener3;
29     protected TestVoteChannelListener listener4;
30
31     protected static boolean logConfigured=false;
32
33     public void setUp() throws Exception JavaDoc {
34         super.setUp();
35         listener1=new TestVoteChannelListener(true);
36         listener2=new TestVoteChannelListener(true);
37         listener3=new TestVoteChannelListener(false);
38         listener4=new TestVoteChannelListener(false);
39
40         channel1=createChannel("A");
41         adapter1=new VotingAdapter(channel1);
42
43         channel1.connect("voting");
44
45         // give some time for the channel to become a coordinator
46
try {
47             Thread.sleep(1000);
48         }
49         catch(Exception JavaDoc ex) {
50         }
51
52         channel2=createChannel("A");
53         adapter2=new VotingAdapter(channel2);
54
55         channel2.connect("voting");
56
57         try {
58             Thread.sleep(1000);
59         }
60         catch(InterruptedException JavaDoc ex) {
61         }
62     }
63
64     public void tearDown() throws Exception JavaDoc {
65         channel2.close();
66
67         try {
68             Thread.sleep(1000);
69         }
70         catch(InterruptedException JavaDoc ex) {
71         }
72
73
74         channel1.close();
75         super.tearDown();
76     }
77
78     public void testVoteAll() throws Exception JavaDoc {
79     
80         adapter1.addVoteListener(listener1);
81         adapter2.addVoteListener(listener2);
82
83         boolean voting1 = adapter1.vote("object1", VotingAdapter.VOTE_ALL, 1000);
84     
85         assertTrue("Result of voting1 should be 'true'.", voting1);
86
87         adapter1.addVoteListener(listener3);
88
89         boolean voting2 = adapter1.vote("object2", VotingAdapter.VOTE_ALL, 1000);
90     
91         assertTrue("Result of voting2 should be 'false'.", !voting2);
92         
93     }
94
95     /**
96      * This class always vote according to the parameter passed on the
97      * object creation.
98      */

99     public static class TestVoteChannelListener implements VotingListener {
100             private boolean vote;
101
102             public TestVoteChannelListener(boolean vote) {
103                     this.vote = vote;
104             }
105
106             public boolean vote(Object JavaDoc decree) {
107                     return vote;
108             }
109     }
110
111
112
113
114     public static void main(String JavaDoc[] args) {
115     junit.textui.TestRunner.run(suite());
116     }
117 }
118
Popular Tags