View Javadoc
1   /**
2    * 
3    */
4   package org.melati.poem.util.test;
5   
6   import java.util.Enumeration;
7   
8   import org.melati.poem.util.Cache;
9   import org.melati.poem.util.CacheDuplicationException;
10  import org.melati.poem.util.EnumUtils;
11  
12  import junit.framework.TestCase;
13  
14  /**
15   * @author timp
16   * @since 30 May 2007
17   *
18   */
19  public class CacheTest extends TestCase {
20  
21    Cache c = null;
22    /**
23     * @param name
24     */
25    public CacheTest(String name) {
26      super(name);
27    }
28  
29    /** 
30     * {@inheritDoc}
31     * @see junit.framework.TestCase#setUp()
32     */
33    protected void setUp() throws Exception {
34      super.setUp();
35      c = new Cache(12);
36    }
37  
38    /** 
39     * {@inheritDoc}
40     * @see junit.framework.TestCase#tearDown()
41     */
42    protected void tearDown() throws Exception {
43      super.tearDown();
44    }
45  
46    /**
47     * Test method for {@link org.melati.poem.util.Cache#Cache(int)}.
48     */
49    public void testCache() {
50     
51    }
52  
53    /**
54     * Test method for {@link org.melati.poem.util.Cache#setSize(int)}.
55     */
56    public void testSetSize() {
57      assertEquals(12, c.getSize());
58      c.setSize(100);
59      assertEquals(100, c.getSize());
60      c.setSize(12);
61      try { 
62        c.setSize(-1);
63        fail("Should have bombed");
64      } catch (IllegalArgumentException e) { 
65        e = null;
66      }
67    }
68  
69    /**
70     * Test method for {@link org.melati.poem.util.Cache#trim(int)}.
71     */
72    public void testTrim() {
73      c.setSize(4);
74      assertEquals(4, c.getSize());
75      assertFalse(c.getInfo().getHeldElements().hasMoreElements());
76      assertFalse(c.getInfo().getDroppedElements().hasMoreElements());
77  
78      c.put("a","a1");
79      c.put("b","a1");
80      c.put("c","a1");
81      c.put("d","a1");
82      assertTrue(c.getInfo().getHeldElements().hasMoreElements());
83      assertFalse(c.getInfo().getDroppedElements().hasMoreElements());
84      c.put("e","a1");
85      assertFalse(c.getInfo().getDroppedElements().hasMoreElements());
86      c.put("f","a1");
87      assertTrue(c.getInfo().getDroppedElements().hasMoreElements());
88      c.trim(3);
89      assertTrue(c.getInfo().getDroppedElements().hasMoreElements());
90      assertEquals(4, c.getSize());   
91    }
92  
93    /**
94     * Test method for {@link org.melati.poem.util.Cache#delete(java.lang.Object)}.
95     */
96    public void testDelete() {
97      c.delete("not in cache");
98      c.put("key", "value");
99      c.delete("key");
100     assertNull(c.get("key"));
101   }
102 
103   /**
104    * Test method for {@link org.melati.poem.util.Cache#put(java.lang.Object, java.lang.Object)}.
105    */
106   public void testPut() {
107     c.put("a","a1");
108     try { 
109       c.put("a","a1");
110       fail("Should have bombed");
111     } catch (CacheDuplicationException e) { 
112       e = null;
113     }
114   }
115   public void testPutNullKey() {
116     try { 
117       c.put(null, "null");
118       fail("Should have bombed");
119     } catch (NullPointerException e) { 
120       e = null;
121     }
122   }
123   public void testPutNullValue() {
124     try { 
125       c.put("null", null);
126       fail("Should have bombed");
127     } catch (NullPointerException e) { 
128       e = null;
129     }
130   }
131 
132   /**
133    * Test method for {@link org.melati.poem.util.Cache#get(java.lang.Object)}.
134    * 
135    * It appears that when the cache size is exceeded (size is one bigger than size!!) 
136    * then all elements are dropped.
137    */
138   public void testGet() {
139     c.setSize(2);
140     c.put("key1", "1");
141     assertEquals("1", c.get("key1"));
142     c.put("key2", "2");
143     assertEquals("1", c.get("key1"));
144     assertEquals("2", c.get("key2"));
145     c.put("key3", "3");
146     assertEquals("1", c.get("key1"));
147     assertEquals("2", c.get("key2"));
148     assertEquals("3", c.get("key3"));
149     c.put("key4", "4");
150     assertEquals("1", c.get("key1"));
151     assertEquals("2", c.get("key2"));
152     assertEquals("3", c.get("key3"));
153     assertEquals("4", c.get("key4"));
154     c.put("key5", "5");
155     assertEquals("1", c.get("key1"));
156     assertEquals("2", c.get("key2"));
157     assertEquals("3", c.get("key3"));
158     assertEquals("4", c.get("key4"));
159     assertEquals("5", c.get("key5"));
160     c.trim(2);
161     assertEquals("1", c.get("key1"));
162     assertEquals("2", c.get("key2"));
163     assertEquals("3", c.get("key3"));
164     assertEquals("4", c.get("key4"));
165     assertEquals("5", c.get("key5"));
166     assertNull(c.get("not in cache"));
167   }
168 
169   /**
170    * Test method for {@link org.melati.poem.util.Cache#iterate(org.melati.poem.util.Procedure)}.
171    */
172   public void testIterate() {
173    
174   }
175 
176   /**
177    * Test method for {@link org.melati.poem.util.Cache#getReport()}.
178    */
179   public void testGetReport() {
180     c.setSize(1);
181     Enumeration<Object> report = c.getReport(); 
182     assertTrue(report.hasMoreElements());
183     assertEquals("1 maxSize, null theMRU, null theLRU, 0 collectedEver", report.nextElement());
184     assertEquals("0 held, 0 total ", report.nextElement());
185     assertFalse(report.hasMoreElements());
186     c.put("key1", "value1");
187     report = c.getReport(); 
188     assertTrue(report.hasMoreElements());
189     assertEquals("1 maxSize, null>>key1=value1>>null theMRU, null>>key1=value1>>null theLRU, 0 collectedEver", report.nextElement());
190     assertEquals("1 held, 1 total ", report.nextElement()); 
191     assertFalse(report.hasMoreElements());
192     c.put("key2", "value2");
193     report = c.getReport(); 
194     assertTrue(report.hasMoreElements());
195     assertEquals("1 maxSize, null>>key2=value2>>key1 theMRU, key2>>key1=value1>>null theLRU, 0 collectedEver", report.nextElement());
196     assertEquals("2 held, 2 total ", report.nextElement()); 
197     assertFalse(report.hasMoreElements());
198     c.put("key3", "value3");
199     report = c.getReport(); 
200     assertTrue(report.hasMoreElements());
201     assertEquals("1 maxSize, null>>key3=value3>>key2 theMRU, key3>>key2=value2>>null theLRU, 0 collectedEver", report.nextElement());
202     assertEquals("2 held, 3 total ", report.nextElement()); 
203     assertFalse(report.hasMoreElements());
204   }
205 
206   /**
207    * Test method for {@link org.melati.poem.util.Cache#getInfo()}.
208    */
209   public void testGetInfo() {
210    Enumeration<Object> report = c.getInfo().getReport();
211    assertTrue(report.hasMoreElements());
212    assertEquals(c.getReport().nextElement(), report.nextElement());
213    
214    assertFalse(c.getInfo().getDroppedElements().hasMoreElements());
215    c.put("key", "value");
216    c.delete("key");
217    assertFalse(c.getInfo().getDroppedElements().hasMoreElements());   
218    c.setSize(1);
219    c.put("key1", "value1");
220    assertFalse(c.getInfo().getDroppedElements().hasMoreElements());   
221    c.put("key2", "value2");
222    assertFalse(c.getInfo().getDroppedElements().hasMoreElements());   
223    c.put("key3", "value3");
224    assertEquals(1, EnumUtils.vectorOf(c.getInfo().getDroppedElements()).size());   
225    
226   }
227 
228   
229   /**
230    * Test method for {@link org.melati.poem.util.Cache#dumpAnalysis()}.
231    */
232   public void testDumpAnalysis() {
233     c.dumpAnalysis();
234   }
235 
236   public void testFillingToBeyondCapacity() { 
237     for (int i = 5; i < 12; i++) { 
238       c.setSize(i);    
239       c.delete("01"); 
240       c.put("01", "a"); 
241       c.dumpAnalysis();
242       c.delete("02"); 
243       c.put("02", "b"); 
244       c.dumpAnalysis();
245       c.delete("03"); 
246       c.put("03", "c"); 
247       c.dumpAnalysis();
248       c.delete("04"); 
249       c.put("04", "d"); 
250       c.dumpAnalysis();
251       c.delete("05"); 
252       c.put("05", "e"); 
253       c.dumpAnalysis();
254       c.delete("06"); 
255       c.put("06", "f"); 
256       c.dumpAnalysis();
257       c.delete("07"); 
258       c.put("07", "g"); 
259       c.dumpAnalysis();
260       c.delete("08"); 
261       c.put("08", "h"); 
262       c.dumpAnalysis();
263       c.delete("09"); 
264       c.put("09", "i"); 
265       c.dumpAnalysis();
266       c.delete("10"); 
267       c.put("10", "j"); 
268       c.dumpAnalysis();
269       c.delete("11"); 
270       c.put("11", "k"); 
271       c.dumpAnalysis();
272       c.delete("12"); 
273       c.put("12", "l"); 
274       c.dumpAnalysis();
275       c.delete("13"); 
276       c.put("13", "m"); 
277       c.dumpAnalysis();
278       c.delete("14"); 
279       c.put("14", "n"); 
280       c.dumpAnalysis();
281       c.delete("15"); 
282       c.put("15", "o"); 
283       c.dumpAnalysis();
284     } 
285     System.err.println("--");
286     for (int i = 12; i > 4; i--) { 
287       c.trim(i);
288       c.delete("01"); 
289       c.put("01", "a"); 
290       c.dumpAnalysis();
291       c.delete("02"); 
292       c.put("02", "b"); 
293       c.dumpAnalysis();
294       c.delete("03"); 
295       c.put("03", "c"); 
296       c.dumpAnalysis();
297       c.delete("04"); 
298       c.put("04", "d"); 
299       c.dumpAnalysis();
300       c.delete("05"); 
301       c.put("05", "e"); 
302       c.dumpAnalysis();
303       c.delete("06"); 
304       c.put("06", "f"); 
305       c.dumpAnalysis();
306       c.delete("07"); 
307       c.put("07", "g"); 
308       c.dumpAnalysis();
309       c.delete("08"); 
310       c.put("08", "h"); 
311       c.dumpAnalysis();
312       c.delete("09"); 
313       c.put("09", "i"); 
314       c.dumpAnalysis();
315       c.delete("10"); 
316       c.put("10", "j"); 
317       c.dumpAnalysis();
318       c.delete("11"); 
319       c.put("11", "k"); 
320       c.dumpAnalysis();
321       c.delete("12"); 
322       c.put("12", "l"); 
323       c.dumpAnalysis();
324       c.delete("13"); 
325       c.put("13", "m"); 
326       c.dumpAnalysis();
327       c.delete("14"); 
328       c.put("14", "n"); 
329       c.dumpAnalysis();
330       c.delete("15"); 
331       c.put("15", "o"); 
332       c.dumpAnalysis();
333     } 
334     System.err.println("--");
335     c.dump();
336   }
337   
338   public void testZeroSizedCache() { 
339     Cache z = new Cache(0);
340     assertNull(z.get("a"));
341     z.put("a", "A");
342     assertEquals("A",z.get("a"));
343     assertFalse(c.getInfo().getDroppedElements().hasMoreElements());
344 
345   }
346   
347   public void testGCHandling() { 
348     char[] ballast = new char[4000000];
349     try {
350       ballast[0]='e'; // Shut eclipse up
351       for (int i=0; i<1000; i++) { 
352         c.put(new Integer(i), new StringBuffer(1000000));
353       }
354     } catch (OutOfMemoryError m) {
355       ballast = null;
356       Runtime r = Runtime.getRuntime();
357       r.gc();
358     }
359     c.dump();
360   }
361 }