KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > impl > PersistentBatchSequenceProviderTest


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

5 package com.tc.objectserver.persistence.impl;
6
7 import com.tc.exception.ImplementMe;
8 import com.tc.objectserver.api.TestSink;
9 import com.tc.objectserver.persistence.api.PersistentSequence;
10 import com.tc.objectserver.persistence.impl.PersistentBatchSequenceProvider.GlobalTransactionIDBatchRequestContext;
11 import com.tc.test.TCTestCase;
12 import com.tc.util.Assert;
13 import com.tc.util.concurrent.NoExceptionLinkedQueue;
14 import com.tc.util.sequence.BatchSequenceReceiver;
15
16 public class PersistentBatchSequenceProviderTest extends TCTestCase {
17
18   public void tests() throws Exception JavaDoc {
19     TestPersistentSequence persistentSequence = new TestPersistentSequence();
20     TestSink requestBatchSink = new TestSink();
21
22     PersistentBatchSequenceProvider provider = new PersistentBatchSequenceProvider(persistentSequence);
23     provider.setRequestBatchSink(requestBatchSink);
24
25     TestBatchSequenceReceiver receiver = new TestBatchSequenceReceiver();
26
27     int batchSize = 5;
28     // make sure that the request context gets put in the sink properly.
29
provider.requestBatch(receiver, batchSize);
30     GlobalTransactionIDBatchRequestContext ctxt = (GlobalTransactionIDBatchRequestContext) requestBatchSink
31         .getInternalQueue().remove(0);
32     assertTrue(requestBatchSink.getInternalQueue().isEmpty());
33     assertSame(receiver, ctxt.getReceiver());
34     assertEquals(batchSize, ctxt.getBatchSize());
35
36     // now check the handler interface to make sure it handles the request properly
37
provider.handleEvent(ctxt);
38
39     // make sure it called the right thing on the sequence
40
Object JavaDoc[] args = (Object JavaDoc[]) persistentSequence.nextBatchQueue.poll(1);
41     assertEquals(new Integer JavaDoc(batchSize), args[0]);
42
43     // make sure it called back on the receiver
44
args = (Object JavaDoc[]) receiver.nextBatchQueue.poll(1);
45     assertEquals(new Long JavaDoc(persistentSequence.sequence - batchSize), args[0]);
46     assertEquals(new Long JavaDoc(persistentSequence.sequence), args[1]);
47   }
48
49   private static final class TestBatchSequenceReceiver implements BatchSequenceReceiver {
50
51     public final NoExceptionLinkedQueue nextBatchQueue = new NoExceptionLinkedQueue();
52
53     public void setNextBatch(long start, long end) {
54       nextBatchQueue.put(new Object JavaDoc[] { new Long JavaDoc(start), new Long JavaDoc(end) });
55     }
56
57     public boolean hasNext() {
58       return true;
59     }
60
61   }
62
63   private static final class TestPersistentSequence implements PersistentSequence {
64
65     public long sequence = 1;
66     public final NoExceptionLinkedQueue nextBatchQueue = new NoExceptionLinkedQueue();
67
68     public long next() {
69       return sequence;
70     }
71
72     public long nextBatch(int batchSize) {
73       nextBatchQueue.put(new Object JavaDoc[] { new Integer JavaDoc(batchSize) });
74       long ls = sequence;
75       sequence += batchSize;
76       return ls;
77     }
78
79     public String JavaDoc getUID() {
80       throw new ImplementMe();
81     }
82
83     public void setNext(long next) {
84       Assert.assertTrue(this.sequence <= next);
85       sequence = next;
86     }
87
88   }
89
90 }
91
Popular Tags