package org.limeprotocol.network;

import org.junit.Before;
import org.junit.Test;
import org.limeprotocol.Envelope;

import static org.junit.Assert.assertEquals;

public class TransportListenerBroadcastSenderImplTest {

    private class DummyTransportListener implements Transport.TransportListener{

        int onReceiveCount;
        int onClosingCount;
        int onClosedCount;

        @Override
        public void onReceive(Envelope envelope) {
            onReceiveCount++;
        }

        @Override
        public void onClosing() {
            onClosingCount++;
        }

        @Override
        public void onClosed() {
            onClosedCount++;
        }
    }

    private DummyTransportListener listener1;
    private DummyTransportListener listener2;
    private TransportListenerBroadcastSender listenerBroadcastSender;

    @Before
    public void setUp(){
        listener1 = new DummyTransportListener();
        listener2 = new DummyTransportListener();
        listenerBroadcastSender = new TransportListenerBroadcastSenderImpl();
    }

    @Test
    public void bothListenerAreCalled(){
        listenerBroadcastSender.addListener(listener1);
        listenerBroadcastSender.addListener(listener2);
        listenerBroadcastSender.broadcastOnReceive(null);
        assertEquals(listener1.onReceiveCount, 1);
        assertEquals(listener2.onReceiveCount, 1);
    }

    @Test
    public void listenerAreCalledInTheCorrectOrder(){
        listenerBroadcastSender.addListener(listener1, 5);

        Transport.TransportListener verifyListener = new Transport.TransportListener() {
            @Override
            public void onReceive(Envelope envelope) {
                assertEquals(listener1.onReceiveCount, 1);
            }

            @Override
            public void onClosing() {

            }

            @Override
            public void onClosed() {

            }
        };

        listenerBroadcastSender.addListener(verifyListener, 5);
        listenerBroadcastSender.broadcastOnReceive(null);
    }
}

@@ -1299,32 +1299,35 @@
    assertEquals(
+1, 
 listener1.onRece
@@ -1326,35 +1326,32 @@
 1.onReceiveCount
-, 1
 );%0A        asser
@@ -1350,32 +1350,35 @@
    assertEquals(
+1, 
 listener2.onRece
@@ -1381,27 +1381,24 @@
 ReceiveCount
-, 1
 );%0A    %7D%0A%0A  
@@ -1514,17 +1514,17 @@
 tener1, 
-5
+1
 );%0A%0A    
@@ -1714,16 +1714,19 @@
 tEquals(
+1, 
 listener
@@ -1741,19 +1741,16 @@
 iveCount
-, 1
 );%0A     

Fix test initialization so it pass