View Javadoc

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.Iterables;
7   
8   import java.io.File;
9   import java.util.concurrent.atomic.AtomicBoolean;
10  import java.util.jar.JarEntry;
11  import java.util.jar.JarFile;
12  import java.util.jar.Manifest;
13  
14  import junit.framework.TestCase;
15  
16  public class TestJarUtils extends TestCase
17  {
18      public void testEntries() throws Exception
19      {
20          final File plugin = new PluginJarBuilder().addResource("foo", "bar").build();
21          final Iterable<JarEntry> entries = JarUtils.getEntries(plugin);
22          assertFalse("must contain some entry", Iterables.isEmpty(entries));
23          final JarEntry entry = Iterables.get(entries, 0);
24          assertEquals("foo", entry.getName());
25      }
26  
27      public void testNoEntries() throws Exception
28      {
29          // cannot create a completely empty jar so we just use the manifest
30          final File plugin = new PluginJarBuilder().build();
31          final Iterable<JarEntry> entries = JarUtils.getEntries(plugin);
32          final JarEntry manifest = Iterables.getOnlyElement(entries);
33          assertNotNull(manifest);
34          assertEquals("META-INF/MANIFEST.MF", manifest.getName());
35      }
36  
37      public void testEntry() throws Exception
38      {
39          final File plugin = new PluginJarBuilder().addResource("foo", "bar").build();
40          final JarEntry entry = JarUtils.getEntry(plugin, "foo");
41          assertNotNull(entry);
42          assertEquals("foo", entry.getName());
43      }
44  
45      public void testEntryNotFound() throws Exception
46      {
47          final File plugin = new PluginJarBuilder().addResource("foo", "bar").build();
48          final JarEntry entry = JarUtils.getEntry(plugin, "bar");
49          assertNull(entry);
50      }
51  
52      public void testManifest() throws Exception
53      {
54          final File plugin = new PluginJarBuilder().build();
55          final Manifest manifest = JarUtils.getManifest(plugin);
56          assertNotNull(manifest);
57      }
58  
59      public void testManifestCreatedIfNotPresent() throws Exception
60      {
61          final File plugin = new PluginJarBuilder().addResource("foo", "bar").buildWithNoManifest();
62          final Manifest manifest = JarUtils.getManifest(plugin);
63          assertNotNull(manifest);
64      }
65  
66      public void testExtractor() throws Exception
67      {
68          final File plugin = new PluginJarBuilder().addResource("dooby", "whacker").build();
69          final Object expected = new Object();
70          final AtomicBoolean called = new AtomicBoolean(false);
71          final Object result = JarUtils.withJar(plugin, new Extractor<Object>()
72          {
73              public Object get(final JarFile jar)
74              {
75                  final JarEntry entry = jar.getJarEntry("dooby");
76                  assertNotNull(entry);
77                  final JarEntry nonEntry = jar.getJarEntry("blot");
78                  assertNull(nonEntry);
79                  called.set(true);
80                  return expected;
81              }
82          });
83          assertTrue(called.get());
84          assertSame(expected, result);
85      }
86  }