package org.jgroups.blocks;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jgroups.JChannel;

public class VotingAdapterTest extends TestCase {

    public static final String SERVER_PROTOCOL_STACK =
            "UDP(mcast_addr=228.3.11.76;mcast_port=12345;ip_ttl=1;"
                    + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000)"
//        + "JMS(topicName=topic/testTopic;cf=UILConnectionFactory;"
//        + "jndiCtx=org.jnp.interfaces.NamingContextFactory;"
//        + "providerURL=localhost;ttl=10000)"
                    + ":PING(timeout=500;num_initial_members=1)"
                    + ":FD"
                    + ":VERIFY_SUSPECT(timeout=1500)"
                    + ":pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800)"
                    + ":FRAG(frag_size=4096)"
                    + ":UNICAST(timeout=5000)"
                    + ":pbcast.STABLE(desired_avg_gossip=200)"
                    + ":pbcast.GMS(join_timeout=5000;join_retry_timeout=1000;"
                    +     "shun=false;print_local_addr=false)"
                    //+ ":SPEED_LIMIT(down_queue_limit=10)"
                    + ":pbcast.STATE_TRANSFER"
            ;


    public VotingAdapterTest(String testName) {
            super(testName);
    }

    public static Test suite() {
            return new TestSuite(VotingAdapterTest.class);
    }

    
    private JChannel channel1;
    private JChannel channel2;

    protected VotingAdapter adapter1;
    protected VotingAdapter adapter2;

    protected TestVoteChannelListener listener1;
    protected TestVoteChannelListener listener2;
    protected TestVoteChannelListener listener3;
    protected TestVoteChannelListener listener4;

    protected static boolean logConfigured=false;

    public void setUp() throws Exception {
        super.setUp();
        listener1=new TestVoteChannelListener(true);
        listener2=new TestVoteChannelListener(true);
        listener3=new TestVoteChannelListener(false);
        listener4=new TestVoteChannelListener(false);

        channel1=new JChannel(SERVER_PROTOCOL_STACK);
        adapter1=new VotingAdapter(channel1);

        channel1.connect("voting");

        // give some time for the channel to become a coordinator
        try {
            Thread.sleep(1000);
        }
        catch(Exception ex) {
        }

        channel2=new JChannel(SERVER_PROTOCOL_STACK);
        adapter2=new VotingAdapter(channel2);

        channel2.connect("voting");

        try {
            Thread.sleep(1000);
        }
        catch(InterruptedException ex) {
        }
    }

    public void tearDown() throws Exception {
        channel2.close();

        try {
            Thread.sleep(1000);
        }
        catch(InterruptedException ex) {
        }


        channel1.close();
        super.tearDown();
    }

    public void testVoteAll() throws Exception {
    
        adapter1.addVoteListener(listener1);
        adapter2.addVoteListener(listener2);

        boolean voting1 = adapter1.vote("object1", VotingAdapter.VOTE_ALL, 1000);
    
        assertTrue("Result of voting1 should be 'true'.", voting1);

        adapter1.addVoteListener(listener3);

        boolean voting2 = adapter1.vote("object2", VotingAdapter.VOTE_ALL, 1000);
    
        assertTrue("Result of voting2 should be 'false'.", !voting2);
        
    }

    /**
     * This class always vote according to the parameter passed on the
     * object creation.
     */
    public static class TestVoteChannelListener implements VotingListener {
            private boolean vote;

            public TestVoteChannelListener(boolean vote) {
                    this.vote = vote;
            }

            public boolean vote(Object decree) {
                    return vote;
            }
    }




    public static void main(String[] args) {
	junit.textui.TestRunner.run(suite());
    }
}

@@ -82,46 +82,42 @@
 Test
-Cas
+Suit
 e;%0A
+%0A
 import 
-junit.framework.TestSuite
+org.jgroups.Channel
 ;%0Aim
@@ -133,24 +133,37 @@
 jgroups.
-J
+tests.
 Channel
+TestBase
 ;%0A%0Apubli
@@ -200,1058 +200,25 @@
 nds 
-TestCase %7B%0A%0A    public static final String SERVER_PROTOCOL_STACK =%0A            %22UDP(mcast_addr=228.3.11.76;mcast_port=12345;ip_ttl=1;%22%0A                    + %22mcast_send_buf_size=150000;mcast_recv_buf_size=80000)%22%0A//        + %22JMS(topicName=topic/testTopic;cf=UILConnectionFactory;%22%0A//        + %22jndiCtx=org.jnp.interfaces.NamingContextFactory;%22%0A//        + %22providerURL=localhost;ttl=10000)%22%0A                    + %22:PING(timeout=500;num_initial_members=1)%22%0A                    + %22:FD%22%0A                    + %22:VERIFY_SUSPECT(timeout=1500)%22%0A                    + %22:pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800)%22%0A                    + %22:FRAG(frag_size=4096)%22%0A                    + %22:UNICAST(timeout=5000)%22%0A                    + %22:pbcast.STABLE(desired_avg_gossip=200)%22%0A                    + %22:pbcast.GMS(join_timeout=5000;join_retry_timeout=1000;%22%0A                    +     %22shun=false;print_local_addr=false)%22%0A                    //+ %22:SPEED_LIMIT(down_queue_limit=10)%22%0A                    + %22:pbcast.STATE_TRANSFER%22%0A            ;%0A
+ChannelTestBase %7B
 %0A%0A  
@@ -407,33 +407,32 @@
    %0A    private 
-J
 Channel channel1
@@ -445,17 +445,16 @@
 private 
-J
 Channel 
@@ -1087,29 +1087,30 @@
 hannel1=
-new J
+create
 Channel(
 SERVER_P
@@ -1101,37 +1101,19 @@
 Channel(
-SERVER_PROTOCOL_STACK
+%22A%22
 );%0A     
@@ -1375,21 +1375,22 @@
 el2=
-new J
+create
 Channel(
 SERV
@@ -1389,29 +1389,11 @@
 nel(
-SERVER_PROTOCOL_STACK
+%22A%22
 );%0A 

upgrade to new test framework