1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 5-Jan-2005
6    */
7   package ca.uhn.cache.internal.impl;
8   
9   import java.util.Date;
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  import ca.uhn.cache.CacheReasonEnum;
14  import ca.uhn.cache.VolatilityEnum;
15  import ca.uhn.cache.impl.Chunk;
16  import ca.uhn.cache.impl.Query;
17  import ca.uhn.cache.internal.IChunk;
18  import ca.uhn.cache.internal.IStaleChunkRule;
19  import junit.framework.TestCase;
20  
21  /***
22   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
23   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:21 $ by $Author: bryan_tripp $
24   */
25  public class DefaultStaleChunkRuleTest extends TestCase {
26  
27      /***
28       * Constructor for DefaultStaleChunkRuleTest.
29       * @param theName ...
30       */
31      public DefaultStaleChunkRuleTest(String theName) {
32          super(theName);
33      }
34  
35      /***
36       */
37      public void testGetMaxAge() {
38          long defaultMaxAge = 10 * 60 * 1000;
39          long volatileMax = 5 * 60 * 1000;
40          long recentChangeMax = 4 * 60 * 1000;
41          long veryRecentChangeMax = 1 * 60 * 1000;
42          
43          assertTrue(volatileMax > recentChangeMax); //must be true for later tests to pass 
44          
45          Map volatilityMap = new HashMap();
46          volatilityMap.put(VolatilityEnum.VOLATILE, new Long(volatileMax));
47          
48          Map updateAgeMap = new HashMap();
49          updateAgeMap.put(new Long(60 * 60 * 1000), new Long(recentChangeMax));
50          updateAgeMap.put(new Long(10 * 60 * 1000), new Long(veryRecentChangeMax));
51          IStaleChunkRule rule = new DefaultStaleChunkRule(volatilityMap, updateAgeMap, defaultMaxAge);
52          
53          long old = 120 * 60 * 1000;
54          
55          IChunk c1 = getChunk(VolatilityEnum.STABLE, old);
56          assertEquals(defaultMaxAge, rule.getMaxAge(c1));
57          
58          IChunk c2 = getChunk(VolatilityEnum.VOLATILE, old);
59          assertEquals(volatileMax, rule.getMaxAge(c2));
60          
61          IChunk c3 = getChunk(VolatilityEnum.STABLE, 30 * 60 * 1000);
62          assertEquals(recentChangeMax, rule.getMaxAge(c3));
63          
64          IChunk c4 = getChunk(VolatilityEnum.VOLATILE, 30 * 60 * 1000);
65          assertEquals(recentChangeMax, rule.getMaxAge(c4));
66          
67          IChunk c5 = getChunk(VolatilityEnum.STABLE, 5 * 60 * 1000);
68          assertEquals(veryRecentChangeMax, rule.getMaxAge(c5));
69      }
70      
71      private IChunk getChunk(VolatilityEnum theVolatility, long theUpdateAge) {
72          Date now = new Date();
73          Date then = new Date(System.currentTimeMillis() - theUpdateAge);
74          return new Chunk("chunk", theVolatility, then, now, now, new CacheReasonEnum[0], new Query());
75      }
76  
77  }