KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > RegionTest


1 package org.jboss.cache.eviction;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.Region;
8 import org.jboss.cache.RegionManager;
9 import org.jboss.cache.config.EvictionConfig;
10
11
12 /**
13  * @author Ben Wang, Feb 11, 2004
14  * @author Daniel Huang (dhuang@jboss.org)
15  */

16 public class RegionTest extends TestCase
17 {
18    RegionManager regionManager_;
19    EvictionAlgorithm algorithm;
20
21    public RegionTest(String JavaDoc s)
22    {
23       super(s);
24    }
25
26    public void setUp() throws Exception JavaDoc
27    {
28       super.setUp();
29       algorithm = new LRUAlgorithm();
30       regionManager_ = new RegionManager();
31       regionManager_.getRegion("/a/b", true).setEvictionPolicy(new DummyEvictionConfiguration());
32    }
33
34    public void tearDown() throws Exception JavaDoc
35    {
36       super.tearDown();
37    }
38
39    public void testAddedQueue()
40    {
41       Fqn fqn1 = Fqn.fromString("/a/b/c");
42       Fqn fqn2 = Fqn.fromString("/a/b/d");
43       Fqn fqn3 = Fqn.fromString("/a/b/e");
44
45       Region region = regionManager_.getRegion("/a/b", true);
46       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
47       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
48       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
49
50       assertEquals("AddedNode queue size ", 3, region.nodeEventQueueSize());
51       EvictedEventNode node = region.takeLastEventNode();
52       Fqn fqn = node.getFqn();
53       assertEquals("DataNode retrieved should be FILO ", fqn, fqn1);
54       assertEquals("AddedNode queue size ", 2, region.nodeEventQueueSize());
55       fqn = region.takeLastEventNode().getFqn();
56       fqn = region.takeLastEventNode().getFqn();
57       node = region.takeLastEventNode();
58       assertNull("DataNode should be null", node);
59    }
60
61    public void testEventQueue()
62    {
63       Fqn fqn1 = Fqn.fromString("/a/b/c");
64       Fqn fqn2 = Fqn.fromString("/a/b/d");
65       Fqn fqn3 = Fqn.fromString("/a/b/e");
66
67       Region region = regionManager_.getRegion("/a/b", true);
68       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.REMOVE_NODE_EVENT));
69       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
70       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.VISIT_NODE_EVENT));
71
72
73
74       assertEquals("RemovedNode queue size ", 3, region.nodeEventQueueSize());
75       NodeEventType event = region.takeLastEventNode().getEventType();
76       assertEquals("DataNode retrieved should be: ", NodeEventType.REMOVE_NODE_EVENT, event);
77       Fqn fqn = region.takeLastEventNode().getFqn();
78       fqn = region.takeLastEventNode().getFqn();
79       EvictedEventNode node = region.takeLastEventNode();
80       assertNull("DataNode should be null", node);
81    }
82
83    public void testMassivePutOnQueue()
84    {
85       Fqn fqn2 = Fqn.fromString("/a/b/d");
86
87       Region region = regionManager_.getRegion("/a/b", true);
88       // This should succeed, alhtough it will produce warning over the threshold.
89
for (int i = 0; i < EvictionConfig.EVENT_QUEUE_SIZE_DEFAULT - 1; i++)
90       {
91          region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
92       }
93
94    }
95
96    void log(String JavaDoc msg)
97    {
98       System.out.println("-- " + msg);
99    }
100
101    public static Test suite()
102    {
103       return new TestSuite(RegionTest.class);
104    }
105
106    public static void main(String JavaDoc[] args)
107    {
108       junit.textui.TestRunner.run(suite());
109    }
110
111 }
112
Popular Tags