View Javadoc
1   package com.atlassian.plugin.osgi.factory.transform.stage;
2   
3   import com.atlassian.plugin.JarPluginArtifact;
4   import com.atlassian.plugin.PluginAccessor;
5   import com.atlassian.plugin.PluginParseException;
6   import com.atlassian.plugin.osgi.container.OsgiContainerManager;
7   import com.atlassian.plugin.osgi.external.SingleModuleDescriptorFactory;
8   import com.atlassian.plugin.osgi.factory.transform.TransformContext;
9   import com.atlassian.plugin.osgi.factory.transform.model.SystemExports;
10  import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
11  import com.atlassian.plugin.test.PluginJarBuilder;
12  import org.dom4j.DocumentException;
13  import org.dom4j.DocumentHelper;
14  import org.dom4j.Element;
15  import org.junit.Test;
16  import org.osgi.framework.ServiceReference;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.Collections;
21  
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  public class TestModuleTypeSpringStage {
27  
28      @Test
29      public void testTransform() throws IOException, DocumentException {
30          Element pluginRoot = DocumentHelper.createDocument().addElement("atlassian-plugin");
31          Element moduleType = pluginRoot.addElement("module-type");
32          moduleType.addAttribute("key", "foo");
33          moduleType.addAttribute("class", "my.FooDescriptor");
34  
35          SpringTransformerTestHelper.transform(new ModuleTypeSpringStage(), pluginRoot,
36                  "beans:bean[@id='springHostContainer' and @class='" + ModuleTypeSpringStage.SPRING_HOST_CONTAINER + "']",
37                  "beans:bean[@id='moduleType-foo' and @class='" + SingleModuleDescriptorFactory.class.getName() + "']",
38                  "osgi:service[@id='moduleType-foo_osgiService' and @auto-export='interfaces']");
39      }
40  
41      @Test
42      public void testTransformForOneApp() throws IOException, DocumentException {
43          Element pluginRoot = DocumentHelper.createDocument().addElement("atlassian-plugin");
44          Element moduleType = pluginRoot.addElement("module-type");
45          moduleType.addAttribute("key", "foo");
46          moduleType.addAttribute("class", "my.FooDescriptor");
47          moduleType.addAttribute("application", "bar");
48          SpringTransformerTestHelper.transform(new ModuleTypeSpringStage(), pluginRoot,
49                  "not(beans:bean[@id='moduleType-foo' and @class='" + SingleModuleDescriptorFactory.class.getName() + "'])");
50  
51          pluginRoot = DocumentHelper.createDocument().addElement("atlassian-plugin");
52          moduleType = pluginRoot.addElement("module-type");
53          moduleType.addAttribute("key", "foo");
54          moduleType.addAttribute("class", "my.FooDescriptor");
55          moduleType.addAttribute("application", "foo");
56          SpringTransformerTestHelper.transform(new ModuleTypeSpringStage(), pluginRoot,
57                  "beans:bean[@id='moduleType-foo' and @class='" + SingleModuleDescriptorFactory.class.getName() + "']");
58      }
59  
60      @Test(expected = PluginParseException.class)
61      public void testTransformOfBadElement() throws IOException, DocumentException {
62          Element pluginRoot = DocumentHelper.createDocument().addElement("atlassian-plugin");
63          Element moduleType = pluginRoot.addElement("module-type");
64          moduleType.addAttribute("key", "foo");
65  
66          SpringTransformerTestHelper.transform(new ModuleTypeSpringStage(), pluginRoot,
67                  "beans:bean[@id='moduleType-foo' and @class='" + SingleModuleDescriptorFactory.class.getName() + "']",
68                  "osgi:service[@id='moduleType-foo_osgiService' and @auto-export='interfaces']");
69      }
70  
71      @Test(expected = PluginParseException.class)
72      public void testTransformOfBadElementKey() throws IOException, DocumentException {
73          Element pluginRoot = DocumentHelper.createDocument().addElement("atlassian-plugin");
74          pluginRoot.addElement("module-type");
75  
76          SpringTransformerTestHelper.transform(new ModuleTypeSpringStage(), pluginRoot,
77                  "beans:bean[@id='moduleType-foo' and @class='" + SingleModuleDescriptorFactory.class.getName() + "']",
78                  "osgi:service[@id='moduleType-foo_osgiService' and @auto-export='interfaces']");
79      }
80  
81      @Test
82      public void testBeanTracking() throws Exception {
83          OsgiContainerManager osgiContainerManager = mock(OsgiContainerManager.class);
84          when(osgiContainerManager.getRegisteredServices()).thenReturn(new ServiceReference[0]);
85  
86          final File plugin = new PluginJarBuilder("plugin")
87                  .addFormattedResource("atlassian-plugin.xml",
88                          "<atlassian-plugin name='Test Bundle instruction plugin 2' key='test.plugin'>",
89                          "    <plugin-info>",
90                          "        <version>1.0</version>",
91                          "    </plugin-info>",
92                          "    <module-type key='mod1' class='my.FooDescriptor' />",
93                          "</atlassian-plugin>")
94                  .build();
95  
96          ModuleTypeSpringStage stage = new ModuleTypeSpringStage();
97          final TransformContext context = new TransformContext(Collections.<HostComponentRegistration>emptyList(), SystemExports.NONE, new JarPluginArtifact(plugin),
98                  null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
99          stage.execute(context);
100 
101         // springHostContainer should always exist if a module type is declared.
102         assertTrue(context.beanExists("springHostContainer"));
103         // these two names are generated from the base key, one for the module type itself and the other for service export.
104         assertTrue(context.beanExists("moduleType-mod1"));
105         assertTrue(context.beanExists("moduleType-mod1_osgiService"));
106     }
107 }