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