KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > 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;
8
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.jgroups.stack.IpAddress;
14
15 import java.io.ByteArrayInputStream JavaDoc;
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.ObjectInputStream JavaDoc;
19 import java.io.ObjectOutputStream JavaDoc;
20 import java.net.UnknownHostException JavaDoc;
21
22
23 /**
24  * @author <a HREF="mailto:bela@jboss.org">Bela Ban</a> Apr 14, 2003
25  * @version $Id: GlobalTransactionTest.java,v 1.8 2006/12/06 13:10:31 msurtani Exp $
26  */

27 public class GlobalTransactionTest extends TestCase
28 {
29
30    public GlobalTransactionTest(String JavaDoc name)
31    {
32       super(name);
33    }
34
35
36    public void testEquality() throws UnknownHostException JavaDoc
37    {
38       IpAddress a1 = new IpAddress("localhost", 4444);
39       GlobalTransaction tx1, tx2;
40
41       tx1 = GlobalTransaction.create(a1);
42       tx2 = GlobalTransaction.create(a1);
43
44       System.out.println("\ntx1: " + tx1 + "\ntx2: " + tx2);
45       assertTrue(tx1.equals(tx2) == false);
46
47       tx2 = tx1;
48       assertTrue(tx1.equals(tx2));
49
50    }
51
52    public void testEqualityWithOtherObject() throws UnknownHostException JavaDoc
53    {
54       IpAddress a1 = new IpAddress("localhost", 4444);
55       GlobalTransaction tx1 = GlobalTransaction.create(a1);
56       System.out.println("\ntx1: " + tx1);
57       assertFalse(tx1.equals(Thread.currentThread()));
58    }
59
60    public void testEqualityWithNull() throws UnknownHostException JavaDoc
61    {
62       IpAddress a1 = new IpAddress("localhost", 4444);
63       GlobalTransaction tx1 = GlobalTransaction.create(a1);
64       System.out.println("\ntx1: " + tx1);
65       assertFalse(tx1.equals(null));
66    }
67
68    public void testHashcode() throws UnknownHostException JavaDoc
69    {
70       IpAddress a1 = new IpAddress("localhost", 4444);
71       GlobalTransaction tx1, tx2;
72
73
74       tx1 = GlobalTransaction.create(a1);
75       tx2 = GlobalTransaction.create(a1);
76
77       System.out.println("\ntx1: " + tx1 + "\ntx2: " + tx2);
78       assertTrue(tx1.equals(tx2) == false);
79
80       int hcode_1 = tx1.hashCode();
81       int hcode_2 = tx2.hashCode();
82       assertFalse(hcode_1 == hcode_2);
83
84       tx2 = tx1;
85       assertTrue(tx1.equals(tx2));
86       hcode_1 = tx1.hashCode();
87       hcode_2 = tx2.hashCode();
88       assertEquals(hcode_1, hcode_2);
89    }
90
91
92    public void testExternalization() throws UnknownHostException JavaDoc
93    {
94       IpAddress a1 = new IpAddress("localhost", 4444);
95       IpAddress a2 = new IpAddress("localhost", 5555);
96       GlobalTransaction tx1, tx2, tx1_copy = null, tx2_copy = null;
97       ByteArrayOutputStream JavaDoc bos = null;
98       ByteArrayInputStream JavaDoc bis = null;
99       ObjectOutputStream JavaDoc out = null;
100       ObjectInputStream JavaDoc in = null;
101       byte[] buf = null;
102
103       tx1 = GlobalTransaction.create(a1);
104       tx2 = GlobalTransaction.create(a2);
105
106       try
107       {
108          bos = new ByteArrayOutputStream JavaDoc(1024);
109          out = new ObjectOutputStream JavaDoc(bos);
110          out.writeObject(tx1);
111          out.writeObject(tx2);
112          out.flush();
113          buf = bos.toByteArray();
114       }
115       catch (IOException JavaDoc ex)
116       {
117          fail("creation of output stream");
118       }
119
120       try
121       {
122          bis = new ByteArrayInputStream JavaDoc(buf);
123          in = new ObjectInputStream JavaDoc(bis);
124          tx1_copy = (GlobalTransaction) in.readObject();
125          tx2_copy = (GlobalTransaction) in.readObject();
126       }
127       catch (IOException JavaDoc ex)
128       {
129          fail("creation of input stream");
130       }
131       catch (ClassNotFoundException JavaDoc e)
132       {
133          e.printStackTrace();
134          fail();
135       }
136
137       System.out.println("\ntx1: " + tx1 + ", tx1_copy: " + tx1_copy +
138               "\ntx2: " + tx2 + ", tx2_copy: " + tx2_copy);
139
140       assertNotNull(tx1_copy);
141       assertNotNull(tx2_copy);
142       assertEquals(tx1, tx1_copy);
143       assertEquals(tx2, tx2_copy);
144
145       int hcode_1 = tx1.hashCode();
146       int hcode_2 = tx2.hashCode();
147       int hcode_3 = tx1_copy.hashCode();
148       int hcode_4 = tx2_copy.hashCode();
149       assertFalse(hcode_1 == hcode_2);
150       assertFalse(hcode_3 == hcode_4);
151       assertEquals(hcode_1, hcode_3);
152       assertEquals(hcode_2, hcode_4);
153    }
154
155
156    public void testWithNullAddress()
157    {
158       GlobalTransaction tx1, tx2, tmp_tx1;
159
160       tx1 = GlobalTransaction.create(null);
161       tx2 = GlobalTransaction.create(null);
162
163       tmp_tx1 = tx1;
164       assertEquals(tx1, tmp_tx1);
165       assertTrue(tx1.equals(tx2) == false);
166    }
167
168    public void testOneNullAddress() throws UnknownHostException JavaDoc
169    {
170       GlobalTransaction tx1, tx2;
171       tx1 = GlobalTransaction.create(null);
172
173       assertFalse(tx1.equals(null));
174
175       tx2 = GlobalTransaction.create(null);
176
177       assertFalse(tx1.equals(tx2));
178       assertFalse(tx2.equals(tx1));
179
180       IpAddress a1 = new IpAddress("localhost", 4444);
181       tx2 = GlobalTransaction.create(a1);
182
183       assertFalse(tx1.equals(tx2));
184       assertFalse(tx2.equals(tx1));
185    }
186
187
188    void log(String JavaDoc msg)
189    {
190       System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
191    }
192
193    public static Test suite()
194    {
195       TestSuite s = new TestSuite(GlobalTransactionTest.class);
196       return s;
197    }
198
199    public static void main(String JavaDoc[] args)
200    {
201       junit.textui.TestRunner.run(suite());
202    }
203
204 }
205
Popular Tags