KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > io > StreamCacheTest


1 package org.columba.core.io;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.util.Random JavaDoc;
8
9 import junit.framework.TestCase;
10
11 public class StreamCacheTest extends TestCase {
12
13     File JavaDoc tempDir = new File JavaDoc("test_temp/");
14     
15     public void testAdd() throws IOException JavaDoc {
16         byte[] random = new byte[1000];
17         new Random JavaDoc().nextBytes(random);
18         
19         StreamCache cache = new StreamCache(tempDir);
20         
21         InputStream JavaDoc in = cache.passiveAdd("test1",new ByteArrayInputStream JavaDoc(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 JavaDoc, InterruptedException JavaDoc {
46         byte[] random1 = new byte[1000];
47         byte[] random2 = new byte[1000];
48         byte[] random3 = new byte[1000];
49         new Random JavaDoc().nextBytes(random1);
50         new Random JavaDoc().nextBytes(random2);
51         new Random JavaDoc().nextBytes(random3);
52         
53         StreamCache cache = new StreamCache(tempDir, 1700);
54         
55         InputStream JavaDoc in = cache.passiveAdd("test1",new ByteArrayInputStream JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc {
105         File JavaDoc[] rest = tempDir.listFiles();
106         for( int i=0; i<rest.length; i++) {
107             rest[i].delete();
108         }
109         
110     }
111 }
112
Popular Tags