1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.cache;
18
19 import java.util.Iterator;
20 import java.io.File;
21 import java.util.Date;
22 import java.io.BufferedInputStream;
23 import java.io.FileInputStream;
24
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29
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 refreshedEntry = null;
46
47 /***
48 * Defines the testcase name for JUnit.
49 *
50 * @param name the testcase's name.
51 */
52 public TestFileCache( String 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 args[])
62 {
63 junit.awtui.TestRunner.main( new String[] { 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
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
84 {
85 String templateFile = "test/testdata/psml/user/cachetest/default.psml";
86 try
87 {
88 File file = new File(templateFile);
89 System.out.println("File = " + file.getCanonicalPath());
90 assertTrue(file.exists());
91
92 createTestFiles(templateFile);
93
94
95 FileCache cache = new FileCache(10, 20);
96
97
98 File directory = new File("test/testdata/psml/user/cachetest/");
99 File[] files = directory.listFiles();
100 for (int ix=0; ix < files.length; ix++)
101 {
102 if (files[ix].isDirectory())
103 {
104 continue;
105 }
106 String 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
116 cache.startFileScanner();
117
118 Thread.sleep(2000);
119
120 assertTrue(cache.getSize() == 20);
121
122 dumpCache(cache.getIterator());
123
124 String stuff = (String) cache.getDocument(files[18].getCanonicalPath());
125 assertNotNull(stuff);
126
127 files[18].setLastModified(new Date().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 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 templateFile)
149 throws java.io.IOException
150 {
151 for (int ix=1; ix < 31; ix++)
152 {
153 String 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 testFile = "test/testdata/psml/user/cachetest/testFile-" + ix + ".psml";
163 File file = new File(testFile);
164 file.delete();
165 }
166 }
167
168 private String readFile(File file)
169 throws java.io.IOException, java.io.FileNotFoundException
170 {
171 BufferedInputStream input;
172
173 input = new BufferedInputStream(new FileInputStream(file));
174 String 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 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