1 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 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 29 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 42 43 public class TestFileCache extends JetspeedTestCase implements FileCacheEventListener 44 { 45 String refreshedEntry = null; 46 47 52 public TestFileCache( String name ) { 53 super( name ); 54 } 55 56 61 public static void main(String args[]) 62 { 63 junit.awtui.TestRunner.main( new String [] { TestFileCache.class.getName() } ); 64 } 65 66 72 public static Test suite() 73 { 74 return new TestSuite( TestFileCache.class ); 76 } 77 78 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 FileCache cache = new FileCache(10, 20); 96 97 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 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 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 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 215 | Popular Tags |