1   /*
2    * RCache - A collection of simple reference-based cache implementations.
3    * Copyright (C) 2007  Rodrigo Ruiz
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
18   */
19  package net.sourceforge.rcache.decorator;
20  
21  import junit.framework.TestCase;
22  import net.sourceforge.rcache.Cache;
23  import net.sourceforge.rcache.WeakCache;
24  
25  /**
26   * TestCase for {@link net.sourceforge.rcache.MruGuard}.
27   *
28   * @author Rodrigo Ruiz
29   */
30  public class MruGuardTest extends TestCase {
31  
32    /**
33     * Used in all tests.
34     */
35    private Cache<Integer, Object> cache = new WeakCache<Integer, Object>();
36  
37    /**
38     * Creates an instance.
39     *
40     * @param name Test name
41     */
42    public MruGuardTest(String name) {
43      super(name);
44    }
45  
46    /**
47     * Test for {@link net.sourceforge.rcache.decorator.MruGuard#MruGuard(Cache, int)}
48     * and {@link net.sourceforge.rcache.decorator.MruGuard#getMaxSize()}.
49     */
50    public final void testMruGuard() {
51      final int size = 5;
52      MruGuard<Integer, Object> guard = new MruGuard<Integer, Object>(cache, size);
53      assertEquals(size, guard.getMaxSize());
54  
55      assertFalse(guard.mruIterator().hasNext());
56  
57      String[] numbers = { "Zero", "One", "Two", "Three", "Four", "Five", "Six" };
58      for (int i = 0; i < numbers.length; i++) {
59        StringBuffer sb = new StringBuffer(numbers[i]);
60        guard.put(i, sb);
61      }
62      System.gc();
63  
64      assertNull(guard.get(0));
65      assertNull(guard.get(1));
66      for (int i = 2; i < numbers.length; i++) {
67        assertEquals(numbers[i], guard.get(i).toString());
68      }
69  
70      // Tests that the MRU list is not changed after removing and putting the
71      // same key
72      guard.remove(2);
73      guard.put(2, new StringBuffer("Two"));
74  
75      assertNull(guard.get(0));
76      assertNull(guard.get(1));
77      for (int i = 2; i < numbers.length; i++) {
78        assertEquals(numbers[i], guard.get(i).toString());
79      }
80  
81      guard.remove(2);
82      assertNull(guard.get(2));
83      for (int i = 2 + 1; i < numbers.length; i++) {
84        assertEquals(numbers[i], guard.get(i).toString());
85      }
86  
87      guard.clear();
88      for (int i = 0; i < numbers.length; i++) {
89        assertNull(guard.get(i));
90      }
91    }
92  }