View Javadoc

1   package com.atlassian.plugin;
2   
3   import com.atlassian.plugin.test.PluginJarBuilder;
4   import com.google.common.collect.ImmutableMap;
5   import org.junit.Test;
6   
7   import java.io.File;
8   import java.io.IOException;
9   
10  import static com.atlassian.plugin.PluginArtifact.AllowsReference.ReferenceMode.FORBID_REFERENCE;
11  import static com.atlassian.plugin.PluginArtifact.AllowsReference.ReferenceMode.PERMIT_REFERENCE;
12  
13  import static org.hamcrest.Matchers.empty;
14  import static org.hamcrest.Matchers.hasItem;
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertNotNull;
17  import static org.junit.Assert.assertNull;
18  import static org.junit.Assert.assertThat;
19  import static org.junit.Assert.assertTrue;
20  
21  public final class TestJarPluginArtifact
22  {
23      @Test
24      public void testGetResourceAsStream() throws IOException
25      {
26          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
27                  .addResource("foo", "bar")
28                  .build());
29  
30          assertNotNull(artifact.getResourceAsStream("foo"));
31          assertNull(artifact.getResourceAsStream("bar"));
32      }
33  
34      @Test
35      public void testContainsJavaExecutableCodeWithNoJava() throws Exception
36      {
37          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
38                  .addResource("foo", "bar")
39                  .build());
40  
41          assertFalse(artifact.containsJavaExecutableCode());
42      }
43  
44      @Test
45      public void testContainsJavaExecutableCodeWithNoJavaAndNoManifest() throws Exception
46      {
47          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
48                  .addResource("foo", "bar")
49                  .buildWithNoManifest());
50  
51          assertFalse(artifact.containsJavaExecutableCode());
52      }
53  
54      @Test
55      public void testContainsJavaExecutableCodeAsJavaClass() throws Exception
56      {
57          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
58                  .addFormattedJava("foo.Bar",
59                          "package foo;",
60                          "public final class Bar {}")
61                  .build());
62  
63          assertTrue(artifact.containsJavaExecutableCode());
64      }
65  
66      @Test
67      public void testContainsJavaExecutableCodeAsJavaLibrary() throws Exception
68      {
69          File innerJar = new PluginJarBuilder().build();
70  
71          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
72                  .addFile("innerJar.jar", innerJar)
73                  .build());
74  
75          assertTrue(artifact.containsJavaExecutableCode());
76      }
77  
78      @Test
79      public void testContainsJavaExecutableCodeAsSpringContextXmlFiles() throws Exception
80      {
81          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
82                  .addFormattedResource("META-INF/spring/file.xml", "bla")
83                  .build());
84  
85          assertTrue(artifact.containsJavaExecutableCode());
86      }
87  
88      @Test
89      public void testContainsJavaExecutableCodeAsSpringContextManifestEntry() throws Exception
90      {
91          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
92                  .manifest(ImmutableMap.of("Spring-Context", "bla"))
93                  .build());
94  
95          assertTrue(artifact.containsJavaExecutableCode());
96      }
97  
98      @Test
99      public void testContainsJavaExecutableCodeAsBundleActivator() throws Exception
100     {
101         final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
102                 .manifest(ImmutableMap.of("Bundle-Activator", "bla"))
103                 .build());
104 
105         assertTrue(artifact.containsJavaExecutableCode());
106     }
107 
108     @Test
109     public void allowsReferenceDefaultsToFalse() throws Exception
110     {
111         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"));
112         assertFalse(jarPluginArtifact.allowsReference());
113     }
114 
115     @Test
116     public void referenceModeForbidReferenceDoesNotAllowReference() throws Exception
117     {
118         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"), FORBID_REFERENCE);
119         assertFalse(jarPluginArtifact.allowsReference());
120     }
121 
122     @Test
123     public void referenceModePermitReferenceAllowsReference() throws Exception
124     {
125         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"), PERMIT_REFERENCE);
126         assertTrue(jarPluginArtifact.allowsReference());
127     }
128 
129     @Test
130     public void testExtraModuleDescriptorFiles() throws Exception
131     {
132         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new PluginJarBuilder("plugin")
133                                                            .addResource("META-INF/atlassian/foo.xml","<atlassian-plugin/>")
134                                                            .build());
135         assertThat(jarPluginArtifact.extraModuleDescriptorFiles("META-INF/atlassian"), hasItem("META-INF/atlassian/foo.xml"));
136     }
137 
138     @Test
139     public void testExtraModuleDescriptorFilesIgnoresSubfolders() throws Exception
140     {
141         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new PluginJarBuilder("plugin")
142                 .addResource("META-INF/atlassian/bar/foo.xml","<atlassian-plugin/>")
143                 .build());
144         assertThat(jarPluginArtifact.extraModuleDescriptorFiles("META-INF/atlassian"), empty());
145     }
146 
147     @Test
148     public void testExtraModuleDescriptorFilesIgnoresNonXmlFiles() throws Exception
149     {
150         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new PluginJarBuilder("plugin")
151                 .addResource("META-INF/atlassian/foo.txt","<atlassian-plugin/>")
152                 .build());
153         assertThat(jarPluginArtifact.extraModuleDescriptorFiles("META-INF/atlassian"), empty());
154     }
155 }