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