View Javadoc
1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.DefaultModuleDescriptorFactory;
4   import com.atlassian.plugin.ModuleDescriptorFactory;
5   import com.atlassian.plugin.Plugin;
6   import com.atlassian.plugin.PluginArtifact;
7   import com.atlassian.plugin.PluginException;
8   import com.atlassian.plugin.hostcontainer.DefaultHostContainer;
9   import com.atlassian.plugin.impl.UnloadablePlugin;
10  import org.dom4j.Element;
11  import org.junit.Rule;
12  import org.junit.Test;
13  import org.junit.rules.ExpectedException;
14  
15  import java.io.ByteArrayInputStream;
16  
17  import static org.hamcrest.MatcherAssert.assertThat;
18  import static org.hamcrest.Matchers.nullValue;
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  public class TestUnloadableStaticPluginFactory {
26      @Rule
27      public ExpectedException expectedException = ExpectedException.none();
28  
29      @Test
30      public void canCreate() {
31          UnloadableStaticPluginFactory factory = new UnloadableStaticPluginFactory("foo.xml");
32          PluginArtifact artifact = mock(PluginArtifact.class);
33          when(artifact.getResourceAsStream("foo.xml")).thenReturn(new ByteArrayInputStream(
34                  "<atlassian-plugin key=\"foo\" />".getBytes()
35          ));
36          assertEquals("foo", factory.canCreate(artifact));
37      }
38  
39      @Test
40      public void canCreateWithOsgi() {
41          UnloadableStaticPluginFactory factory = new UnloadableStaticPluginFactory("foo.xml");
42          PluginArtifact artifact = mock(PluginArtifact.class);
43          when(artifact.getResourceAsStream("foo.xml")).thenReturn(new ByteArrayInputStream(
44                  "<atlassian-plugin key=\"foo\" plugins-version=\"2\"/>".getBytes()
45          ));
46          assertEquals(null, factory.canCreate(artifact));
47      }
48  
49      @Test
50      public void canCreateWithNoDescriptor() {
51          UnloadableStaticPluginFactory factory = new UnloadableStaticPluginFactory("foo.xml");
52          PluginArtifact artifact = mock(PluginArtifact.class);
53          when(artifact.getResourceAsStream("foo.xml")).thenReturn(null);
54          assertEquals(null, factory.canCreate(artifact));
55      }
56  
57      @Test
58      public void create() {
59          UnloadableStaticPluginFactory factory = new UnloadableStaticPluginFactory("foo.xml");
60          PluginArtifact artifact = mock(PluginArtifact.class);
61          when(artifact.getResourceAsStream("foo.xml")).thenReturn(new ByteArrayInputStream(
62                  "<atlassian-plugin key=\"foo\" />".getBytes()
63          ));
64          when(artifact.toString()).thenReturn("plugin.jar");
65          UnloadablePlugin plugin = (UnloadablePlugin) factory.create(artifact, new DefaultModuleDescriptorFactory(new DefaultHostContainer()));
66          assertNotNull(plugin);
67          assertEquals("foo", plugin.getKey());
68          assertTrue(plugin.getErrorText().contains("plugin.jar"));
69  
70      }
71  
72      @Test
73      public void createWithNoKey() {
74          UnloadableStaticPluginFactory factory = new UnloadableStaticPluginFactory("foo.xml");
75          PluginArtifact artifact = mock(PluginArtifact.class);
76          when(artifact.getResourceAsStream("foo.xml")).thenReturn(new ByteArrayInputStream(
77                  "<atlassian-plugin />".getBytes()
78          ));
79          when(artifact.toString()).thenReturn("plugin.jar");
80          UnloadablePlugin plugin = (UnloadablePlugin) factory.create(artifact, new DefaultModuleDescriptorFactory(new DefaultHostContainer()));
81          assertNotNull(plugin);
82          assertEquals(null, plugin.getKey());
83          assertTrue(plugin.getErrorText().contains("plugin.jar"));
84  
85      }
86  
87      @Test
88      public void createModuleInvalidPluginType() {
89          final UnloadableStaticPluginFactory factory = new UnloadableStaticPluginFactory("foo.xml");
90  
91          final Plugin plugin = mock(Plugin.class);
92          final Element module = mock(Element.class);
93          final ModuleDescriptorFactory moduleDescriptorFactory = mock(ModuleDescriptorFactory.class);
94  
95          assertThat(factory.createModule(plugin, module, moduleDescriptorFactory), nullValue());
96      }
97  
98      @Test
99      public void createModuleNotForUnloadable() {
100         final UnloadableStaticPluginFactory factory = new UnloadableStaticPluginFactory("foo.xml");
101 
102         final UnloadablePlugin plugin = mock(UnloadablePlugin.class);
103         final Element module = mock(Element.class);
104         final ModuleDescriptorFactory moduleDescriptorFactory = mock(ModuleDescriptorFactory.class);
105 
106         expectedException.expect(PluginException.class);
107 
108         factory.createModule(plugin, module, moduleDescriptorFactory);
109     }
110 }