KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > basic > GlobalTransactionTest


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.tests.basic;
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 /**
20  * @author <a HREF="mailto:bela@jboss.org">Bela Ban</a> Apr 14, 2003
21  * @version $Id: GlobalTransactionTest.java,v 1.1.1.1 2005/03/31 10:15:08 belaban Exp $
22  */

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