KickJava   Java API By Example, From Geeks To Geeks.

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


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.MockStage;
8 import com.tc.exception.ImplementMe;
9 import com.tc.net.protocol.tcm.ChannelID;
10 import com.tc.net.protocol.tcm.MessageChannel;
11 import com.tc.object.ObjectID;
12 import com.tc.object.TestRequestManagedObjectMessage;
13 import com.tc.object.net.ChannelStats;
14 import com.tc.objectserver.core.api.ServerConfigurationContext;
15 import com.tc.objectserver.core.impl.TestServerConfigurationContext;
16 import com.tc.objectserver.impl.ObjectRequestManagerImpl;
17 import com.tc.objectserver.impl.TestObjectManager;
18 import com.tc.objectserver.l1.api.TestClientStateManager;
19 import com.tc.objectserver.tx.TestServerTransactionManager;
20 import com.tc.stats.counter.Counter;
21 import com.tc.stats.counter.CounterImpl;
22
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import junit.framework.TestCase;
27
28 public class ManagedObjectRequestHandlerTest extends TestCase {
29
30   public void testObjectRequestCounter() {
31     TestChannelStats channelStats = new TestChannelStats();
32     Counter channelReqCounter = new CounterImpl(666L);
33     Counter channelRemCounter = new CounterImpl(69L);
34     Counter requestCounter = new CounterImpl(0L);
35     Counter removeCounter = new CounterImpl(0L);
36     channelStats.reqCounter = channelReqCounter;
37     channelStats.remCounter = channelRemCounter;
38
39     TestServerConfigurationContext context = new TestServerConfigurationContext();
40     context.objectManager = new TestObjectManager();
41     context.objectRequestManager = new ObjectRequestManagerImpl(context.objectManager,
42                                                                 new TestServerTransactionManager());
43     context.clientStateManager = new TestClientStateManager();
44     context.addStage(ServerConfigurationContext.RESPOND_TO_OBJECT_REQUEST_STAGE, new MockStage("yo"));
45     context.channelStats = channelStats;
46
47     ManagedObjectRequestHandler handler = new ManagedObjectRequestHandler(requestCounter, removeCounter);
48     handler.initialize(context);
49
50     TestRequestManagedObjectMessage msg = new TestRequestManagedObjectMessage();
51     HashSet JavaDoc s = new HashSet JavaDoc();
52     s.add(new ObjectID(1));
53     msg.setObjectIDs(s);
54     Set JavaDoc removed = makeRemovedSet(31);
55     msg.setRemoved(removed);
56
57     assertEquals(0, requestCounter.getValue());
58     assertEquals(0, removeCounter.getValue());
59     assertEquals(666, channelReqCounter.getValue());
60     assertEquals(69, channelRemCounter.getValue());
61     handler.handleEvent(msg);
62     assertEquals(1, requestCounter.getValue());
63     assertEquals(31, removeCounter.getValue());
64     assertEquals(667, channelReqCounter.getValue());
65     assertEquals(100, channelRemCounter.getValue());
66   }
67
68   private Set JavaDoc makeRemovedSet(int num) {
69     Set JavaDoc rv = new HashSet JavaDoc();
70     for (int i = 0; i < num; i++) {
71       rv.add(new ObjectID(i));
72     }
73     return rv;
74   }
75
76   private static class TestChannelStats implements ChannelStats {
77     Counter remCounter;
78     Counter reqCounter;
79
80     public Counter getCounter(MessageChannel channel, String JavaDoc name) {
81       if (OBJECT_FLUSH_RATE.equals(name)) {
82         return remCounter;
83       } else if (OBJECT_REQUEST_RATE.equals(name)) { return reqCounter; }
84       throw new RuntimeException JavaDoc(name);
85     }
86
87     public void notifyTransaction(ChannelID channelID) {
88       throw new ImplementMe();
89
90     }
91   }
92
93 }
94
Popular Tags