KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > handler > ApplyTransactionChangeHandlerTest


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.handler;
6
7 import com.tc.async.impl.MockSink;
8 import com.tc.async.impl.MockStage;
9 import com.tc.net.protocol.tcm.ChannelID;
10 import com.tc.object.dmi.DmiDescriptor;
11 import com.tc.object.dna.impl.ObjectStringSerializer;
12 import com.tc.object.lockmanager.api.LockID;
13 import com.tc.object.lockmanager.api.Notify;
14 import com.tc.object.lockmanager.api.ThreadID;
15 import com.tc.object.tx.TransactionID;
16 import com.tc.object.tx.TxnBatchID;
17 import com.tc.object.tx.TxnType;
18 import com.tc.objectserver.api.ObjectInstanceMonitor;
19 import com.tc.objectserver.context.ApplyTransactionContext;
20 import com.tc.objectserver.context.BroadcastChangeContext;
21 import com.tc.objectserver.core.api.ServerConfigurationContext;
22 import com.tc.objectserver.core.impl.TestServerConfigurationContext;
23 import com.tc.objectserver.gtx.TestGlobalTransactionManager;
24 import com.tc.objectserver.impl.ObjectInstanceMonitorImpl;
25 import com.tc.objectserver.lockmanager.api.NotifiedWaiters;
26 import com.tc.objectserver.lockmanager.api.TestLockManager;
27 import com.tc.objectserver.tx.NullTransactionalObjectManager;
28 import com.tc.objectserver.tx.ServerTransaction;
29 import com.tc.objectserver.tx.ServerTransactionImpl;
30 import com.tc.objectserver.tx.TestServerTransactionManager;
31 import com.tc.util.SequenceID;
32
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.LinkedList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 import junit.framework.TestCase;
41
42 public class ApplyTransactionChangeHandlerTest extends TestCase {
43
44   private ApplyTransactionChangeHandler handler;
45   private ObjectInstanceMonitor instanceMonitor;
46   private TestServerTransactionManager transactionManager;
47   private TestGlobalTransactionManager gtxm;
48   private TestLockManager lockManager;
49   private MockSink broadcastSink;
50
51   public void setUp() throws Exception JavaDoc {
52     instanceMonitor = new ObjectInstanceMonitorImpl();
53     transactionManager = new TestServerTransactionManager();
54     lockManager = new TestLockManager();
55     gtxm = new TestGlobalTransactionManager();
56     handler = new ApplyTransactionChangeHandler(instanceMonitor, gtxm);
57
58     MockStage stageBo = new MockStage("Bo");
59     MockStage stageCo = new MockStage("Co");
60     broadcastSink = stageBo.sink;
61     TestServerConfigurationContext context = new TestServerConfigurationContext();
62     context.transactionManager = transactionManager;
63     context.txnObjectManager = new NullTransactionalObjectManager();
64     context.addStage(ServerConfigurationContext.BROADCAST_CHANGES_STAGE, stageBo);
65     context.addStage(ServerConfigurationContext.COMMIT_CHANGES_STAGE, stageCo);
66     context.lockManager = lockManager;
67
68     handler.initialize(context);
69   }
70
71   public void testLockManagerNotifyIsCalled() throws Exception JavaDoc {
72     TxnBatchID batchID = new TxnBatchID(1);
73     TransactionID txID = new TransactionID(1);
74     LockID[] lockIDs = new LockID[] { new LockID("1") };
75     ChannelID channelID = new ChannelID(1);
76     List JavaDoc dnas = Collections.unmodifiableList(new LinkedList JavaDoc());
77     ObjectStringSerializer serializer = null;
78     Map JavaDoc newRoots = Collections.unmodifiableMap(new HashMap JavaDoc());
79     TxnType txnType = TxnType.NORMAL;
80     List JavaDoc notifies = new LinkedList JavaDoc();
81
82     for (int i = 0; i < 10; i++) {
83       notifies.add(new Notify(new LockID("" + i), new ThreadID(i), i % 2 == 0));
84     }
85     SequenceID sequenceID = new SequenceID(1);
86     ServerTransaction tx = new ServerTransactionImpl(batchID, txID, sequenceID, lockIDs, channelID, dnas, serializer,
87                                                      newRoots, txnType, notifies, DmiDescriptor.EMPTY_ARRAY);
88     // call handleEvent with the global transaction reporting that it doesn't need an apply...
89
assertTrue(lockManager.notifyCalls.isEmpty());
90     assertTrue(broadcastSink.queue.isEmpty());
91     handler.handleEvent(getApplyTxnContext(tx));
92     // even if the transaction has already been applied, the notifies must be applied, since they aren't persistent.
93
assertEquals(notifies.size(), lockManager.notifyCalls.size());
94     lockManager.notifyCalls.clear();
95     assertNotNull(broadcastSink.queue.remove(0));
96
97     // call handleEvent with the global transaction reporting that it DOES need an apply...
98
handler.handleEvent(getApplyTxnContext(tx));
99
100     assertEquals(notifies.size(), lockManager.notifyCalls.size());
101     NotifiedWaiters notifiedWaiters = null;
102     for (Iterator JavaDoc i = notifies.iterator(); i.hasNext();) {
103       Notify notify = (Notify) i.next();
104       Object JavaDoc[] args = (Object JavaDoc[]) lockManager.notifyCalls.remove(0);
105       assertEquals(notify.getLockID(), args[0]);
106       assertEquals(channelID, args[1]);
107       assertEquals(notify.getThreadID(), args[2]);
108       assertEquals(new Boolean JavaDoc(notify.getIsAll()), args[3]);
109       if (notifiedWaiters == null) {
110         notifiedWaiters = (NotifiedWaiters) args[4];
111       }
112       assertNotNull(notifiedWaiters);
113       assertSame(notifiedWaiters, args[4]);
114     }
115
116     // next, check to see that the handler puts the newly pending waiters into the broadcast context.
117
BroadcastChangeContext bctxt = (BroadcastChangeContext) broadcastSink.queue.remove(0);
118     assertNotNull(bctxt);
119     assertEquals(notifiedWaiters, bctxt.getNewlyPendingWaiters());
120   }
121
122   private ApplyTransactionContext getApplyTxnContext(ServerTransaction txt) {
123     ApplyTransactionContext atc = new ApplyTransactionContext(txt, new HashMap JavaDoc());
124     return atc;
125   }
126
127 }
128
Popular Tags