View Javadoc

1   package com.atlassian.plugin.osgi.factory;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   
7   import com.atlassian.plugin.JarPluginArtifact;
8   import com.atlassian.plugin.ModuleDescriptorFactory;
9   import com.atlassian.plugin.Plugin;
10  import com.atlassian.plugin.PluginArtifact;
11  import com.atlassian.plugin.event.PluginEventManager;
12  import com.atlassian.plugin.impl.UnloadablePlugin;
13  import com.atlassian.plugin.osgi.container.OsgiContainerManager;
14  import com.atlassian.plugin.test.PluginJarBuilder;
15  
16  import org.junit.Before;
17  import org.junit.Test;
18  import org.junit.runner.RunWith;
19  import org.mockito.Mock;
20  import org.mockito.invocation.InvocationOnMock;
21  import org.mockito.runners.MockitoJUnitRunner;
22  import org.mockito.stubbing.Answer;
23  
24  import static org.hamcrest.MatcherAssert.assertThat;
25  import static org.hamcrest.Matchers.instanceOf;
26  import static org.hamcrest.Matchers.is;
27  import static org.hamcrest.Matchers.notNullValue;
28  import static org.hamcrest.Matchers.nullValue;
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.verifyZeroInteractions;
31  import static org.mockito.Mockito.when;
32  
33  @RunWith (MockitoJUnitRunner.class)
34  public class TestOsgiBundleFactory
35  {
36      @Mock private OsgiContainerManager osgiContainerManager;
37      @Mock private PluginEventManager pluginEventManager;
38      @Mock private ModuleDescriptorFactory moduleDescriptorFactory;
39  
40      OsgiBundleFactory osgiBundleFactory;
41  
42      @Before
43      public void setUp() throws Exception
44      {
45          osgiBundleFactory = new OsgiBundleFactory(osgiContainerManager, pluginEventManager);
46      }
47  
48      @Test
49      public void canCreateReturnsPluginKeyForJarFileContainingBundle() throws Exception
50      {
51          final String pluginKey = osgiBundleFactory.canCreate(TestOsgiBundlePlugin.getBundleJarPluginArtifact());
52          assertThat(pluginKey, is(TestOsgiBundlePlugin.PLUGIN_KEY));
53      }
54  
55      @Test
56      public void canCreateReturnsNullForJarFileContainingBundleAndPlugin() throws Exception
57      {
58          final String pluginKey = osgiBundleFactory.canCreate(getBundlePluginJarPluginArtifact());
59          assertThat(pluginKey, nullValue());
60      }
61  
62      @Test
63      public void canCreateReturnsNullForPluginJar() throws Exception
64      {
65          final String pluginKey = osgiBundleFactory.canCreate(getPluginJarPluginArtifact());
66          assertThat(pluginKey, nullValue());
67      }
68  
69      @Test
70      public void canCreateReturnsNullForNonFileArtifact() throws Exception
71      {
72          final String pluginKey = osgiBundleFactory.canCreate(getNonFilePluginArtifact());
73          assertThat(pluginKey, nullValue());
74      }
75  
76      @Test
77      public void createReturnsConfiguredPluginWhenBundleInstallSucceeds() throws Exception
78      {
79          final PluginArtifact pluginArtifact = TestOsgiBundlePlugin.getBundleJarPluginArtifact();
80          final Plugin plugin = osgiBundleFactory.create(pluginArtifact, moduleDescriptorFactory);
81          assertThat(plugin, notNullValue());
82          assertThat(plugin, instanceOf(OsgiBundlePlugin.class));
83          TestOsgiBundlePlugin.assertThatPluginIsConfigured(plugin);
84  
85          // create does _not_ install the bundle
86          verifyZeroInteractions(osgiContainerManager);
87      }
88  
89      @Test
90      public void createReturnsUnloadablePluginForJarFileContainingBundleAndPlugin() throws Exception
91      {
92          final Plugin plugin = osgiBundleFactory.create(getBundlePluginJarPluginArtifact(), moduleDescriptorFactory);
93          assertThat(plugin, notNullValue());
94          assertThat(plugin, instanceOf(UnloadablePlugin.class));
95      }
96  
97      @Test
98      public void createReturnsUnloadablePluginForPluginJar() throws Exception
99      {
100         final Plugin plugin = osgiBundleFactory.create(getPluginJarPluginArtifact(), moduleDescriptorFactory);
101         assertThat(plugin, notNullValue());
102         assertThat(plugin, instanceOf(UnloadablePlugin.class));
103     }
104 
105     @Test
106     public void createReturnsUnloadablePluginForNonFileArtifact() throws Exception
107     {
108         final Plugin plugin = osgiBundleFactory.create(getNonFilePluginArtifact(), moduleDescriptorFactory);
109         assertThat(plugin, notNullValue());
110         assertThat(plugin, instanceOf(UnloadablePlugin.class));
111     }
112 
113     private static PluginArtifact getBundlePluginJarPluginArtifact() throws IOException
114     {
115         return new JarPluginArtifact(new PluginJarBuilder("somebundleplugin")
116                 .manifest(TestOsgiBundlePlugin.getBundleManifest())
117                 .addPluginInformation(TestOsgiBundlePlugin.PLUGIN_KEY, TestOsgiBundlePlugin.NAME, TestOsgiBundlePlugin.VERSION, 2)
118                 .build());
119     }
120 
121     private static PluginArtifact getPluginJarPluginArtifact() throws IOException
122     {
123         return new JarPluginArtifact(new PluginJarBuilder("someplugin")
124                 .addPluginInformation(TestOsgiBundlePlugin.PLUGIN_KEY, TestOsgiBundlePlugin.NAME, TestOsgiBundlePlugin.VERSION, 2)
125                 .build());
126     }
127 
128     private static PluginArtifact getNonFilePluginArtifact() throws IOException
129     {
130         final File tempFile = File.createTempFile("foo", "bar");
131         final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
132         when(pluginArtifact.getName()).thenReturn(tempFile.getPath());
133         when(pluginArtifact.getInputStream()).thenAnswer(new Answer<Object>()
134         {
135             @Override
136             public Object answer(final InvocationOnMock invocation) throws Throwable
137             {
138                 return new FileInputStream(tempFile);
139             }
140         });
141         when(pluginArtifact.toFile()).thenReturn(tempFile);
142         return pluginArtifact;
143     }
144 }