package org.jgroups.tests;

import org.jgroups.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.LinkedList;
import java.util.List;

/**
 * Tests unicast functionality
 * @author Bela Ban
 * @version $Id: UnicastUnitTest.java,v 1.5 2008/04/14 08:42:56 belaban Exp $
 */
public class UnicastUnitTest extends ChannelTestBase {
    JChannel ch1, ch2=null;


    @BeforeMethod
    protected void setUp() throws Exception {
        ch1=createChannel();
        ch2=createChannel();
    }

    @AfterMethod
    protected void tearDown() throws Exception {
        if(ch2 != null)
            ch2.close();
        if(ch1 != null)
            ch1.close();
    }



    @Test
    public void testUnicastMessageInCallbackExistingMember() throws Exception {
        ch1.connect("x");
        MyReceiver receiver=new MyReceiver(ch1);
        ch1.setReceiver(receiver);
        ch2.connect("x");
        Exception ex=receiver.getEx();
        if(ex != null)
            throw ex;
        ch1.setReceiver(null);
    }


    private static class MyReceiver extends ReceiverAdapter {
        Channel channel;
        Exception ex;

        public MyReceiver(Channel channel) {
            this.channel=channel;
        }

        public Exception getEx() {
            return ex;
        }

        public void viewAccepted(View new_view) {
            Address local_addr=channel.getLocalAddress();
            assertNotNull(local_addr);
            System.out.println("[" + local_addr + "]: " + new_view);
            List<Address> members=new LinkedList<Address>(new_view.getMembers());
            assertEquals("members=" + members + ", local_addr=" + local_addr, 2, members.size());
            members.remove(local_addr);
            assertEquals(1, members.size());
            Address dest=members.get(0);
            Message unicast_msg=new Message(dest, null, null);
            try {
                // uncomment line below for workaround
                // channel.down(new Event(Event.ENABLE_UNICASTS_TO, dest));
                channel.send(unicast_msg);
            }
            catch(Exception e) {
                ex=e;
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }


}
@@ -321,47 +321,83 @@
 v 1.
-5
+6
  2008/0
-4/14 08:42:56 belaban Exp $%0A */
+6/25 20:07:46 vlada Exp $%0A */%0A@Test(groups=%22temp%22,sequential=false)
 %0Apub
@@ -564,24 +564,30 @@
 eateChannel(
+true,2
 );%0A        c
@@ -603,16 +603,19 @@
 Channel(
+ch1
 );%0A    %7D
@@ -892,33 +892,47 @@
    ch1.connect(%22
-x
+UnicastUnitTest
 %22);%0A        MyRe
@@ -1024,17 +1024,31 @@
 onnect(%22
-x
+UnicastUnitTest
 %22);%0A    
@@ -2395,11 +2395,9 @@
 %7D%0A    %7D%0A
-%0A%0A
 %7D

move to testng temp group