001    package org.hackystat.utilities.uricache;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertNull;
005    import static org.junit.Assert.assertTrue;
006    
007    import java.util.logging.Level;
008    import java.util.logging.Logger;
009    import org.junit.Ignore;
010    import org.junit.Test;
011    
012    /**
013     * Tests the UriCache class.
014     * 
015     * @author Philip Johnson
016     * 
017     */
018    public class TestUriCache {
019      
020      private static final String testSubDir = "TestUriCache";
021    
022      /**
023       * Test simple cache put and get.
024       */
025      @Test
026      public void testSimpleCache() {
027        // Create a cache
028        String key = "key";
029        String value = "value";
030        UriCache cache = new UriCache("TestSimpleCache", testSubDir);
031        cache.put(key, value);
032        assertEquals("Checking simple get", value, cache.get(key));
033        assertTrue("Checking keys", cache.getKeys().contains(key));
034        cache.remove(key);
035        assertNull("Checking non-existant get", cache.get(key));
036        assertEquals("Checking size", 0, cache.size());
037      }
038      
039      /**
040       * Tests addition and deletion of hierarchical cache entries. 
041       * The idea is that if you use keys with the ":" to separate parts, you can delete
042       * collections of cache elements with one call by specifying the colon delimited hierarchy.
043       * See http://jakarta.apache.org/jcs/faq.html#hierarchical-removal for details. 
044       * Unfortunately this does not work.
045       */
046      @Ignore
047      @Test
048      public void testHierarchicalKeyCacheRemoval() {
049        // Create a cache. 
050        UriCache cache = new UriCache("HierarchicalKeyCache", testSubDir, 1D, 1L);
051        cache.clear();
052        Logger.getLogger("org.apache.jcs").setLevel(Level.ALL);
053        cache.setLoggingLevel("ALL");
054        // Add three elements.
055        cache.put("foo:bar:baz", "one");
056        cache.put("foo:bar:qux", "two");
057        cache.put("bar:quxx", "three");
058        cache.remove("foo:bar:");
059        System.out.println(cache.get("foo:bar:baz"));
060        System.out.println(cache.get("foo:bar:qux"));
061        assertNull("Checking foo:bar:baz is gone", cache.get("foo:bar:baz"));
062        assertNull("Checking foo:bar:qux is gone", cache.get("foo:bar:qux"));
063        assertEquals("Checking foo:qux is still there", "three", cache.get("bar:quxx"));
064      }
065      
066      /**
067       * Tests addition and deletion of grouped cache entries. 
068       */
069      @Test
070      public void testGroupedElementsCache() {
071        // Create a cache. 
072        UriCache cache = new UriCache("GroupedKeyCache", testSubDir, 1D, 1000L);
073        cache.clear();
074        String group1 = "group1";
075        String group2 = "group2";
076        String one = "one";
077        String two = "two";
078        String three = "three";
079        cache.putInGroup(one, group1, "1");
080        cache.putInGroup(two, group1, "2");
081        cache.putInGroup(three, "group2", "3");
082        assertEquals("Test simple group retrieval1", "1", cache.getFromGroup(one, group1));
083        assertEquals("Test simple group retrieval2", "2", cache.getFromGroup(two, group1));
084        assertEquals("Test simple group retrieval3", "3", cache.getFromGroup(three, "group2"));
085        assertNull("Test non-group retrieval won't get the element", cache.get(one));
086        assertEquals("Test group1 keyset", 2, cache.getGroupKeys(group1).size());
087        assertEquals("Test group2 keyset", 1, cache.getGroupKeys(group2).size());
088        cache.removeFromGroup(one, group1);
089        assertEquals("Test new group1 keyset", 1, cache.getGroupKeys(group1).size());
090        assertTrue("Test group1 keyset element", cache.getGroupKeys(group1).contains(two));
091        cache.clearGroup(group1);
092        assertEquals("Test clearGroup", 0, cache.getGroupKeys(group1).size());
093        assertNull("Test clearGroup 2", cache.getFromGroup(one, group1));
094      }
095      
096     
097      /**
098       * Test simple cache put and get.
099       */
100      @Test
101      public void testDisposeCache() {
102        // Create a cache
103        String key = "key";
104        String value = "value";
105        String cacheName = "TestDisposeCache";
106        UriCache cache = new UriCache(cacheName, testSubDir);
107        cache.put(key, value);
108        assertEquals("Checking simple get", value, cache.get(key));
109        // now dispose and try again.
110        UriCache.dispose(cacheName);
111        cache = new UriCache(cacheName, testSubDir);
112        cache.put(key, value);
113        assertEquals("Checking simple get 2", value, cache.get(key));
114      }
115    
116      /**
117       * Test use of disk cache.
118       */
119      @Test
120      public void testDiskCache() {
121        // Create a cache
122        Double maxLifeDays = 1D;
123        Long capacity = 100L;
124        UriCache cache = new UriCache("TestDiskCache", testSubDir, maxLifeDays, capacity);
125        // Now do a loop and put more than 100 items in it.
126        for (int i = 1; i < 200; i++) {
127          cache.put(i, i);
128        }
129        // Now check to see that we can retrieve all 200 items.
130        for (int i = 1; i < 200; i++) {
131          assertEquals("Checking retrieval", i, cache.get(i));
132        }
133      }
134      
135      /**
136       * Test that we can expire elements from the cache. 
137       * This test no longer works because we've changed the 
138       * @throws Exception If problems occur.
139       */
140      @Test
141      public void testElementExpiration() throws Exception {
142        // Create a cache with maxLife of 1 second and a maximum of 100 in-memory elements.
143        Double maxLifeDays = 1.157e-5D;
144        Long capacity = 100L;
145        UriCache cache = new UriCache("TestExpiration", testSubDir, maxLifeDays, capacity);
146        // Now do a loop and put 200 items in it.
147        for (int i = 1; i < 200; i++) {
148          cache.put(i, i);
149        }
150        // Add an element that expires in 3 seconds. 
151        cache.put(300, 300, 8.4e-4D);
152        // Now wait for two seconds.
153        Thread.sleep(2000);
154        // Now check to see that all of the items with the default maxLife are gone.
155        for (int i = 1; i < 200; i++) {
156          assertNull("Checking retrieval", cache.get(i));
157        }
158        // And check that our item with the custom maxLife time is still there.
159        assertEquals("Check non-expired element", 300, cache.get(300));
160        // Now wait one more second, enough time for our custom maxLife time to be exceeded.
161        Thread.sleep(1001);
162        // Now see that our element with the custom maxLife time is now gone.
163        assertNull("Check expired element", cache.get(300));
164      }
165    }