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 public void testCacheManagementBasedOnHitsRatio()
31 cache = new Cache( 0.5 );
32 newCacheObjectTests();
35 String value = "value";
36 for ( int ctr = 1; ctr < 10; ctr++ )
38 cache.put( key + ctr, value + ctr );
41 while ( cache.getHitRate() < 0.75 )
45 cache.put( "key10", "value10" );
46 assertNull( "first key must be expired", cache.get( "key1" ) );
49 public void testCacheManagementBasedOnCacheSize()
51 cache = new Cache( 9 );
52 newCacheObjectTests();
55 String value = "value";
56 for ( int ctr = 1; ctr < 10; ctr++ )
58 cache.put( key + ctr, value + ctr );
61 cache.put( "key10", "value10" );
62 assertNull( "first key must be expired", cache.get( "key1" ) );
63 assertEquals( "check cache size to be max size", 9, cache.size() );
66 public void testCacheManagementBasedOnCacheSizeAndHitRate()
68 cache = new Cache( 0.5, 9 );
69 newCacheObjectTests();
72 String value = "value";
73 for ( int ctr = 1; ctr < 5; ctr++ )
75 cache.put( key + ctr, value + ctr );
78 while ( cache.getHitRate() < 0.5 )
83 cache.put( "key10", "value10" );
84 assertNull( "first key must be expired", cache.get( "key1" ) );
86 while ( cache.getHitRate() >= 0.5 )
91 for ( int ctr = 5; ctr < 10; ctr++ )
93 cache.put( key + ctr, value + ctr );
96 cache.put( "key11", "value11" );
97 assertNull( "second key must be expired", cache.get( "key2" ) );
98 assertEquals( "check cache size to be max size", 9, cache.size() );
101 public void testCacheOnRedundantData()
103 cache = new Cache( 0.5, 9 );
104 newCacheObjectTests();
107 String value = "value";
108 for ( int ctr = 1; ctr < 10; ctr++ )
110 cache.put( key + ctr, value + ctr );
113 cache.put( "key1", "value1" );
114 cache.put( "key10", "value10" );
115 assertNull( "second key must be gone", cache.get( "key2" ) );
116 assertEquals( "check cache size to be max size", 9, cache.size() );
119 private void newCacheObjectTests()
121 assertEquals( (double) 0, cache.getHitRate(), 0 );
122 assertEquals( "check cache size", 0, cache.size() );
124 String value = "value";
127 cache.put( key, value );
128 assertEquals( "check cache hit", value, cache.get( key ) );
129 assertEquals( (double) 1, cache.getHitRate(), 0 );
130 assertEquals( "check cache size", 1, cache.size() );
131 assertNull( "check cache miss", cache.get( "none" ) );
132 assertEquals( (double) 0.5, cache.getHitRate(), 0 );
134 assertNull( "check flushed object", cache.get( "key" ) );
135 assertEquals( (double) 0, cache.getHitRate(), 0 );
136 assertEquals( "check flushed cache size", 0, cache.size() );