KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > test > TestCache


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestCache.java,v 1.6 2005/02/21 12:19:21 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.util.test;
8
9 /**
10     @author bwm out of kers
11 */

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 JavaDoc name)
22        { super( name ); }
23     
24     public static TestSuite suite() {
25         TestSuite suite = new TestSuite("Cache");
26         suite.addTest( new CacheTestCase(CacheManager.RAND));
27         // suite.addTest( new CacheTestCase(CacheManager.ENHNODECACHE));
28
return suite;
29     }
30            
31     static class CacheTestCase extends TestCase {
32         String JavaDoc cacheType;
33         
34         CacheTestCase(String JavaDoc 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 JavaDoc 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 JavaDoc e) {}
55     }
56     
57     public void testCacheSimpleReturn(String JavaDoc type) {
58         
59         int size = 100;
60         // this test does not fill the cache
61
Cache c1 = CacheManager.createCache(type, "c1", size);
62         
63         String JavaDoc k1 = "one";
64         String JavaDoc k2 = k1;
65         String JavaDoc k3 = k2;
66         Integer JavaDoc v1 = new Integer JavaDoc(-1);
67         Integer JavaDoc v2 = v1;
68         Integer JavaDoc v3 = v2;
69         c1.put(k1, v1);
70
71         for (int i=0; i<size; i++) {
72             k1 = k2;
73             v1 = v2;
74             Object JavaDoc 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 JavaDoc(i);
85             c1.put(k3,v3);
86         }
87     }
88     
89     public void testFillTheCache(String JavaDoc type) {
90         final int size = 100;
91         Cache c1 = CacheManager.createCache(type, "c1", size);
92         String JavaDoc[] k = new String JavaDoc[size];
93         String JavaDoc[] v = new String JavaDoc[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 /*
119     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
120     All rights reserved.
121
122     Redistribution and use in source and binary forms, with or without
123     modification, are permitted provided that the following conditions
124     are met:
125
126     1. Redistributions of source code must retain the above copyright
127        notice, this list of conditions and the following disclaimer.
128
129     2. Redistributions in binary form must reproduce the above copyright
130        notice, this list of conditions and the following disclaimer in the
131        documentation and/or other materials provided with the distribution.
132
133     3. The name of the author may not be used to endorse or promote products
134        derived from this software without specific prior written permission.
135
136     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
137     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
138     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
139     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
140     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
141     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
145     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 */

147
Popular Tags