001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestCache.java,v 1.9 2008/01/02 12:08:35 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.util.test;
008:
009: /**
010: @author bwm out of kers
011: */
012:
013: import com.hp.hpl.jena.util.cache.*;
014:
015: import junit.framework.*;
016:
017: public class TestCache extends TestCase {
018:
019: public TestCache(String name) {
020: super (name);
021: }
022:
023: public static TestSuite suite() {
024: TestSuite suite = new TestSuite("Cache");
025: suite.addTest(new CacheTestCase(CacheManager.RAND));
026: // suite.addTest( new CacheTestCase(CacheManager.ENHNODECACHE));
027: return suite;
028: }
029:
030: static class CacheTestCase extends TestCase {
031: String cacheType;
032:
033: CacheTestCase(String cacheType) {
034: super (cacheType);
035: this .cacheType = cacheType;
036: }
037:
038: protected void runTest() {
039: testCache();
040: }
041:
042: public void testCache() {
043: testCacheCreation(cacheType);
044: testCacheSimpleReturn(cacheType);
045: testFillTheCache(cacheType);
046: }
047:
048: public void testCacheCreation(String type) {
049: Cache c1 = CacheManager.createCache(type, "c1", 100);
050: try {
051: Cache c2 = CacheManager.createCache(type, "c2", 1);
052: assertTrue("Missing error on bad cache size: " + type,
053: false);
054: } catch (Error e) {
055: }
056: }
057:
058: public void testCacheSimpleReturn(String type) {
059:
060: int size = 100;
061: // this test does not fill the cache
062: Cache c1 = CacheManager.createCache(type, "c1", size);
063:
064: String k1 = "one";
065: String k2 = k1;
066: String k3 = k2;
067: Integer v1 = new Integer(-1);
068: Integer v2 = v1;
069: Integer v3 = v2;
070: c1.put(k1, v1);
071:
072: for (int i = 0; i < size; i++) {
073: k1 = k2;
074: v1 = v2;
075: Object o = c1.get(k1);
076: assertTrue("expected a hit", o != null);
077: assertEquals("should be the expected object", o, v1);
078: k2 = k3;
079: v2 = v3;
080: o = c1.get(k2);
081: assertTrue("expected a hit", o != null);
082: assertEquals("should be the expected object", o, v2);
083:
084: k3 = "T" + i;
085: v3 = new Integer(i);
086: c1.put(k3, v3);
087: }
088: }
089:
090: public void testFillTheCache(String type) {
091: final int size = 100;
092: Cache c1 = CacheManager.createCache(type, "c1", size);
093: String[] k = new String[size];
094: String[] v = new String[size];
095:
096: for (int i = 0; i < size; i++) {
097: k[i] = "K" + i;
098: v[i] = "V" + i;
099: c1.put(k[i], v[i]);
100: }
101:
102: int count = 0;
103:
104: for (int i = 0; i < size; i++) {
105: if (c1.get(k[i]) != null) {
106: count++;
107: }
108: }
109:
110: assertTrue("too low a hit rate: " + type + " = " + count,
111: count > size / 2);
112: assertEquals("count puts", size, c1.getPuts());
113: assertEquals("count gets", size, c1.getGets());
114: assertEquals("count hits", count, c1.getHits());
115: }
116: }
117:
118: }
119: /*
120: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
121: All rights reserved.
122:
123: Redistribution and use in source and binary forms, with or without
124: modification, are permitted provided that the following conditions
125: are met:
126:
127: 1. Redistributions of source code must retain the above copyright
128: notice, this list of conditions and the following disclaimer.
129:
130: 2. Redistributions in binary form must reproduce the above copyright
131: notice, this list of conditions and the following disclaimer in the
132: documentation and/or other materials provided with the distribution.
133:
134: 3. The name of the author may not be used to endorse or promote products
135: derived from this software without specific prior written permission.
136:
137: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
138: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
139: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
140: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
141: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
142: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
143: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
144: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
145: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
146: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
147: */
|