1 4 package com.tc.object.msg; 5 6 import com.tc.bytes.TCByteBuffer; 7 import com.tc.exception.ImplementMe; 8 import com.tc.io.TCByteBufferInputStream; 9 import com.tc.io.TCByteBufferOutputStream; 10 import com.tc.net.protocol.tcm.NullMessageMonitor; 11 import com.tc.net.protocol.tcm.TCMessageHeader; 12 import com.tc.net.protocol.tcm.TCMessageType; 13 import com.tc.object.dna.impl.ObjectStringSerializer; 14 import com.tc.object.session.SessionID; 15 import com.tc.object.tx.TransactionBatch; 16 import com.tc.object.tx.TransactionID; 17 import com.tc.test.TCTestCase; 18 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.HashSet ; 22 import java.util.Random ; 23 import java.util.Set ; 24 25 28 public class CommitTransactionMessageTest extends TCTestCase { 29 30 public void testMessage() throws Exception { 31 Random rnd = new Random (); 32 33 for (int i = 0; i < 100; i++) { 34 int len = rnd.nextInt(40960); 35 36 byte orig[] = new byte[len]; 37 rnd.nextBytes(orig); 38 39 TCByteBufferOutputStream bbos = new TCByteBufferOutputStream(); 40 bbos.write(orig); 41 42 Set acknowledged = new HashSet (); 43 for (int j = 0; j < 10; j++) { 44 acknowledged.add(new TransactionID(j)); 45 } 46 47 TransactionBatch batch = new TestTransactionBatch(bbos.toArray(), acknowledged); 48 49 CommitTransactionMessageImpl msg = new CommitTransactionMessageImpl(new NullMessageMonitor(), 50 new TCByteBufferOutputStream(4, 4096, false), 51 null, 52 TCMessageType.COMMIT_TRANSACTION_MESSAGE); 53 ObjectStringSerializer serializer = new ObjectStringSerializer(); 54 msg.setBatch(batch, serializer); 55 msg.dehydrate(); 56 57 CommitTransactionMessageImpl msg2 = new CommitTransactionMessageImpl(SessionID.NULL_ID, new NullMessageMonitor(), null, 58 (TCMessageHeader) msg.getHeader(), msg 59 .getPayload()); 60 msg2.hydrate(); 61 62 assertEquals(acknowledged, msg2.addAcknowledgedTransactionIDsTo(new HashSet ())); 63 64 TCByteBufferInputStream bbis = new TCByteBufferInputStream(msg2.getBatchData()); 65 byte[] compare = new byte[orig.length]; 66 int read = bbis.read(compare); 67 assertEquals(compare.length, read); 68 assertTrue(Arrays.equals(orig, compare)); 69 } 70 71 } 72 73 private static class TestTransactionBatch implements TransactionBatch { 74 75 private final TCByteBuffer[] batchData; 76 private final Collection acknowledged; 77 78 public TestTransactionBatch(TCByteBuffer[] batchData, Collection acknowledged) { 79 this.batchData = batchData; 80 this.acknowledged = acknowledged; 81 } 82 83 public Collection getAcknowledgedTransactionIDs() { 84 return this.acknowledged; 85 } 86 87 public boolean isEmpty() { 88 throw new ImplementMe(); 89 } 90 91 public TCByteBuffer[] getData() { 92 return batchData; 93 } 94 95 public void recycle() { 96 return; 97 } 98 99 } 100 } 101 | Popular Tags |