KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > cache > TestFileCache


1 /*
2  * Copyright 2000-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
17 package org.apache.jetspeed.cache;
18
19 import java.util.Iterator JavaDoc;
20 import java.io.File JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24
25 // Junit imports
26
import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 // Jetspeed imports
30
import org.apache.jetspeed.test.JetspeedTestCase;
31 import org.apache.jetspeed.util.FileCopy;
32 import org.apache.jetspeed.util.Streams;
33 import org.apache.turbine.util.StringUtils;
34
35
36 /**
37  * Unit test for FileCache
38  *
39  * @author <a HREF="mailto:david@bluesunrise.com">David Sean Taylor</a>
40  * @version $Id: TestFileCache.java,v 1.1 2004/04/07 22:02:42 jford Exp $
41  */

42
43 public class TestFileCache extends JetspeedTestCase implements FileCacheEventListener
44 {
45     String JavaDoc refreshedEntry = null;
46
47     /**
48      * Defines the testcase name for JUnit.
49      *
50      * @param name the testcase's name.
51      */

52     public TestFileCache( String JavaDoc name ) {
53         super( name );
54     }
55     
56     /**
57      * Start the tests.
58      *
59      * @param args the arguments. Not used
60      */

61     public static void main(String JavaDoc args[])
62     {
63         junit.awtui.TestRunner.main( new String JavaDoc[] { TestFileCache.class.getName() } );
64     }
65  
66     /**
67      * Creates the test suite.
68      *
69      * @return a test suite (<code>TestSuite</code>) that includes all methods
70      * starting with "test"
71      */

72     public static Test suite()
73     {
74         // All methods starting with "test" will be executed in the test suite.
75
return new TestSuite( TestFileCache.class );
76     }
77
78     /**
79      * Tests loading the cache
80      * @throws Exception
81      */

82
83     public void testLoadCache() throws Exception JavaDoc
84     {
85         String JavaDoc templateFile = "test/testdata/psml/user/cachetest/default.psml";
86         try
87         {
88             File JavaDoc file = new File JavaDoc(templateFile);
89             System.out.println("File = " + file.getCanonicalPath());
90             assertTrue(file.exists());
91
92             createTestFiles(templateFile);
93
94             // create the Cache wake up after 10 seconds, cache size 20
95
FileCache cache = new FileCache(10, 20);
96
97             // load the Cache
98
File JavaDoc directory = new File JavaDoc("test/testdata/psml/user/cachetest/");
99             File JavaDoc[] files = directory.listFiles();
100             for (int ix=0; ix < files.length; ix++)
101             {
102                 if (files[ix].isDirectory())
103                 {
104                     continue;
105                 }
106                 String JavaDoc testData = readFile(files[ix]);
107                 cache.put(files[ix], testData);
108             }
109
110             assertTrue(cache.getSize() == 31);
111
112             dumpCache(cache.getIterator());
113
114             cache.addListener(this);
115             // start the cache's scanner
116
cache.startFileScanner();
117
118             Thread.sleep(2000);
119
120             assertTrue(cache.getSize() == 20);
121
122             dumpCache(cache.getIterator());
123
124             String JavaDoc stuff = (String JavaDoc) cache.getDocument(files[18].getCanonicalPath());
125             assertNotNull(stuff);
126
127             files[18].setLastModified(new Date JavaDoc().getTime());
128
129
130             Thread.sleep(9000);
131
132             assertNotNull(refreshedEntry);
133             System.out.println("refreshed entry = " + refreshedEntry);
134
135             cache.stopFileScanner();
136
137             removeTestFiles();
138         }
139         catch (Exception JavaDoc e)
140         {
141             fail(StringUtils.stackTrace(e));
142         }
143
144         System.out.println("Completed loadCache Test OK ");
145
146     }
147
148     private void createTestFiles(String JavaDoc templateFile)
149         throws java.io.IOException JavaDoc
150     {
151         for (int ix=1; ix < 31; ix++)
152         {
153             String JavaDoc testFile = "test/testdata/psml/user/cachetest/testFile-" + ix + ".psml";
154             FileCopy.copy(templateFile, testFile);
155         }
156     }
157
158     private void removeTestFiles()
159     {
160         for (int ix=1; ix < 31; ix++)
161         {
162             String JavaDoc testFile = "test/testdata/psml/user/cachetest/testFile-" + ix + ".psml";
163             File JavaDoc file = new File JavaDoc(testFile);
164             file.delete();
165         }
166     }
167
168     private String JavaDoc readFile(File JavaDoc file)
169         throws java.io.IOException JavaDoc, java.io.FileNotFoundException JavaDoc
170     {
171         BufferedInputStream JavaDoc input;
172
173         input = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
174         String JavaDoc result = Streams.getAsString(input);
175         input.close();
176         return result;
177     }
178
179     /**
180      * Refresh event, called when the entry is being refreshed from file system.
181      *
182      * @param entry the entry being refreshed.
183      */

184     public void refresh(FileCacheEntry entry)
185     {
186         System.out.println("entry is refreshing: " + entry.getFile().getName());
187         this.refreshedEntry = entry.getFile().getName();
188     }
189
190     /**
191      * Evict event, called when the entry is being evicted out of the cache
192      *
193      * @param entry the entry being refreshed.
194      */

195     public void evict(FileCacheEntry entry)
196     {
197         System.out.println("entry is evicting: " + entry.getFile().getName());
198     }
199
200     private void dumpCache(Iterator JavaDoc it)
201     {
202         for ( ; it.hasNext(); )
203         {
204             FileCacheEntry entry = (FileCacheEntry) it.next();
205             System.out.println(entry.getFile().getName());
206         }
207     }
208 }
209
210
211
212
213
214
215
Popular Tags