/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache;


import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jgroups.stack.IpAddress;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.UnknownHostException;


/**
 * @author <a href="mailto:bela@jboss.org">Bela Ban</a> Apr 14, 2003
 * @version $Id: GlobalTransactionTest.java 3104 2006-12-06 13:12:08Z msurtani $
 */
public class GlobalTransactionTest extends TestCase {

   public GlobalTransactionTest(String name) {
      super(name);
   }



   public void testEquality() throws UnknownHostException
   {
      IpAddress a1=new IpAddress("localhost", 4444);
      GlobalTransaction tx1, tx2;

      tx1=GlobalTransaction.create(a1);
      tx2=GlobalTransaction.create(a1);

      System.out.println("\ntx1: " + tx1 + "\ntx2: " + tx2);
      assertTrue(tx1.equals(tx2) == false);

      tx2=tx1;
      assertTrue(tx1.equals(tx2));

   }

   public void testEqualityWithOtherObject() throws UnknownHostException
   {
      IpAddress a1=new IpAddress("localhost", 4444);
      GlobalTransaction tx1=GlobalTransaction.create(a1);
      System.out.println("\ntx1: " + tx1);
      assertFalse(tx1.equals(Thread.currentThread()));
   }

   public void testEqualityWithNull() throws UnknownHostException
   {
      IpAddress a1=new IpAddress("localhost", 4444);
      GlobalTransaction tx1=GlobalTransaction.create(a1);
      System.out.println("\ntx1: " + tx1);
      assertFalse(tx1.equals(null));
   }

   public void testHashcode() throws UnknownHostException
   {
      IpAddress a1=new IpAddress("localhost", 4444);
      GlobalTransaction tx1, tx2;


      tx1=GlobalTransaction.create(a1);
      tx2=GlobalTransaction.create(a1);

      System.out.println("\ntx1: " + tx1 + "\ntx2: " + tx2);
      assertTrue(tx1.equals(tx2) == false);

      int hcode_1=tx1.hashCode();
      int hcode_2=tx2.hashCode();
      assertFalse(hcode_1 == hcode_2);

      tx2=tx1;
      assertTrue(tx1.equals(tx2));
      hcode_1=tx1.hashCode();
      hcode_2=tx2.hashCode();
      assertEquals(hcode_1, hcode_2);
   }


   public void testExternalization() throws UnknownHostException
   {
      IpAddress a1=new IpAddress("localhost", 4444);
      IpAddress a2=new IpAddress("localhost", 5555);
      GlobalTransaction tx1, tx2, tx1_copy=null, tx2_copy=null;
      ByteArrayOutputStream bos=null;
      ByteArrayInputStream bis=null;
      ObjectOutputStream out=null;
      ObjectInputStream in=null;
      byte[] buf=null;

      tx1=GlobalTransaction.create(a1);
      tx2=GlobalTransaction.create(a2);

      try {
         bos=new ByteArrayOutputStream(1024);
         out=new ObjectOutputStream(bos);
         out.writeObject(tx1);
         out.writeObject(tx2);
         out.flush();
         buf=bos.toByteArray();
      }
      catch(IOException ex) {
         fail("creation of output stream");
      }

      try {
         bis=new ByteArrayInputStream(buf);
         in=new ObjectInputStream(bis);
         tx1_copy=(GlobalTransaction)in.readObject();
         tx2_copy=(GlobalTransaction)in.readObject();
      }
      catch(IOException ex) {
         fail("creation of input stream");
      }
      catch(ClassNotFoundException e) {
         e.printStackTrace();
         fail();
      }

      System.out.println("\ntx1: " + tx1 + ", tx1_copy: " + tx1_copy +
                         "\ntx2: " + tx2 + ", tx2_copy: " + tx2_copy);

      assertNotNull(tx1_copy);
      assertNotNull(tx2_copy);
      assertEquals(tx1, tx1_copy);
      assertEquals(tx2, tx2_copy);

      int hcode_1=tx1.hashCode();
      int hcode_2=tx2.hashCode();
      int hcode_3=tx1_copy.hashCode();
      int hcode_4=tx2_copy.hashCode();
      assertFalse(hcode_1 == hcode_2);
      assertFalse(hcode_3 == hcode_4);
      assertEquals(hcode_1, hcode_3);
      assertEquals(hcode_2, hcode_4);
   }


   public void testWithNullAddress() {
      GlobalTransaction tx1, tx2, tmp_tx1;

      tx1=GlobalTransaction.create(null);
      tx2=GlobalTransaction.create(null);

      tmp_tx1=tx1;
      assertEquals(tx1, tmp_tx1);
      assertTrue(tx1.equals(tx2) == false);
   }

   public void testOneNullAddress() throws UnknownHostException
   {
      GlobalTransaction tx1, tx2;
      tx1 = GlobalTransaction.create(null);

      assertFalse(tx1.equals(null));

      tx2 = GlobalTransaction.create(null);

      assertFalse(tx1.equals(tx2));
      assertFalse(tx2.equals(tx1));

      IpAddress a1 = new IpAddress("localhost", 4444);
      tx2 = GlobalTransaction.create(a1);

      assertFalse(tx1.equals(tx2));
      assertFalse(tx2.equals(tx1));
   }


   void log(String msg) {
      System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
   }

   public static Test suite() {
      TestSuite s=new TestSuite(GlobalTransactionTest.class);
      return s;
   }

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

}
