1 7 package org.jboss.test.cache.test.generic; 8 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.jboss.cache.GlobalTransaction; 14 import org.jgroups.stack.IpAddress; 15 16 import java.io.*; 17 18 19 23 public class GlobalTransactionUnitTestCase extends TestCase { 24 25 public GlobalTransactionUnitTestCase(String name) { 26 super(name); 27 } 28 29 protected void setUp() throws Exception { 30 } 31 32 protected void tearDown() throws Exception { 33 } 34 35 36 public void testEquality() { 37 IpAddress a1=new IpAddress("localhost", 4444); 38 GlobalTransaction tx1, tx2; 39 40 tx1=GlobalTransaction.create(a1); 41 tx2=GlobalTransaction.create(a1); 42 43 System.out.println("\ntx1: " + tx1 + "\ntx2: " + tx2); 44 assertTrue(tx1.equals(tx2) == false); 45 46 tx2=tx1; 47 assertTrue(tx1.equals(tx2)); 48 } 49 50 public void testEqualityWithOtherObject() { 51 IpAddress a1=new IpAddress("localhost", 4444); 52 GlobalTransaction tx1=GlobalTransaction.create(a1); 53 System.out.println("\ntx1: " + tx1); 54 assertFalse(tx1.equals(Thread.currentThread())); 55 } 56 57 58 public void testExternalization() { 59 IpAddress a1=new IpAddress("localhost", 4444); 60 IpAddress a2=new IpAddress("localhost", 5555); 61 GlobalTransaction tx1, tx2, tx1_copy=null, tx2_copy=null; 62 ByteArrayOutputStream bos=null; 63 ByteArrayInputStream bis=null; 64 ObjectOutputStream out=null; 65 ObjectInputStream in=null; 66 byte[] buf=null; 67 68 tx1=GlobalTransaction.create(a1); 69 tx2=GlobalTransaction.create(a2); 70 71 try { 72 bos=new ByteArrayOutputStream(1024); 73 out=new ObjectOutputStream(bos); 74 out.writeObject(tx1); 75 out.writeObject(tx2); 76 out.flush(); 77 buf=bos.toByteArray(); 78 } 79 catch(IOException ex) { 80 fail("creation of output stream"); 81 } 82 83 try { 84 bis=new ByteArrayInputStream(buf); 85 in=new ObjectInputStream(bis); 86 tx1_copy=(GlobalTransaction)in.readObject(); 87 tx2_copy=(GlobalTransaction)in.readObject(); 88 } 89 catch(IOException ex) { 90 fail("creation of input stream"); 91 } 92 catch(ClassNotFoundException e) { 93 e.printStackTrace(); 94 fail(); 95 } 96 97 System.out.println("\ntx1: " + tx1 + ", tx1_copy: " + tx1_copy + 98 "\ntx2: " + tx2 + ", tx2_copy: " + tx2_copy); 99 100 assertNotNull(tx1_copy); 101 assertNotNull(tx2_copy); 102 assertEquals(tx1, tx1_copy); 103 assertEquals(tx2, tx2_copy); 104 } 105 106 107 public void testWithNullAddress() { 108 GlobalTransaction tx1, tx2, tmp_tx1; 109 110 tx1=GlobalTransaction.create(null); 111 tx2=GlobalTransaction.create(null); 112 113 tmp_tx1=tx1; 114 assertEquals(tx1, tmp_tx1); 115 assertTrue(tx1.equals(tx2) == false); 116 } 117 118 119 void sleep(long timeout) { 120 try { 121 Thread.sleep(timeout); 122 } 123 catch(InterruptedException e) { 124 } 125 } 126 127 void log(String msg) { 128 System.out.println("-- [" + Thread.currentThread() + "]: " + msg); 129 } 130 131 public static Test suite() { 132 TestSuite s=new TestSuite(GlobalTransactionUnitTestCase.class); 133 return s; 134 } 135 136 public static void main(String [] args) { 137 junit.textui.TestRunner.run(suite()); 138 } 139 140 } 141 | Popular Tags |