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