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