001: package org.columba.core.io;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.File;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.util.Random;
008:
009: import junit.framework.TestCase;
010:
011: public class StreamCacheTest extends TestCase {
012:
013: File tempDir = new File("test_temp/");
014:
015: public void testAdd() throws IOException {
016: byte[] random = new byte[1000];
017: new Random().nextBytes(random);
018:
019: StreamCache cache = new StreamCache(tempDir);
020:
021: InputStream in = cache.passiveAdd("test1",
022: new ByteArrayInputStream(random));
023:
024: byte[] test = new byte[1000];
025:
026: in.read(test);
027: in.close();
028:
029: assertEquals(1000, cache.getActSize());
030:
031: in = cache.get("test1");
032: in.read(test);
033: in.close();
034:
035: for (int i = 0; i < 1000; i++) {
036: assertEquals(test[i], random[i]);
037: }
038:
039: cache.clear();
040:
041: assertEquals(0, cache.getActSize());
042:
043: assertEquals(0, tempDir.list().length);
044: }
045:
046: public void testMaxsize() throws IOException, InterruptedException {
047: byte[] random1 = new byte[1000];
048: byte[] random2 = new byte[1000];
049: byte[] random3 = new byte[1000];
050: new Random().nextBytes(random1);
051: new Random().nextBytes(random2);
052: new Random().nextBytes(random3);
053:
054: StreamCache cache = new StreamCache(tempDir, 1700);
055:
056: InputStream in = cache.passiveAdd("test1",
057: new ByteArrayInputStream(random1));
058:
059: byte[] test = new byte[1000];
060:
061: in.read(test);
062: in.close();
063:
064: assertEquals(1000, cache.getActSize());
065:
066: Thread.sleep(100);
067:
068: in = cache.passiveAdd("test2",
069: new ByteArrayInputStream(random2));
070: in.read(test);
071: in.close();
072:
073: assertEquals(1000, cache.getActSize());
074: assertEquals(null, cache.get("test1"));
075:
076: in = cache.get("test2");
077: in.read(test);
078: in.close();
079:
080: for (int i = 0; i < 1000; i++) {
081: assertEquals(test[i], random2[i]);
082: }
083:
084: cache.setMaxSize(2000);
085: in = cache.passiveAdd("test3",
086: new ByteArrayInputStream(random3));
087: in.read(test);
088: in.close();
089:
090: assertEquals(2000, cache.getActSize());
091:
092: in = cache.get("test3");
093: in.read(test);
094: in.close();
095:
096: for (int i = 0; i < 1000; i++) {
097: assertEquals(test[i], random3[i]);
098: }
099:
100: cache.clear();
101:
102: assertEquals(0, cache.getActSize());
103:
104: assertEquals(0, tempDir.list().length);
105: }
106:
107: protected void tearDown() throws Exception {
108: File[] rest = tempDir.listFiles();
109: for (int i = 0; i < rest.length; i++) {
110: rest[i].delete();
111: }
112:
113: }
114: }
|