KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > msg > CommitTransactionMessageTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Random JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * @author steve
27  */

28 public class CommitTransactionMessageTest extends TCTestCase {
29
30   public void testMessage() throws Exception JavaDoc {
31     Random JavaDoc rnd = new Random JavaDoc();
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 JavaDoc acknowledged = new HashSet JavaDoc();
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 JavaDoc()));
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 JavaDoc acknowledged;
77
78     public TestTransactionBatch(TCByteBuffer[] batchData, Collection JavaDoc acknowledged) {
79       this.batchData = batchData;
80       this.acknowledged = acknowledged;
81     }
82
83     public Collection JavaDoc 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