    1   /*
    2    * Created on Apr 30, 2004
    3    *
    4    * TODO To change the template for this generated file go to Window -
    5    * Preferences - Java - Code Generation - Code and Comments
    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  		// If xid2 is made local it should be the same as the original
    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  		// Now test to see if xid2 and testXid are the same.
    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  }