View Javadoc

1   package com.atlassian.plugin.cache.filecache.impl;
2   
3   import junit.framework.TestCase;
4   import org.apache.commons.io.FileUtils;
5   
6   import java.io.ByteArrayOutputStream;
7   import java.io.File;
8   import java.io.IOException;
9   import java.util.concurrent.atomic.AtomicLong;
10  
11  public class TestFileCacheImpl extends TestCase {
12      File tmpDir;
13  
14      @Override
15      protected void setUp() throws Exception {
16          tmpDir = File.createTempFile("TestCachedFile", "test");
17          tmpDir.delete();
18          tmpDir.mkdirs();
19      }
20  
21      public void testNormalOperation() throws Exception {
22          FileCacheImpl<String> cache = new FileCacheImpl<String>(tmpDir, 100, new AtomicLong(0));
23          final ByteArrayOutputStream out = new ByteArrayOutputStream();
24  
25          MockFileCacheStreamProvider in1 = new MockFileCacheStreamProvider("contents1");
26          cache.stream("key1", out, in1);
27          assertEquals(1, in1.callCount);
28          assertEquals("contents1", new String(out.toByteArray()));
29          assertCacheFile("contents1", 1);
30  
31          out.reset();
32          cache.stream("key1", out, in1);
33          assertEquals(1, in1.callCount);
34          assertEquals("contents1", new String(out.toByteArray()));
35          assertCacheFile("contents1", 1);
36  
37          MockFileCacheStreamProvider in2 = new MockFileCacheStreamProvider("contents2");
38          out.reset();
39          cache.stream("key2", out, in2);
40          assertEquals(1, in2.callCount);
41          assertEquals("contents2", new String(out.toByteArray()));
42          assertCacheFile("contents1", 1);
43          assertCacheFile("contents2", 2);
44      }
45  
46      public void testEviction() throws Exception {
47          FileCacheImpl<String> cache = new FileCacheImpl<String>(tmpDir, 0, new AtomicLong(0)); // zero tells map-maker to evict immediately
48  
49          final ByteArrayOutputStream out = new ByteArrayOutputStream();
50  
51          MockFileCacheStreamProvider in1 = new MockFileCacheStreamProvider("contents1");
52          cache.stream("key1", out, in1);
53          assertEquals(1, in1.callCount);
54          assertEquals("contents1", new String(out.toByteArray()));
55          assertCacheFileAbsent(1);
56  
57          out.reset();
58          cache.stream("key1", out, in1);
59          assertEquals(2, in1.callCount);
60          assertEquals("contents1", new String(out.toByteArray()));
61          assertCacheFileAbsent(1);
62      }
63      
64      public void testMissingInitialDirectory() throws IOException 
65      {
66          tmpDir.delete();
67          assertFalse(tmpDir.exists());
68          FileCacheImpl<String> cache = new FileCacheImpl<String>(tmpDir, 100, new AtomicLong(0));
69          assertTrue(tmpDir.exists());
70          assertTrue(tmpDir.isDirectory());
71      }
72      public void testClearFilesInConstructor() throws IOException 
73      {
74          File f1 = new File(tmpDir, "1" + FileCacheImpl.EXT);
75          f1.createNewFile();
76          File f2 = new File(tmpDir, "2" + FileCacheImpl.EXT);
77          f2.createNewFile();
78          
79          assertTrue(f1.isFile());
80          assertTrue(f2.isFile());
81          FileCacheImpl<String> cache = new FileCacheImpl<String>(tmpDir, 100, new AtomicLong(0));
82          assertTrue(tmpDir.exists());
83          assertTrue(tmpDir.isDirectory());
84          assertFalse(f1.isFile());
85          assertFalse(f2.isFile());
86      }
87      
88      public void testClearCache() throws Exception
89      {
90          FileCacheImpl<String> cache = new FileCacheImpl<String>(tmpDir, 100, new AtomicLong(0));
91          
92          MockFileCacheStreamProvider in1 = new MockFileCacheStreamProvider("contents1");
93          cache.stream("key1", new ByteArrayOutputStream(), in1);
94          assertCacheFile("contents1", 1);
95          
96          cache.clear();
97          assertCacheFileAbsent(1);
98  
99          // recreate with same key -- should get a different file number
100         cache.stream("key1", new ByteArrayOutputStream(), in1);
101         assertCacheFile("contents1", 2);
102     }
103 
104     public void testSharedFilenameCounter() throws Exception
105     {
106         final AtomicLong filenameCounter = new AtomicLong(0);
107         final FileCacheImpl<String> cache1 = new FileCacheImpl<String>(tmpDir, 100, filenameCounter);
108         final FileCacheImpl<String> cache2 = new FileCacheImpl<String>(tmpDir, 100, filenameCounter);
109 
110         final ByteArrayOutputStream out = new ByteArrayOutputStream();
111         final MockFileCacheStreamProvider in1 = new MockFileCacheStreamProvider("contents1");
112         final MockFileCacheStreamProvider in2 = new MockFileCacheStreamProvider("contents2");
113 
114         cache1.stream("key1", out, in1);
115         cache2.stream("key2", out, in2);
116 
117         assertCacheFile("contents1", 1);
118         assertCacheFile("contents2", 2);
119     }
120 
121     private void assertCacheFile(String contents, int fcount) throws IOException {
122         File f = new File(tmpDir, fcount + FileCacheImpl.EXT);
123         assertTrue(f.exists());
124         assertTrue(f.isFile());
125         assertEquals(contents, FileUtils.readFileToString(f));
126     }
127     private void assertCacheFileAbsent(int fcount) throws IOException {
128         File f = new File(tmpDir, fcount + FileCacheImpl.EXT);
129         assertFalse(f.exists());
130     }
131 }