1
2
3
4
5
6
7 package org.codehaus.activemq.message;
8 import javax.jms.JMSException;
9 import javax.transaction.xa.Xid;
10 import junit.framework.TestCase;
11
12 /***
13 * @version $Revision: 1.2 $
14 */
15 public class ActiveMQXidTest extends TestCase {
16 final byte branch[] = new byte[256];
17 final byte global[] = new byte[256];
18 final int format = 101;
19 private Xid testXid;
20
21 protected void setUp() throws Exception {
22 for (int i = 0; i < branch.length; i++) {
23 branch[i] = (byte) i;
24 }
25 for (int i = 0; i < global.length; i++) {
26 global[i] = (byte) i;
27 }
28 testXid = new Xid() {
29 public byte[] getBranchQualifier() {
30 return branch;
31 }
32 public int getFormatId() {
33 return format;
34 }
35 public byte[] getGlobalTransactionId() {
36 return global;
37 }
38 };
39 }
40
41 public void testEncodeDecode() throws JMSException {
42 ActiveMQXid xid1 = new ActiveMQXid(testXid);
43 String localxid = xid1.toLocalTransactionId();
44 System.out.println(localxid);
45
46 ActiveMQXid xid2 = new ActiveMQXid(localxid);
47
48
49 assertTrue( localxid.equals(xid2.toLocalTransactionId()));
50
51 assertEquals("CompareTo should match", 0, xid1.compareTo(xid2));
52 assertEquals("equals should match", xid1, xid2);
53
54
55 assertTrue( xid2.getFormatId() == testXid.getFormatId() );
56 assertSameBytes( xid2.getBranchQualifier(), testXid.getBranchQualifier() );
57 assertSameBytes( xid2.getGlobalTransactionId(), testXid.getGlobalTransactionId() );
58 }
59
60 /***
61 * @param bs
62 * @param bs2
63 */
64 protected void assertSameBytes(byte[] bs, byte[] bs2) {
65 assertFalse( bs==null ^ bs2==null );
66 if( bs==null )
67 return;
68 assertTrue( bs.length == bs2.length );
69 for (int i = 0; i < bs.length; i++) {
70 assertTrue( bs[i] == bs2[i]);
71 }
72 }
73 }