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() throws Exception {
129         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"));
130         assertThat(jarPluginArtifact.getReferenceMode(), is(FORBID_REFERENCE));
131         assertFalse(jarPluginArtifact.allowsReference());
132     }
133 
134     @Test
135     public void referenceModeForbidReferenceDoesNotAllowReference() throws Exception {
136         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"), FORBID_REFERENCE);
137         assertThat(jarPluginArtifact.getReferenceMode(), is(FORBID_REFERENCE));
138         assertFalse(jarPluginArtifact.allowsReference());
139     }
140 
141     @Test
142     public void referenceModePermitReferenceAllowsReference() throws Exception {
143         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"), PERMIT_REFERENCE);
144         assertThat(jarPluginArtifact.getReferenceMode(), is(PERMIT_REFERENCE));
145         assertTrue(jarPluginArtifact.allowsReference());
146     }
147 
148 
149     @Test
150     public void legacyReferenceModeForbidReferenceDoesNotAllowReference() throws Exception {
151         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"),
152                 PluginArtifact.AllowsReference.ReferenceMode.FORBID_REFERENCE);
153         assertFalse(jarPluginArtifact.allowsReference());
154     }
155 
156     @Test
157     public void legacyReferenceModePermitReferenceAllowsReference() throws Exception {
158         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"),
159                 PluginArtifact.AllowsReference.ReferenceMode.PERMIT_REFERENCE);
160         assertTrue(jarPluginArtifact.allowsReference());
161     }
162 
163     @Test
164     public void testExtraModuleDescriptorFiles() throws Exception {
165         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new PluginJarBuilder("plugin")
166                 .addResource("META-INF/atlassian/foo.xml", "<atlassian-plugin/>")
167                 .build());
168         assertThat(jarPluginArtifact.extraModuleDescriptorFiles("META-INF/atlassian"), hasItem("META-INF/atlassian/foo.xml"));
169     }
170 
171     @Test
172     public void testExtraModuleDescriptorFilesIgnoresSubfolders() throws Exception {
173         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new PluginJarBuilder("plugin")
174                 .addResource("META-INF/atlassian/bar/foo.xml", "<atlassian-plugin/>")
175                 .build());
176         assertThat(jarPluginArtifact.extraModuleDescriptorFiles("META-INF/atlassian"), empty());
177     }
178 
179     @Test
180     public void testExtraModuleDescriptorFilesIgnoresNonXmlFiles() throws Exception {
181         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new PluginJarBuilder("plugin")
182                 .addResource("META-INF/atlassian/foo.txt", "<atlassian-plugin/>")
183                 .build());
184         assertThat(jarPluginArtifact.extraModuleDescriptorFiles("META-INF/atlassian"), empty());
185     }
186 }