View Javadoc
1   package com.atlassian.plugin.osgi.container;
2   
3   import com.atlassian.plugin.osgi.container.impl.DefaultOsgiPersistentCache;
4   import com.atlassian.plugin.test.PluginTestUtils;
5   import junit.framework.TestCase;
6   import org.apache.commons.io.FileUtils;
7   
8   import java.io.File;
9   import java.io.IOException;
10  
11  public class TestDefaultOsgiPersistentCache extends TestCase {
12      private File tmpDir;
13  
14      @Override
15      protected void setUp() throws Exception {
16          super.setUp();
17          tmpDir = PluginTestUtils.createTempDirectory(TestDefaultOsgiPersistentCache.class);
18      }
19  
20      public void testRecordLastVersion() throws IOException {
21          DefaultOsgiPersistentCache cache = new DefaultOsgiPersistentCache(tmpDir);
22          File versionFile = new File(new File(tmpDir, "transformed-plugins"), "cache.key");
23          cache.validate("1.0");
24          assertTrue(versionFile.exists());
25          String txt = FileUtils.readFileToString(versionFile);
26  
27          //"e8dc057d3346e56aed7cf252185dbe1fa6454411" == SHA1("1.0").
28          assertEquals(String.valueOf("e8dc057d3346e56aed7cf252185dbe1fa6454411"), txt);
29      }
30  
31      public void testCleanOnUpgrade() throws IOException {
32          DefaultOsgiPersistentCache cache = new DefaultOsgiPersistentCache(tmpDir);
33          File tmp = File.createTempFile("foo", ".txt", new File(tmpDir, "transformed-plugins"));
34          cache.validate("1.0");
35          assertTrue(tmp.exists());
36          cache.validate("2.0");
37          assertFalse(tmp.exists());
38      }
39  
40      public void testNullVersion() throws IOException {
41          DefaultOsgiPersistentCache cache = new DefaultOsgiPersistentCache(tmpDir);
42          cache.validate(null);
43          File tmp = File.createTempFile("foo", ".txt", new File(tmpDir, "transformed-plugins"));
44          assertTrue(tmp.exists());
45          cache.validate(null);
46          assertTrue(tmp.exists());
47      }
48  
49      @Override
50      protected void tearDown() throws Exception {
51          super.tearDown();
52          FileUtils.cleanDirectory(tmpDir);
53      }
54  }