001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.util;
020:
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: /**
026: * Test LRUCache2.
027: */
028: public class LRUCache2Test extends TestCase {
029:
030: /**
031: * @see junit.framework.TestCase#setUp()
032: */
033: protected void setUp() throws Exception {
034: // TODO Auto-generated method stub
035: super .setUp();
036: }
037:
038: public void testTimeout() {
039: // Create cache with 100 item limit and 15 second timeout
040: TestEnvironment env = new TestEnvironment();
041: LRUCache2 cache = new LRUCache2(env, 100, 15000);
042:
043: env.time = 1000;
044: cache.put("key1", "string1");
045: cache.put("key2", "string2");
046: cache.put("key3", "string3");
047: assertNotNull(cache.get("key1"));
048: assertNotNull(cache.get("key2"));
049: assertNotNull(cache.get("key3"));
050:
051: env.time = 16000;
052: assertNull(cache.get("key1"));
053: assertNull(cache.get("key2"));
054: assertNull(cache.get("key3"));
055: }
056:
057: public void testLRU() {
058: // Create cache with 3 item limit and 15 second timeout
059: TestEnvironment env = new TestEnvironment();
060: LRUCache2 cache = new LRUCache2(env, 3, 15000);
061:
062: env.time = 1000;
063: cache.put("key1", "string1");
064: cache.put("key2", "string2");
065: cache.put("key3", "string3");
066: assertNotNull(cache.get("key1"));
067: assertNotNull(cache.get("key2"));
068: assertNotNull(cache.get("key3"));
069:
070: try {
071: Thread.sleep(200);
072: } catch (InterruptedException ignored) {
073: }
074:
075: // accessing key1 and key2 will make key3 LRU
076: cache.get("key1");
077: cache.get("key2");
078:
079: // adding a forth key will push out the LRU entry
080: cache.put("key4", "string4");
081: assertNull(cache.get("key3"));
082: }
083:
084: public void testPurge() {
085: // Create cache with 100 item limit and 15 second timeout
086: TestEnvironment env = new TestEnvironment();
087: LRUCache2 cache = new LRUCache2(env, 100, 15000);
088:
089: env.time = 1000;
090: cache.put("key1", "string1");
091: cache.put("key2", "string2");
092: cache.put("key3", "string3");
093: assertNotNull(cache.get("key1"));
094: assertNotNull(cache.get("key2"));
095: assertNotNull(cache.get("key3"));
096:
097: cache.purge(new String[] { "key1", "key2" });
098: assertEquals(1, cache.size());
099:
100: cache.purge();
101: assertEquals(0, cache.size());
102: }
103:
104: /**
105: * @see junit.framework.TestCase#tearDown()
106: */
107: protected void tearDown() throws Exception {
108: // TODO Auto-generated method stub
109: super .tearDown();
110: }
111:
112: public static class TestEnvironment implements
113: LRUCache2.Environment {
114: public long time = 0;
115:
116: public long getCurrentTimeInMillis() {
117: return time;
118: }
119: }
120:
121: public static Test suite() {
122: return new TestSuite(LRUCache2Test.class);
123: }
124:
125: }
|