1 package org.columba.core.io; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.util.Random ; 8 9 import junit.framework.TestCase; 10 11 public class StreamCacheTest extends TestCase { 12 13 File tempDir = new File ("test_temp/"); 14 15 public void testAdd() throws IOException { 16 byte[] random = new byte[1000]; 17 new Random ().nextBytes(random); 18 19 StreamCache cache = new StreamCache(tempDir); 20 21 InputStream in = cache.passiveAdd("test1",new ByteArrayInputStream (random)); 22 23 byte[] test = new byte[1000]; 24 25 in.read(test); 26 in.close(); 27 28 assertEquals(1000, cache.getActSize()); 29 30 in = cache.get("test1"); 31 in.read(test); 32 in.close(); 33 34 for( int i=0; i<1000; i++) { 35 assertEquals(test[i], random[i]); 36 } 37 38 cache.clear(); 39 40 assertEquals( 0, cache.getActSize()); 41 42 assertEquals( 0, tempDir.list().length); 43 } 44 45 public void testMaxsize() throws IOException , InterruptedException { 46 byte[] random1 = new byte[1000]; 47 byte[] random2 = new byte[1000]; 48 byte[] random3 = new byte[1000]; 49 new Random ().nextBytes(random1); 50 new Random ().nextBytes(random2); 51 new Random ().nextBytes(random3); 52 53 StreamCache cache = new StreamCache(tempDir, 1700); 54 55 InputStream in = cache.passiveAdd("test1",new ByteArrayInputStream (random1)); 56 57 byte[] test = new byte[1000]; 58 59 in.read(test); 60 in.close(); 61 62 assertEquals(1000, cache.getActSize()); 63 64 Thread.sleep(100); 65 66 in = cache.passiveAdd("test2", new ByteArrayInputStream (random2)); 67 in.read(test); 68 in.close(); 69 70 assertEquals(1000, cache.getActSize()); 71 assertEquals(null, cache.get("test1")); 72 73 74 in = cache.get("test2"); 75 in.read(test); 76 in.close(); 77 78 for( int i=0; i<1000; i++) { 79 assertEquals(test[i], random2[i]); 80 } 81 82 cache.setMaxSize(2000); 83 in = cache.passiveAdd("test3", new ByteArrayInputStream (random3)); 84 in.read(test); 85 in.close(); 86 87 assertEquals(2000, cache.getActSize()); 88 89 in = cache.get("test3"); 90 in.read(test); 91 in.close(); 92 93 for( int i=0; i<1000; i++) { 94 assertEquals(test[i], random3[i]); 95 } 96 97 cache.clear(); 98 99 assertEquals( 0, cache.getActSize()); 100 101 assertEquals( 0, tempDir.list().length); 102 } 103 104 protected void tearDown() throws Exception { 105 File [] rest = tempDir.listFiles(); 106 for( int i=0; i<rest.length; i++) { 107 rest[i].delete(); 108 } 109 110 } 111 } 112 | Popular Tags |