1 package com.atlassian.plugin.osgi.factory.transform;
2
3 import com.atlassian.plugin.test.PluginJarBuilder;
4 import com.google.common.collect.ImmutableMap;
5 import com.google.common.collect.Iterables;
6 import org.junit.Test;
7
8 import java.io.File;
9 import java.util.concurrent.atomic.AtomicBoolean;
10 import java.util.jar.JarEntry;
11 import java.util.jar.Manifest;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertSame;
18 import static org.junit.Assert.assertTrue;
19
20 public class TestJarUtils {
21
22 @Test
23 public void testEntries() throws Exception {
24 final File plugin = new PluginJarBuilder().addResource("foo", "bar").build();
25 final Iterable<JarEntry> entries = JarUtils.getEntries(plugin);
26 assertFalse("must contain some entry", Iterables.isEmpty(entries));
27 final JarEntry entry = Iterables.get(entries, 0);
28 assertEquals("foo", entry.getName());
29 }
30
31 @Test
32 public void testNoEntries() throws Exception {
33
34 final File plugin = new PluginJarBuilder().build();
35 final Iterable<JarEntry> entries = JarUtils.getEntries(plugin);
36 final JarEntry manifest = Iterables.getOnlyElement(entries);
37 assertNotNull(manifest);
38 assertEquals("META-INF/MANIFEST.MF", manifest.getName());
39 }
40
41 @Test
42 public void testHasManifestEntryReturnsFalseWithNoManifest() {
43 assertFalse(JarUtils.hasManifestEntry(null, "entry"));
44 }
45
46 @Test
47 public void testHasManifestEntryReturnsFalseWithEmptyManifest() {
48 assertFalse(JarUtils.hasManifestEntry(new Manifest(), "entry"));
49 }
50
51 @Test
52 public void testHasManifestEntryReturnsFalseWithManifest() throws Exception {
53 final File plugin = new PluginJarBuilder().manifest(ImmutableMap.of("entry", "foo")).build();
54 assertTrue(JarUtils.hasManifestEntry(JarUtils.getManifest(plugin), "entry"));
55 }
56
57 @Test
58 public void testEntry() throws Exception {
59 final File plugin = new PluginJarBuilder().addResource("foo", "bar").build();
60 final JarEntry entry = JarUtils.getEntry(plugin, "foo");
61 assertNotNull(entry);
62 assertEquals("foo", entry.getName());
63 }
64
65 @Test
66 public void testEntryNotFound() throws Exception {
67 final File plugin = new PluginJarBuilder().addResource("foo", "bar").build();
68 final JarEntry entry = JarUtils.getEntry(plugin, "bar");
69 assertNull(entry);
70 }
71
72 @Test
73 public void testManifest() throws Exception {
74 final File plugin = new PluginJarBuilder().build();
75 final Manifest manifest = JarUtils.getManifest(plugin);
76 assertNotNull(manifest);
77 }
78
79 @Test
80 public void testManifestCreatedIfNotPresent() throws Exception {
81 final File plugin = new PluginJarBuilder().addResource("foo", "bar").buildWithNoManifest();
82 final Manifest manifest = JarUtils.getManifest(plugin);
83 assertNotNull(manifest);
84 }
85
86 @Test
87 public void testExtractor() throws Exception {
88 final File plugin = new PluginJarBuilder().addResource("dooby", "whacker").build();
89 final Object expected = new Object();
90 final AtomicBoolean called = new AtomicBoolean(false);
91 final Object result = JarUtils.withJar(plugin, jar -> {
92 final JarEntry entry = jar.getJarEntry("dooby");
93 assertNotNull(entry);
94 final JarEntry nonEntry = jar.getJarEntry("blot");
95 assertNull(nonEntry);
96 called.set(true);
97 return expected;
98 });
99 assertTrue(called.get());
100 assertSame(expected, result);
101 }
102 }