1 6 7 package com.hp.hpl.jena.util.test; 8 9 12 13 import com.hp.hpl.jena.util.cache.*; 14 15 import junit.framework.*; 16 17 18 public class TestCache extends TestCase 19 { 20 21 public TestCache(String name) 22 { super( name ); } 23 24 public static TestSuite suite() { 25 TestSuite suite = new TestSuite("Cache"); 26 suite.addTest( new CacheTestCase(CacheManager.RAND)); 27 return suite; 29 } 30 31 static class CacheTestCase extends TestCase { 32 String cacheType; 33 34 CacheTestCase(String cacheType) { 35 super( cacheType ); 36 this.cacheType = cacheType; 37 } 38 39 protected void runTest() { 40 testCache(); 41 } 42 43 public void testCache() { 44 testCacheCreation(cacheType); 45 testCacheSimpleReturn(cacheType); 46 testFillTheCache(cacheType); 47 } 48 49 public void testCacheCreation(String type) { 50 Cache c1 = CacheManager.createCache(type, "c1", 100); 51 try { 52 Cache c2 = CacheManager.createCache(type, "c2", 1); 53 assertTrue("Missing error on bad cache size: " + type, false); 54 } catch (Error e) {} 55 } 56 57 public void testCacheSimpleReturn(String type) { 58 59 int size = 100; 60 Cache c1 = CacheManager.createCache(type, "c1", size); 62 63 String k1 = "one"; 64 String k2 = k1; 65 String k3 = k2; 66 Integer v1 = new Integer (-1); 67 Integer v2 = v1; 68 Integer v3 = v2; 69 c1.put(k1, v1); 70 71 for (int i=0; i<size; i++) { 72 k1 = k2; 73 v1 = v2; 74 Object o = c1.get(k1); 75 assertTrue("expected a hit", o != null); 76 assertEquals("should be the expected object", o, v1); 77 k2 = k3; 78 v2 = v3; 79 o = c1.get(k2); 80 assertTrue("expected a hit", o != null); 81 assertEquals("should be the expected object", o, v2); 82 83 k3 = "T" + i; 84 v3 = new Integer (i); 85 c1.put(k3,v3); 86 } 87 } 88 89 public void testFillTheCache(String type) { 90 final int size = 100; 91 Cache c1 = CacheManager.createCache(type, "c1", size); 92 String [] k = new String [size]; 93 String [] v = new String [size]; 94 95 for (int i=0; i<size; i++) { 96 k[i] = "K" + i; 97 v[i] = "V" + i; 98 c1.put(k[i], v[i]); 99 } 100 101 int count = 0; 102 103 for (int i=0; i<size; i++) { 104 if (c1.get(k[i]) != null) { 105 count++; 106 } 107 } 108 109 assertTrue("too low a hit rate: " + type + " = " + count, 110 count > size/2); 111 assertEquals("count puts", size, c1.getPuts()); 112 assertEquals("count gets", size, c1.getGets()); 113 assertEquals("count hits", count, c1.getHits()); 114 } 115 } 116 117 } 118 147 | Popular Tags |