/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// Package
package KFM.Cache.test;
// Imports
// This application/module packages
import appl.Portal.Test.*;
// Other application/module packages
import junit.framework.*;
import com.coolservlets.util.*;
// KFM packages
import KFM.Cache.*;
// Library classes (JHDK, JSDK, RegExp, ...)
// Java packages
import java.util.*;
import java.io.*;
/**
*
* <P>TestFileCache documentation.</P>
*
* <H2>Usage</H2>
*
* <P>Usage.</P>
*
* <H2>Related classes</H2>
*
* <P>Cut&paste-source was `P_Test�.</P>
*
* <PRE>
* - ServletTestCase -- Father class.
* </PRE>
*
* @version 0.1 (2001 08 29)
* @see ServletTestCase
*/
public class TestFileCache extends ServletTestCase
{
private String mName = "";
private String mCacheDir;
private FileCache mFileCache;
private String mLastCachedKey;
/**
*
* Constructor for this class.
*/
public TestFileCache(String aName) {
super(aName);
}
/**
* Checks if we can add content to cache and retrieve it.
*/
public void testAdd()
{
assertTrue("CacheDir not defined in properties file", mCacheDir != null);
int tCacheMaxSize = 100000; // bytes
int tCacheMaxLifetime = -1; // never expire
emptyCacheDir();
mFileCache = new FileCache(tCacheMaxSize, tCacheMaxLifetime, mCacheDir);
// write html to cache
String tCacheKey = "cachedfile.html";
String tHTML = "<html>very short HTML</html>";
Cacheable tCacheItem = new CachedHTMLItem(tHTML, tCacheKey);
mFileCache.add(tCacheKey, tCacheItem);
// get html from cache
tCacheItem = mFileCache.get(tCacheKey);
String tCachedContent = ((CachedHTMLItem) tCacheItem).getHTML().trim();
assertTrue("Could not retrieve cashed item: " + tCacheKey, tCacheItem != null);
assertTrue("Cached item doen't contain expected content: " + tCachedContent, tCachedContent.equals(tHTML));
}
/**
* Make cache full, then look if cache size sinks and it is still possible to retrieve a cached item.
* <p>Note: Excpects that testAdd() was previously executed. (Maybe we should use setUp() in future.)
*/
public void testFull()
{
// create big string
StringBuffer tStringBuffer = new StringBuffer("<html>very long HTML...");
for (int i = 0; i < 100; i++) {
tStringBuffer.append(" ");
}
tStringBuffer.append("</html>");
// make cache almost full
String tHTML = tStringBuffer.toString();
int i = 0;
while (mFileCache.getSize() < mFileCache.getMaxSize() * 0.95) {
String tCacheKey = "cachedfile" + i + ".html";
Cacheable tCacheItem = new CachedHTMLItem(tHTML, tCacheKey);
mFileCache.add(tCacheKey, tCacheItem);
i++;
}
// check if the cache size sinks
tHTML = "<html>very short HTML</html>";
int tSizePrev;
i = 0;
do {
tSizePrev = mFileCache.getSize();
String tCacheKey = "newcachedfile" + i + ".html";
Cacheable tCacheItem = new CachedHTMLItem(tHTML, tCacheKey);
mFileCache.add(tCacheKey, tCacheItem);
i++;
} while (tSizePrev < mFileCache.getSize() || i < 100);
assertTrue("Cache size did not sink when full (or could not make cache full)", i != 100);
// get html from cache
mLastCachedKey = "newcachedfile" + (i-1) + ".html";
Cacheable tCacheItem = mFileCache.get(mLastCachedKey);
String tCachedContent = ((CachedHTMLItem) tCacheItem).getHTML().trim();
assertTrue("Could not retrieve cashed item: " + mLastCachedKey, tCacheItem != null);
assertTrue("Cached item doen't contain expected content: " + tCachedContent, tCachedContent.equals(tHTML));
}
/**
* Check if cache recycles existing cache files, check if files were added with correct size information.
* <p>Note: Excpects that testFull() was previously executed. (Maybe we should use setUp() in future.)
*/
public void testRecycle()
{
// check that we have files recycled
int tCacheMaxSize = 100000; // bytes
int tCacheMaxLifetime = -1; // never expire
mFileCache = new FileCache(tCacheMaxSize, tCacheMaxLifetime, mCacheDir);
assertTrue("No files were recycled or cache size calculation wrong", mFileCache.getSize() != 0);
// check that a particular recycled file was added with correct size information:
// remove file from cache, the check if cache size sinks
Cacheable tCacheItem = mFileCache.get(mLastCachedKey);
assertTrue("Could not retrieve recycled cashed item: " + mLastCachedKey, tCacheItem != null);
int tSize = mFileCache.getSize();
mFileCache.remove(mLastCachedKey);
assertTrue("Cached file " + mLastCachedKey + " added with wrong size information: "
+ (tSize - mFileCache.getSize()), tSize > mFileCache.getSize());
}
/**
* Check if a file expires and can't be retrieved anymore.
*/
public void testExpire()
{
int tCacheMaxSize = 100000; // bytes
int tCacheMaxLifetime = 2000; // expire after 2 seconds
emptyCacheDir();
mFileCache = new FileCache(tCacheMaxSize, tCacheMaxLifetime, mCacheDir);
// write html to cache
String tCacheKey = "cachedfile.html";
String tHTML = "<html>very short HTML</html>";
Cacheable tCacheItem = new CachedHTMLItem(tHTML, tCacheKey);
mFileCache.add(tCacheKey, tCacheItem);
// get html from cache
tCacheItem = mFileCache.get(tCacheKey);
String tCachedContent = ((CachedHTMLItem) tCacheItem).getHTML().trim();
assertTrue("Could not retrieve cashed item: " + tCacheKey, tCacheItem != null);
assertTrue("Cached item doen't contain expected content: " + tCachedContent, tCachedContent.equals(tHTML));
// wait so that cache item expires
try { Thread.sleep(2000); } catch (InterruptedException e) {}
// try to get html from cache (should fail)
tCacheItem = mFileCache.get(tCacheKey);
assertTrue("Could retrieve expired cached item: " + tCacheKey, tCacheItem != null);
}
/**
* Checks if we can add content to memory cache and retrieve it.
*/
public void testMemory()
{
// *** Slightly adapted Cut&Paste from testAdd() BEGIN
int tCacheMaxSize = 100000; // bytes
int tCacheMaxLifetime = -1; // never expire
emptyCacheDir();
Cache tMemoryCache = new Cache(tCacheMaxSize, tCacheMaxLifetime);
// write html to cache
String tCacheKey = "cachedfile.html";
String tHTML = "<html>very short HTML</html>";
Cacheable tCacheItem = new CachedHTMLItem(tHTML, tCacheKey);
tMemoryCache.add(tCacheKey, tCacheItem);
// get html from cache
tCacheItem = tMemoryCache.get(tCacheKey);
String tCachedContent = ((CachedHTMLItem) tCacheItem).getHTML().trim();
assertTrue("Could not retrieve cashed item: " + tCacheKey, tCacheItem != null);
assertTrue("Cached item doen't contain expected content: " + tCachedContent, tCachedContent.equals(tHTML));
// *** Slightly adapted Cut&Paste from testAdd() END
}
/**
* Creates clean cache directory. Removes all files from previous runs.
* <p>Note: You must invoke it before the FileCache constructor, otherwise you get strange behavior.
*/
private void emptyCacheDir()
{
if (mCacheDir == null) {
return;
}
File tDir = new File(mCacheDir);
if (tDir.exists()) {
// delete old files in cache
String[] tFiles = tDir.list();
for (int i=0; i < tFiles.length; i++) {
new File(tDir, tFiles[i]).delete();
}
} else {
if (! tDir.mkdirs()) {
throw new RuntimeException("FileCache: could not create cache "
+ "directory " + mCacheDir);
}
}
}
/**
* Create empty file with arbitrary length. Useful to simulate files in the cache.
*/
public static void createEmptyFile(String aFilename, long length) throws FileNotFoundException, IOException
{
RandomAccessFile tFile = new RandomAccessFile(aFilename, "rw");
try {
tFile.seek(length - 1);
tFile.writeByte(0); // write something so that the file gets its new size
} catch (IOException ex) {
throw ex;
}
}
public void initialize() {
String tStr;
super.initialize();
// Set all Properties for this class.
// Make sure all members are static.
Properties tProps = getProperties();
tStr = tProps.getProperty("name");
if(null != tStr) {
mName = tStr;
}
mCacheDir = tProps.getProperty("CacheDir");
}
public void setUp()
{
initialize();
}
}