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.AbstractGroupMessage;
010: import com.tc.net.groups.ClientID;
011: import com.tc.net.groups.NodeID;
012: import com.tc.net.groups.NodeIDImpl;
013: import com.tc.net.protocol.tcm.ChannelID;
014: import com.tc.object.dna.impl.ObjectStringSerializer;
015: import com.tc.object.gtx.GlobalTransactionID;
016: import com.tc.object.msg.TestTransactionBatch;
017: import com.tc.object.tx.ServerTransactionID;
018: import com.tc.object.tx.TransactionID;
019: import com.tc.object.tx.TxnBatchID;
020: import com.tc.objectserver.tx.TestCommitTransactionMessage;
021: import com.tc.objectserver.tx.TestCommitTransactionMessageFactory;
022: import com.tc.objectserver.tx.TestServerTransaction;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInput;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutput;
029: import java.io.ObjectOutputStream;
030: import java.util.ArrayList;
031: import java.util.HashSet;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Set;
035:
036: import junit.framework.TestCase;
037:
038: public class ServerTxnAckMessageTest extends TestCase {
039: private AbstractGroupMessage relayedCommitTransactionMessage;
040: private Set serverTransactionIDs;
041: private final int channelId = 2;
042: private final NodeID nodeID = new NodeIDImpl("foo", "foobar"
043: .getBytes());
044:
045: public void setUp() {
046: TestCommitTransactionMessage testCommitTransactionMessage = (TestCommitTransactionMessage) new TestCommitTransactionMessageFactory()
047: .newCommitTransactionMessage();
048: testCommitTransactionMessage.setBatch(new TestTransactionBatch(
049: new TCByteBuffer[] { TCByteBufferFactory.getInstance(
050: false, 3452) }), new ObjectStringSerializer());
051: testCommitTransactionMessage.setChannelID(new ClientID(
052: new ChannelID(channelId)));
053:
054: serverTransactionIDs = new HashSet();
055: ClientID cid = new ClientID(new ChannelID(channelId));
056: ServerTransactionID stid1 = new ServerTransactionID(cid,
057: new TransactionID(4234));
058: ServerTransactionID stid2 = new ServerTransactionID(cid,
059: new TransactionID(6543));
060: ServerTransactionID stid3 = new ServerTransactionID(cid,
061: new TransactionID(1654));
062: ServerTransactionID stid4 = new ServerTransactionID(cid,
063: new TransactionID(3460));
064: serverTransactionIDs.add(stid1);
065: serverTransactionIDs.add(stid2);
066: serverTransactionIDs.add(stid3);
067: serverTransactionIDs.add(stid4);
068:
069: List transactions = new ArrayList();
070: transactions.add(new TestServerTransaction(stid1,
071: new TxnBatchID(32), new GlobalTransactionID(23)));
072: transactions.add(new TestServerTransaction(stid2,
073: new TxnBatchID(12), new GlobalTransactionID(54)));
074: transactions.add(new TestServerTransaction(stid3,
075: new TxnBatchID(43), new GlobalTransactionID(55)));
076: transactions.add(new TestServerTransaction(stid4,
077: new TxnBatchID(9), new GlobalTransactionID(78)));
078:
079: relayedCommitTransactionMessage = RelayedCommitTransactionMessageFactory
080: .createRelayedCommitTransactionMessage(
081: testCommitTransactionMessage, transactions,
082: 700, new GlobalTransactionID(99));
083: relayedCommitTransactionMessage.setMessageOrginator(nodeID);
084: }
085:
086: public void tearDown() {
087: relayedCommitTransactionMessage = null;
088: serverTransactionIDs = null;
089: }
090:
091: private void validate(ServerTxnAckMessage stam,
092: ServerTxnAckMessage stam1) {
093: assertEquals(stam.getType(), stam1.getType());
094: assertEquals(stam.getMessageID(), stam1.getMessageID());
095: assertEquals(stam.inResponseTo(), stam1.inResponseTo());
096: assertEquals(stam.messageFrom(), stam1.messageFrom());
097:
098: Set acked = stam.getAckedServerTxnIDs();
099: Set acked1 = stam1.getAckedServerTxnIDs();
100: assertEquals(acked.size(), acked1.size());
101: for (Iterator iter = acked.iterator(); iter.hasNext();) {
102: ServerTransactionID stid = (ServerTransactionID) iter
103: .next();
104: assertTrue(acked1.contains(stid));
105: acked1.remove(stid);
106: }
107: assertTrue(acked1.isEmpty());
108:
109: assertEquals(stam.getDestinationID(), nodeID);
110: }
111:
112: private ServerTxnAckMessage writeAndRead(ServerTxnAckMessage stam)
113: throws Exception {
114: ByteArrayOutputStream bo = new ByteArrayOutputStream();
115: ObjectOutput oo = new ObjectOutputStream(bo);
116: oo.writeObject(stam);
117: System.err.println("Written : " + stam);
118: ByteArrayInputStream bi = new ByteArrayInputStream(bo
119: .toByteArray());
120: ObjectInput oi = new ObjectInputStream(bi);
121: ServerTxnAckMessage stam1 = (ServerTxnAckMessage) oi
122: .readObject();
123: System.err.println("Read : " + stam1);
124: return stam1;
125: }
126:
127: public void testBasicSerialization() throws Exception {
128: ServerTxnAckMessage stam = ServerTxnAckMessageFactory
129: .createServerTxnAckMessage(
130: relayedCommitTransactionMessage,
131: serverTransactionIDs);
132: ServerTxnAckMessage stam1 = writeAndRead(stam);
133: validate(stam, stam1);
134: }
135: }
|