001: package org.mortbay.jetty;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005:
006: import org.mortbay.jetty.ResourceCache.Content;
007: import org.mortbay.resource.Resource;
008: import org.mortbay.resource.ResourceFactory;
009:
010: import junit.framework.TestCase;
011:
012: public class ResourceCacheTest extends TestCase {
013: Resource directory;
014: File[] files = new File[10];
015: String[] names = new String[files.length];
016: ResourceCache cache = new ResourceCache(new MimeTypes());
017: ResourceFactory factory;
018:
019: /* ------------------------------------------------------------ */
020: /* (non-Javadoc)
021: * @see junit.framework.TestCase#setUp()
022: */
023: protected void setUp() throws Exception {
024: for (int i = 0; i < files.length; i++) {
025: files[i] = File.createTempFile("RCT", ".txt");
026: files[i].deleteOnExit();
027: names[i] = files[i].getName();
028: FileOutputStream out = new FileOutputStream(files[i]);
029: for (int j = 0; j < (i * 10 - 1); j++)
030: out.write(' ');
031: out.write('\n');
032: out.close();
033: }
034:
035: directory = Resource.newResource(files[0].getParentFile()
036: .getAbsolutePath());
037:
038: factory = new ResourceFactory() {
039: public Resource getResource(String path) {
040: try {
041: return directory.addPath(path);
042: } catch (Exception e) {
043: return null;
044: }
045: }
046:
047: };
048: cache.setMaxCacheSize(95);
049: cache.setMaxCachedFileSize(85);
050: cache.setMaxCachedFiles(4);
051: cache.start();
052: }
053:
054: /* ------------------------------------------------------------ */
055: /* (non-Javadoc)
056: * @see junit.framework.TestCase#tearDown()
057: */
058: protected void tearDown() throws Exception {
059: cache.stop();
060: }
061:
062: /* ------------------------------------------------------------ */
063: public void testResourceCache() throws Exception {
064: assertTrue(cache.lookup("does not exist", factory) == null);
065: assertTrue(cache.lookup(names[9], factory) == null);
066:
067: Content content;
068: content = cache.lookup(names[8], factory);
069: assertTrue(content != null);
070: assertEquals(80, content.getContentLength());
071:
072: assertEquals(80, cache.getCachedSize());
073: assertEquals(1, cache.getCachedFiles());
074:
075: content = cache.lookup(names[1], factory);
076: assertEquals(90, cache.getCachedSize());
077: assertEquals(2, cache.getCachedFiles());
078:
079: content = cache.lookup(names[2], factory);
080: assertEquals(30, cache.getCachedSize());
081: assertEquals(2, cache.getCachedFiles());
082:
083: content = cache.lookup(names[3], factory);
084: assertEquals(60, cache.getCachedSize());
085: assertEquals(3, cache.getCachedFiles());
086:
087: content = cache.lookup(names[4], factory);
088: assertEquals(90, cache.getCachedSize());
089: assertEquals(3, cache.getCachedFiles());
090:
091: content = cache.lookup(names[5], factory);
092: assertEquals(90, cache.getCachedSize());
093: assertEquals(2, cache.getCachedFiles());
094:
095: content = cache.lookup(names[6], factory);
096: assertEquals(60, cache.getCachedSize());
097: assertEquals(1, cache.getCachedFiles());
098:
099: FileOutputStream out = new FileOutputStream(files[6]);
100: out.write(' ');
101: out.close();
102: content = cache.lookup(names[7], factory);
103: assertEquals(70, cache.getCachedSize());
104: assertEquals(1, cache.getCachedFiles());
105:
106: content = cache.lookup(names[6], factory);
107: assertEquals(71, cache.getCachedSize());
108: assertEquals(2, cache.getCachedFiles());
109:
110: content = cache.lookup(names[0], factory);
111: assertEquals(72, cache.getCachedSize());
112: assertEquals(3, cache.getCachedFiles());
113:
114: content = cache.lookup(names[1], factory);
115: assertEquals(82, cache.getCachedSize());
116: assertEquals(4, cache.getCachedFiles());
117:
118: content = cache.lookup(names[2], factory);
119: assertEquals(32, cache.getCachedSize());
120: assertEquals(4, cache.getCachedFiles());
121:
122: content = cache.lookup(names[3], factory);
123: assertEquals(61, cache.getCachedSize());
124: assertEquals(4, cache.getCachedFiles());
125:
126: cache.flushCache();
127: assertEquals(0, cache.getCachedSize());
128: assertEquals(0, cache.getCachedFiles());
129:
130: }
131: }
|