001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jetspeed.cache.file;
019:
020: import java.io.BufferedInputStream;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.util.Date;
024: import java.util.Iterator;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.commons.io.StreamUtils;
031: import org.apache.commons.lang.exception.ExceptionUtils;
032:
033: /**
034: * Unit test for FileCache
035: *
036: * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
037: * @version $Id: TestFileCache.java 516448 2007-03-09 16:25:47Z ate $
038: */
039:
040: public class TestFileCache extends TestCase implements
041: FileCacheEventListener {
042: protected static final String TEST_DIRECTORY = "./testdata";
043: protected static final int CACHE_SIZE = 20;
044: protected static final int SCAN_RATE = 10;
045: String refreshedEntry = null;
046:
047: /**
048: * Creates the test suite.
049: *
050: * @return a test suite (<code>TestSuite</code>) that includes all methods
051: * starting with "test"
052: */
053: public static Test suite() {
054: // All methods starting with "test" will be executed in the test suite.
055: return new TestSuite(TestFileCache.class);
056: }
057:
058: private FileCache cache = null;
059:
060: protected void setUp() throws Exception {
061: super .setUp();
062:
063: cache = new FileCache(SCAN_RATE, CACHE_SIZE);
064: }
065:
066: /**
067: * @see junit.framework.TestCase#tearDown()
068: */
069: public void tearDown() throws Exception {
070: super .tearDown();
071: removeTestFiles();
072: }
073:
074: /**
075: * Tests loading the cache
076: * @throws Exception
077: */
078:
079: public void testLoadCache() throws Exception {
080: String templateFile = TEST_DIRECTORY + "/default.psml";
081: try {
082: File file = new File(templateFile);
083: assertTrue(file.exists());
084:
085: createTestFiles(templateFile);
086:
087: // create the Cache wake up after 10 seconds, cache size 20
088: // FileCache cache = new FileCache(10, 20);
089:
090: // load the Cache
091: File directory = new File(TEST_DIRECTORY);
092: File[] files = directory.listFiles();
093: for (int ix = 0; ix < files.length; ix++) {
094: if (files[ix].isDirectory()
095: || files[ix].getName().equals(".cvsignore")) {
096: continue;
097: }
098: String testData = readFile(files[ix]);
099: cache.put(files[ix], testData);
100: }
101:
102: assertTrue(cache.getSize() == 31);
103:
104: dumpCache(cache.getIterator());
105:
106: cache.addListener(this );
107:
108: // start the cache's scanner
109: cache.startFileScanner();
110:
111: Thread.sleep(2000);
112:
113: assertTrue(cache.getSize() == 20);
114:
115: dumpCache(cache.getIterator());
116:
117: // Reload files array to get the files back in the correct order
118: // because the cache CAN have reordered them while evicting.
119: // This can happen if test files where left over from a previous
120: // test which then will have an older timestamp.
121: // In that case it is NOT garanteed that files[18] below will still
122: // be in the cache!
123: // Note: this is only an issue for the test itself and not for the
124: // cache as such.
125:
126: Iterator it = cache.getIterator();
127: for (int ix = 0; it.hasNext(); ix++) {
128: FileCacheEntry entry = (FileCacheEntry) it.next();
129: files[ix] = entry.getFile();
130: }
131:
132: String stuff = (String) cache.getDocument(files[18]
133: .getCanonicalPath());
134: assertNotNull(stuff);
135:
136: files[18].setLastModified(new Date().getTime());
137:
138: Thread.sleep(9000);
139:
140: assertNotNull(refreshedEntry);
141: System.out.println("refreshed entry = " + refreshedEntry);
142:
143: cache.stopFileScanner();
144:
145: // evict all from cache
146: cache.evictAll();
147: assertTrue(cache.getSize() == 0);
148:
149: removeTestFiles();
150: } catch (Exception e) {
151: fail(ExceptionUtils.getStackTrace(e));
152: }
153:
154: System.out.println("Completed loadCache Test OK ");
155:
156: }
157:
158: private void createTestFiles(String templateFile)
159: throws java.io.IOException {
160: for (int ix = 1; ix < 31; ix++) {
161: String testFile = TEST_DIRECTORY + "/testFile-" + ix
162: + ".psml";
163: FileCopy.copy(templateFile, testFile);
164: }
165: }
166:
167: private void removeTestFiles() {
168: for (int ix = 1; ix < 31; ix++) {
169: String testFile = TEST_DIRECTORY + "/testFile-" + ix
170: + ".psml";
171: File file = new File(testFile);
172: if (file.exists())
173: file.delete();
174: }
175: }
176:
177: private String readFile(File file) throws java.io.IOException,
178: java.io.FileNotFoundException {
179: BufferedInputStream input;
180:
181: input = new BufferedInputStream(new FileInputStream(file));
182: String result = StreamUtils.streamAsString(input);
183: input.close();
184: return result;
185: }
186:
187: /**
188: * Refresh event, called when the entry is being refreshed from file system.
189: *
190: * @param entry the entry being refreshed.
191: */
192: public void refresh(FileCacheEntry entry) {
193: System.out.println("entry is refreshing: "
194: + entry.getFile().getName());
195: this .refreshedEntry = entry.getFile().getName();
196: }
197:
198: /**
199: * Evict event, called when the entry is being evicted out of the cache
200: *
201: * @param entry the entry being refreshed.
202: */
203: public void evict(FileCacheEntry entry) {
204: System.out.println("entry is evicting: "
205: + entry.getFile().getName());
206: }
207:
208: private void dumpCache(Iterator it) {
209: for (; it.hasNext();) {
210: FileCacheEntry entry = (FileCacheEntry) it.next();
211: System.out.println(entry.getFile().getName());
212: }
213: }
214:
215: }
|