/*
 * $Id: TestLcapSocket.java,v 1.4 2002-11-19 23:26:16 tal Exp $
 */

/*

Copyright (c) 2002 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.protocol;

import java.util.*;
import java.io.*;
import java.net.*;
import junit.framework.TestCase;
import org.lockss.daemon.*;
import org.lockss.util.*;
import org.lockss.test.*;


/**
 * This is the test class for org.lockss.protocol.TestLcapSocket
 */

public class TestLcapSocket extends LockssTestCase{
  public static Class testedClasses[] = {
    org.lockss.protocol.LcapSocket.class
  };


  public TestLcapSocket(String msg){
    super(msg);
  }

  String testStr = "This is test data";
  byte[] testData = testStr.getBytes();
  int testPort = 1234;
  InetAddress testAddr;
  DatagramPacket testPacket;

  public void setUp() throws Exception {
    testAddr = InetAddress.getByName("127.0.0.1");
    testPacket =
      new DatagramPacket(testData, testData.length, testAddr, testPort);
  }

  /** Sender sends a packet to a mock datagram socket in a while */
  class Sender extends DoLater {
    MockDatagramSocketExtras sock;
    DatagramPacket pkt;

    Sender(long waitMs, MockDatagramSocketExtras sock, DatagramPacket pkt) {
      super(waitMs);
      this.sock = sock;
      this.pkt = pkt;
    }

    protected void doit() {
      sock.addToReceiveQueue(pkt);
    }
  }

  /** Put something onto a queue in a while */
  private Sender sendIn(long ms, MockDatagramSocketExtras sock,
			DatagramPacket pkt) {
    Sender p = new Sender(ms, sock, pkt);
    p.start();
    return p;
  }

  public void testSend() throws Exception {
    MockDatagramSocket dskt = new MockDatagramSocket();
    LcapSocket skt = new LcapSocket(dskt);
    assertTrue(dskt.getSentPackets().isEmpty());
    skt.send(testPacket);
    DatagramPacket sent = (DatagramPacket)dskt.getSentPackets().elementAt(0);
    assertEquals(testPacket, sent);
  }
  
  public void testMulticastReceive() throws Exception {
    Queue rcvQ = new FifoQueue();
    MockMulticastSocket mskt = new MockMulticastSocket();
    LcapSocket.Multicast lskt = new LcapSocket.Multicast(rcvQ, mskt,
							 (InetAddress)null);
    mskt.addToReceiveQueue(testPacket);
    Interrupter intr = interruptMeIn(500);
    PrivilegedAccessor.invokeMethod(lskt, "receivePacket");
    assertTrue(!rcvQ.isEmpty());
    LockssDatagram rcvd = (LockssDatagram)rcvQ.get(Deadline.in(0));
    assertEquals(testPacket, rcvd.getPacket());
    intr.cancel();
  }
}


@@ -32,29 +32,29 @@
 v 1.
-4
+5
  2002-1
-1-19 23:26:16
+2-02 00:43:08
  tal
@@ -3173,16 +3173,605 @@
 %0A  %7D%0A  %0A
+  static Logger log = Logger.getLogger(%22SockTest%22);%0A%0A  public void testUnicastReceive() throws Exception %7B%0A    Queue rcvQ = new FifoQueue();%0A    MockDatagramSocket dskt = new MockDatagramSocket();%0A    LcapSocket.Unicast lskt = new LcapSocket.Unicast(rcvQ, dskt);%0A    dskt.addToReceiveQueue(testPacket);%0A    Interrupter intr;%0A    intr = interruptMeIn(500);%0A    PrivilegedAccessor.invokeMethod(lskt, %22receivePacket%22);%0A    assertTrue(!rcvQ.isEmpty());%0A    LockssDatagram rcvd = (LockssDatagram)rcvQ.get(Deadline.in(0));%0A    assertEquals(testPacket, rcvd.getPacket());%0A    intr.cancel();%0A  %7D%0A%0A
   public

Test unicast receive.