1 package org.apache.maven.repository.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import junit.framework.TestCase;
24 public class CacheTest
29 private static final double CACHE_HIT_RATIO = 0.5;
31 private static final double CACHE_HIT_RATIO_THRESHOLD = 0.75;
33 public void testCacheManagementBasedOnHitsRatio()
35 cache = new Cache( CACHE_HIT_RATIO );
36 newCacheObjectTests();
39 String value = "value";
40 for ( int ctr = 1; ctr < 10; ctr++ )
42 cache.put( key + ctr, value + ctr );
45 while ( cache.getHitRate() < CACHE_HIT_RATIO_THRESHOLD )
49 cache.put( "key10", "value10" );
50 assertNull( "first key must be expired", cache.get( "key1" ) );
53 public void testCacheManagementBasedOnCacheSize()
55 cache = new Cache( 9 );
56 newCacheObjectTests();
59 String value = "value";
60 for ( int ctr = 1; ctr < 10; ctr++ )
62 cache.put( key + ctr, value + ctr );
65 cache.put( "key10", "value10" );
66 assertNull( "first key must be expired", cache.get( "key1" ) );
67 assertEquals( "check cache size to be max size", 9, cache.size() );
70 public void testCacheManagementBasedOnCacheSizeAndHitRate()
72 cache = new Cache( CACHE_HIT_RATIO, 9 );
73 newCacheObjectTests();
76 String value = "value";
77 for ( int ctr = 1; ctr < 5; ctr++ )
79 cache.put( key + ctr, value + ctr );
82 while ( cache.getHitRate() < CACHE_HIT_RATIO )
87 cache.put( "key10", "value10" );
88 assertNull( "first key must be expired", cache.get( "key1" ) );
90 while ( cache.getHitRate() >= CACHE_HIT_RATIO )
95 for ( int ctr = 5; ctr < 10; ctr++ )
97 cache.put( key + ctr, value + ctr );
100 cache.put( "key11", "value11" );
101 assertNull( "second key must be expired", cache.get( "key2" ) );
102 assertEquals( "check cache size to be max size", 9, cache.size() );
105 public void testCacheOnRedundantData()
107 cache = new Cache( CACHE_HIT_RATIO, 9 );
108 newCacheObjectTests();
111 String value = "value";
112 for ( int ctr = 1; ctr < 10; ctr++ )
114 cache.put( key + ctr, value + ctr );
117 cache.put( "key1", "value1" );
118 cache.put( "key10", "value10" );
119 assertNull( "second key must be gone", cache.get( "key2" ) );
120 assertEquals( "check cache size to be max size", 9, cache.size() );
123 private void newCacheObjectTests()
125 assertEquals( (double) 0, cache.getHitRate(), 0 );
126 assertEquals( "check cache size", 0, cache.size() );
128 String value = "value";
131 cache.put( key, value );
132 assertEquals( "check cache hit", value, cache.get( key ) );
133 assertEquals( (double) 1, cache.getHitRate(), 0 );
134 assertEquals( "check cache size", 1, cache.size() );
135 assertNull( "check cache miss", cache.get( "none" ) );
136 assertEquals( CACHE_HIT_RATIO, cache.getHitRate(), 0 );
138 assertNull( "check flushed object", cache.get( "key" ) );
139 assertEquals( (double) 0, cache.getHitRate(), 0 );
140 assertEquals( "check flushed cache size", 0, cache.size() );