KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > cache > LRUEvictionPolicyTest


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.cache;
5
6 import gnu.trove.TLinkable;
7
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import com.tc.object.ObjectID;
12 import com.tc.test.TCTestCase;
13
14 /**
15  *
16  */

17 public class LRUEvictionPolicyTest extends TCTestCase {
18
19   public EvictionPolicy createNewCache(int size) {
20     return new LRUEvictionPolicy(size);
21   }
22
23   public void tests() throws Exception JavaDoc {
24     int cacheSize = 10;
25     EvictionPolicy slc = createNewCache(cacheSize);
26     Cacheable[] cacheables = new Cacheable[cacheSize];
27     for (int i = 0; i < cacheSize; i++) {
28       cacheables[i] = new TestCacheable(new ObjectID(i));
29       boolean evict = slc.add(cacheables[i]);
30       assertFalse(evict);
31     }
32     assertTrue(slc.add(new TestCacheable(new ObjectID(11))));
33     Collection JavaDoc c = slc.getRemovalCandidates(-1);
34     assertEquals(new ObjectID(0), ((Cacheable) (c.iterator().next())).getObjectID());
35     removeAll(slc, c);
36
37     slc.markReferenced(cacheables[1]);
38
39     assertTrue(slc.add(new TestCacheable(new ObjectID(12))));
40     c = slc.getRemovalCandidates(-1);
41     assertEquals(1, c.size());
42     assertTrue(c.iterator().next() == cacheables[2]);
43
44     slc.remove(cacheables[3]);
45
46     slc.add(new TestCacheable(new ObjectID(13)));
47     c = slc.getRemovalCandidates(-1);
48     assertTrue(c.iterator().next() == cacheables[4]);
49
50     slc.add(new TestCacheable(new ObjectID(14)));
51     c = slc.getRemovalCandidates(-1);
52
53     assertEquals(1, c.size());
54     assertTrue(c.contains(cacheables[5]));
55     slc.remove(cacheables[5]);
56
57     c = slc.getRemovalCandidates(-1);
58     assertEquals(1, c.size());
59     assertTrue(c.contains(cacheables[6]));
60     slc.remove(cacheables[6]);
61
62     // repopulate the cache with just items from 'cachables'
63
for (int i = 0; i < cacheables.length; i++) {
64       cacheables[i] = new TestCacheable(new ObjectID(100 + i));
65       slc.add(cacheables[i]);
66       c = slc.getRemovalCandidates(-1);
67       removeAll(slc, c);
68     }
69
70     // go through the cache again and assert that the cache is back down to the proper size and
71
// the LRU policy is still in effect.
72
for (int i = 0; i < cacheables.length; i++) {
73       slc.add(new TestCacheable(new ObjectID(200 + i)));
74       c = slc.getRemovalCandidates(-1);
75       assertEquals(1, c.size());
76       Cacheable evicted = (Cacheable) c.iterator().next();
77       assertTrue(evicted == cacheables[i]);
78       removeAll(slc, c);
79     }
80   }
81
82   protected void removeAll(EvictionPolicy slc, Collection JavaDoc c) {
83     for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
84       slc.remove((Cacheable) iter.next());
85     }
86   }
87
88   public static class TestCacheable implements Cacheable {
89     private ObjectID id;
90     private TLinkable next;
91     private TLinkable previous;
92     private int accessed = 0;
93
94     public TestCacheable(ObjectID id) {
95       this.id = id;
96     }
97
98     public TestCacheable(ObjectID id, int accessed) {
99       this.id = id;
100       this.accessed = accessed;
101     }
102
103     public ObjectID getObjectID() {
104       return id;
105     }
106
107     public void markAccessed() {
108       this.accessed++;
109     }
110
111     public TLinkable getNext() {
112       return next;
113     }
114
115     public TLinkable getPrevious() {
116       return previous;
117     }
118
119     public void setNext(TLinkable next) {
120       this.next = next;
121     }
122
123     public void setPrevious(TLinkable previous) {
124       this.previous = previous;
125     }
126
127     public String JavaDoc toString() {
128       return "TestCacheable[" + id + "]";
129     }
130
131     public void clearAccessed() {
132       this.accessed = 0;
133
134     }
135
136     public boolean recentlyAccessed() {
137       return (this.accessed > 0);
138     }
139
140     public boolean canEvict() {
141       return true;
142     }
143
144     public int accessCount(int factor) {
145       accessed = accessed / factor;
146       return accessed;
147     }
148     
149     public int accessCount() {
150       return accessed;
151     }
152   }
153 }
Popular Tags