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