1   /*
2    * Copyright 2001-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.cache;
17  
18  import java.io.File;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  public class TestShareableFileStash extends TestCase {
25     public TestShareableFileStash(String testName) {
26        super(testName);
27     }
28  
29     public static void main(String args[]) {
30        String[] testCaseName = { TestShareableFileStash.class.getName() };
31        junit.textui.TestRunner.main(testCaseName);
32     }
33  
34     public static Test suite() {
35        return new TestSuite(TestShareableFileStash.class);
36     }
37  
38     private Cache cache = null;
39     private File basedir = new File("cachetest");
40  
41     public void setUp() {
42        basedir.mkdir();
43        cache = new SimpleCache(
44                       new ShareableFileStash(new File(basedir.getAbsoluteFile(),"cachedir"),10,32),
45                       null,
46                       null,
47                       null,
48                       null
49                    );
50     }
51  
52     public void tearDown() {
53        // basedir.delete();
54     }
55  
56     public void testLong() throws Exception {
57  
58        System.out.println("NUM_RETRIEVE_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_FOUND));
59        System.out.println("NUM_RETRIEVE_NOT_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_NOT_FOUND));
60        System.out.println("NUM_RETRIEVE_REQUESTED: " + cache.getStat(CacheStat.NUM_RETRIEVE_REQUESTED));
61        System.out.println("NUM_STORE_NOT_STORED: " + cache.getStat(CacheStat.NUM_STORE_NOT_STORED));
62        System.out.println("NUM_STORE_REQUESTED: " + cache.getStat(CacheStat.NUM_STORE_REQUESTED));
63        System.out.println("NUM_STORE_STORED: " + cache.getStat(CacheStat.NUM_STORE_STORED));
64        System.out.println("CUR_CAPACITY: " + cache.getStat(CacheStat.CUR_CAPACITY));
65  
66        StringBuffer buf = new StringBuffer();
67  
68        for(int i=0;i<3;i++) {
69           buf.append("All your base are now belong to us. ");
70        }
71        for(int i=0;i<1500;i++) {
72           String key = "this is the key " + i;
73           assertTrue("object " + i + " should be storeable",cache.store(key,buf,new Long(System.currentTimeMillis()+600000L),null));
74           assertTrue("object " + i + " should be in the cache",cache.contains(key));
75        }
76        System.out.println("NUM_RETRIEVE_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_FOUND));
77        System.out.println("NUM_RETRIEVE_NOT_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_NOT_FOUND));
78        System.out.println("NUM_RETRIEVE_REQUESTED: " + cache.getStat(CacheStat.NUM_RETRIEVE_REQUESTED));
79        System.out.println("NUM_STORE_NOT_STORED: " + cache.getStat(CacheStat.NUM_STORE_NOT_STORED));
80        System.out.println("NUM_STORE_REQUESTED: " + cache.getStat(CacheStat.NUM_STORE_REQUESTED));
81        System.out.println("NUM_STORE_STORED: " + cache.getStat(CacheStat.NUM_STORE_STORED));
82        System.out.println("CUR_CAPACITY: " + cache.getStat(CacheStat.CUR_CAPACITY));
83  
84        int found = 0;
85        for(int i=0;i<1500;i++) {
86           String key = "this is the key " + i;
87           if(null != cache.retrieve(key)) {
88              found++;
89           }
90        }
91        System.out.println("found: " + found);
92  
93        System.out.println("NUM_RETRIEVE_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_FOUND));
94        System.out.println("NUM_RETRIEVE_NOT_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_NOT_FOUND));
95        System.out.println("NUM_RETRIEVE_REQUESTED: " + cache.getStat(CacheStat.NUM_RETRIEVE_REQUESTED));
96        System.out.println("NUM_STORE_NOT_STORED: " + cache.getStat(CacheStat.NUM_STORE_NOT_STORED));
97        System.out.println("NUM_STORE_REQUESTED: " + cache.getStat(CacheStat.NUM_STORE_REQUESTED));
98        System.out.println("NUM_STORE_STORED: " + cache.getStat(CacheStat.NUM_STORE_STORED));
99        System.out.println("CUR_CAPACITY: " + cache.getStat(CacheStat.CUR_CAPACITY));
100 
101       cache.clear();
102       Thread.yield();
103       Thread.sleep(2000L);
104       {
105           found = 0;
106           for(int i=0;i<1500;i++) {
107              String key = "this is the key " + i;
108              if(null != cache.retrieve(key)) {
109                 found++;
110              }
111           }
112           System.out.println("found: " + found);
113 
114           System.out.println("NUM_RETRIEVE_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_FOUND));
115           System.out.println("NUM_RETRIEVE_NOT_FOUND: " + cache.getStat(CacheStat.NUM_RETRIEVE_NOT_FOUND));
116           System.out.println("NUM_RETRIEVE_REQUESTED: " + cache.getStat(CacheStat.NUM_RETRIEVE_REQUESTED));
117           System.out.println("NUM_STORE_NOT_STORED: " + cache.getStat(CacheStat.NUM_STORE_NOT_STORED));
118           System.out.println("NUM_STORE_REQUESTED: " + cache.getStat(CacheStat.NUM_STORE_REQUESTED));
119           System.out.println("NUM_STORE_STORED: " + cache.getStat(CacheStat.NUM_STORE_STORED));
120           System.out.println("CUR_CAPACITY: " + cache.getStat(CacheStat.CUR_CAPACITY));
121       }
122    }
123 }