001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.l2.msg;
006:
007: import com.tc.bytes.TCByteBuffer;
008: import com.tc.bytes.TCByteBufferFactory;
009: import com.tc.net.groups.ClientID;
010: import com.tc.net.protocol.tcm.ChannelID;
011: import com.tc.object.dna.impl.ObjectStringSerializer;
012: import com.tc.object.gtx.GlobalTransactionID;
013: import com.tc.object.msg.TestTransactionBatch;
014: import com.tc.object.tx.ServerTransactionID;
015: import com.tc.object.tx.TransactionID;
016: import com.tc.object.tx.TxnBatchID;
017: import com.tc.objectserver.tx.TestCommitTransactionMessage;
018: import com.tc.objectserver.tx.TestCommitTransactionMessageFactory;
019: import com.tc.objectserver.tx.TestServerTransaction;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.ObjectInput;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutput;
026: import java.io.ObjectOutputStream;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import junit.framework.TestCase;
032:
033: public class RelayedCommitTransactionMessageTest extends TestCase {
034:
035: private TestCommitTransactionMessage testCommitTransactionMessage;
036: private List transactions;
037: private List serverTransactionIDs;
038: private final int channelId = 2;
039:
040: public void setUp() {
041: testCommitTransactionMessage = (TestCommitTransactionMessage) new TestCommitTransactionMessageFactory()
042: .newCommitTransactionMessage();
043: testCommitTransactionMessage.setBatch(new TestTransactionBatch(
044: new TCByteBuffer[] { TCByteBufferFactory.getInstance(
045: false, 3452) }), new ObjectStringSerializer());
046: testCommitTransactionMessage.setChannelID(new ClientID(
047: new ChannelID(channelId)));
048:
049: serverTransactionIDs = new ArrayList();
050: ClientID cid = new ClientID(new ChannelID(channelId));
051: ServerTransactionID stid1 = new ServerTransactionID(cid,
052: new TransactionID(4234));
053: ServerTransactionID stid2 = new ServerTransactionID(cid,
054: new TransactionID(6543));
055: ServerTransactionID stid3 = new ServerTransactionID(cid,
056: new TransactionID(1654));
057: ServerTransactionID stid4 = new ServerTransactionID(cid,
058: new TransactionID(3460));
059: serverTransactionIDs.add(stid1);
060: serverTransactionIDs.add(stid2);
061: serverTransactionIDs.add(stid3);
062: serverTransactionIDs.add(stid4);
063:
064: transactions = new ArrayList();
065: transactions.add(new TestServerTransaction(stid1,
066: new TxnBatchID(32), new GlobalTransactionID(23)));
067: transactions.add(new TestServerTransaction(stid2,
068: new TxnBatchID(12), new GlobalTransactionID(54)));
069: transactions.add(new TestServerTransaction(stid3,
070: new TxnBatchID(43), new GlobalTransactionID(55)));
071: transactions.add(new TestServerTransaction(stid4,
072: new TxnBatchID(9), new GlobalTransactionID(78)));
073: }
074:
075: public void tearDown() {
076: testCommitTransactionMessage = null;
077: transactions = null;
078: serverTransactionIDs = null;
079: }
080:
081: private void validate(RelayedCommitTransactionMessage rctm,
082: RelayedCommitTransactionMessage rctm1) {
083: assertEquals(rctm.getType(), rctm1.getType());
084: assertEquals(rctm.getMessageID(), rctm1.getMessageID());
085: assertEquals(rctm.inResponseTo(), rctm1.inResponseTo());
086: assertEquals(rctm.messageFrom(), rctm1.messageFrom());
087:
088: assertEquals(rctm.getClientID(), rctm1.getClientID());
089:
090: GlobalTransactionID lwm = rctm
091: .getLowGlobalTransactionIDWatermark();
092: GlobalTransactionID lwm1 = rctm1
093: .getLowGlobalTransactionIDWatermark();
094: assertEquals(lwm, lwm1);
095:
096: for (Iterator iter = serverTransactionIDs.iterator(); iter
097: .hasNext();) {
098: ServerTransactionID serverTransactionID = (ServerTransactionID) iter
099: .next();
100: assertEquals(
101: rctm
102: .getOrCreateGlobalTransactionID(serverTransactionID),
103: rctm1
104: .getOrCreateGlobalTransactionID(serverTransactionID));
105: }
106:
107: TCByteBuffer[] tcbb = rctm.getBatchData();
108: TCByteBuffer[] tcbb1 = rctm1.getBatchData();
109: assertEquals(tcbb.length, tcbb1.length);
110: for (int i = 0; i < tcbb.length; i++) {
111: assertEquals(tcbb[i].getBoolean(), tcbb1[i].getBoolean());
112: assertEquals(tcbb[i].hasArray(), tcbb1[i].hasArray());
113: assertEquals(tcbb[i].hasRemaining(), tcbb1[i]
114: .hasRemaining());
115: assertEquals(tcbb[i].isDirect(), tcbb1[i].isDirect());
116: assertEquals(tcbb[i].isReadOnly(), tcbb1[i].isReadOnly());
117: byte[] byteArray = tcbb[i].array();
118: byte[] byteArray1 = tcbb1[i].array();
119: assertEquals(byteArray.length, byteArray1.length);
120: for (int j = 0; j < byteArray.length; j++) {
121: assertEquals(byteArray[j], byteArray1[j]);
122: }
123: assertEquals(tcbb[i].arrayOffset(), tcbb1[i].arrayOffset());
124: assertEquals(tcbb[i].capacity(), tcbb1[i].capacity());
125: assertEquals(tcbb[i].get(), tcbb1[i].get());
126: assertEquals(tcbb[i].getChar(), tcbb1[i].getChar());
127: assertEquals(tcbb[i].getInt(), tcbb1[i].getInt());
128: assertEquals(tcbb[i].getLong(), tcbb1[i].getLong());
129: assertEquals(tcbb[i].getShort(), tcbb1[i].getShort());
130: assertEquals(tcbb[i].getUbyte(), tcbb1[i].getUbyte());
131: assertEquals(tcbb[i].getUint(), tcbb1[i].getUint());
132: assertEquals(tcbb[i].getUshort(), tcbb1[i].getUshort());
133: assertEquals(tcbb[i].limit(), tcbb1[i].limit());
134: assertEquals(tcbb[i].position(), tcbb1[i].position());
135: assertEquals(tcbb[i].remaining(), tcbb1[i].remaining());
136:
137: assertEquals(rctm.getSequenceID(), rctm1.getSequenceID());
138: }
139: }
140:
141: private RelayedCommitTransactionMessage writeAndRead(
142: RelayedCommitTransactionMessage rctm) throws Exception {
143: ByteArrayOutputStream bo = new ByteArrayOutputStream();
144: ObjectOutput oo = new ObjectOutputStream(bo);
145: oo.writeObject(rctm);
146: System.err.println("Written : " + rctm);
147: ByteArrayInputStream bi = new ByteArrayInputStream(bo
148: .toByteArray());
149: ObjectInput oi = new ObjectInputStream(bi);
150: RelayedCommitTransactionMessage rctm1 = (RelayedCommitTransactionMessage) oi
151: .readObject();
152: System.err.println("Read : " + rctm1);
153: return rctm1;
154: }
155:
156: public void testBasicSerialization() throws Exception {
157: RelayedCommitTransactionMessage rctm = RelayedCommitTransactionMessageFactory
158: .createRelayedCommitTransactionMessage(
159: testCommitTransactionMessage, transactions,
160: 420, new GlobalTransactionID(49));
161: RelayedCommitTransactionMessage rctm1 = writeAndRead(rctm);
162: validate(rctm, rctm1);
163: }
164: }
|