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.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertNull;
16  import static org.junit.Assert.assertTrue;
17  
18  public final class TestJarPluginArtifact
19  {
20      @Test
21      public void testGetResourceAsStream() throws IOException
22      {
23          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
24                  .addResource("foo", "bar")
25                  .build());
26  
27          assertNotNull(artifact.getResourceAsStream("foo"));
28          assertNull(artifact.getResourceAsStream("bar"));
29      }
30  
31      @Test
32      public void testContainsJavaExecutableCodeWithNoJava() throws Exception
33      {
34          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
35                  .addResource("foo", "bar")
36                  .build());
37  
38          assertFalse(artifact.containsJavaExecutableCode());
39      }
40  
41      @Test
42      public void testContainsJavaExecutableCodeWithNoJavaAndNoManifest() throws Exception
43      {
44          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
45                  .addResource("foo", "bar")
46                  .buildWithNoManifest());
47  
48          assertFalse(artifact.containsJavaExecutableCode());
49      }
50  
51      @Test
52      public void testContainsJavaExecutableCodeAsJavaClass() throws Exception
53      {
54          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
55                  .addFormattedJava("foo.Bar",
56                          "package foo;",
57                          "public final class Bar {}")
58                  .build());
59  
60          assertTrue(artifact.containsJavaExecutableCode());
61      }
62  
63      @Test
64      public void testContainsJavaExecutableCodeAsJavaLibrary() throws Exception
65      {
66          File innerJar = new PluginJarBuilder().build();
67  
68          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
69                  .addFile("innerJar.jar", innerJar)
70                  .build());
71  
72          assertTrue(artifact.containsJavaExecutableCode());
73      }
74  
75      @Test
76      public void testContainsJavaExecutableCodeAsSpringContextXmlFiles() throws Exception
77      {
78          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
79                  .addFormattedResource("META-INF/spring/file.xml", "bla")
80                  .build());
81  
82          assertTrue(artifact.containsJavaExecutableCode());
83      }
84  
85      @Test
86      public void testContainsJavaExecutableCodeAsSpringContextManifestEntry() throws Exception
87      {
88          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
89                  .manifest(ImmutableMap.of("Spring-Context", "bla"))
90                  .build());
91  
92          assertTrue(artifact.containsJavaExecutableCode());
93      }
94  
95      @Test
96      public void testContainsJavaExecutableCodeAsBundleActivator() throws Exception
97      {
98          final JarPluginArtifact artifact = new JarPluginArtifact(new PluginJarBuilder()
99                  .manifest(ImmutableMap.of("Bundle-Activator", "bla"))
100                 .build());
101 
102         assertTrue(artifact.containsJavaExecutableCode());
103     }
104 
105     @Test
106     public void allowsReferenceDefaultsToFalse() throws Exception
107     {
108         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"));
109         assertFalse(jarPluginArtifact.allowsReference());
110     }
111 
112     @Test
113     public void referenceModeForbidReferenceDoesNotAllowReference() throws Exception
114     {
115         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"), FORBID_REFERENCE);
116         assertFalse(jarPluginArtifact.allowsReference());
117     }
118 
119     @Test
120     public void referenceModePermitReferenceAllowsReference() throws Exception
121     {
122         final JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(new File("some.jar"), PERMIT_REFERENCE);
123         assertTrue(jarPluginArtifact.allowsReference());
124     }
125 }