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.osgi.container.OsgiContainerManager;
6   import com.atlassian.plugin.osgi.factory.transform.TransformContext;
7   import com.atlassian.plugin.osgi.factory.transform.model.SystemExports;
8   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
9   import com.atlassian.plugin.test.PluginJarBuilder;
10  import junit.framework.TestCase;
11  import org.dom4j.Element;
12  import org.dom4j.DocumentHelper;
13  import org.dom4j.DocumentException;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.util.Collections;
18  
19  import com.atlassian.plugin.osgi.external.SingleModuleDescriptorFactory;
20  import com.atlassian.plugin.PluginParseException;
21  import org.osgi.framework.ServiceReference;
22  
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  public class TestModuleTypeSpringStage extends TestCase
27  {
28      public void testTransform() throws IOException, DocumentException
29      {
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      public void testTransformForOneApp() throws IOException, DocumentException
42      {
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      public void testTransformOfBadElement() throws IOException, DocumentException
61      {
62          Element pluginRoot = DocumentHelper.createDocument().addElement("atlassian-plugin");
63          Element moduleType = pluginRoot.addElement("module-type");
64          moduleType.addAttribute("key", "foo");
65  
66          try
67          {
68              SpringTransformerTestHelper.transform(new ModuleTypeSpringStage(), pluginRoot,
69                      "beans:bean[@id='moduleType-foo' and @class='"+ SingleModuleDescriptorFactory.class.getName()+"']",
70                      "osgi:service[@id='moduleType-foo_osgiService' and @auto-export='interfaces']");
71              fail();
72          }
73          catch (PluginParseException ex)
74          {
75              // pass
76          }
77      }
78  
79      public void testTransformOfBadElementKey() throws IOException, DocumentException
80      {
81          Element pluginRoot = DocumentHelper.createDocument().addElement("atlassian-plugin");
82          Element moduleType = pluginRoot.addElement("module-type");
83  
84          try
85          {
86              SpringTransformerTestHelper.transform(new ModuleTypeSpringStage(), pluginRoot,
87                      "beans:bean[@id='moduleType-foo' and @class='"+ SingleModuleDescriptorFactory.class.getName()+"']",
88                      "osgi:service[@id='moduleType-foo_osgiService' and @auto-export='interfaces']");
89              fail();
90          }
91          catch (PluginParseException ex)
92          {
93              // pass
94          }
95      }
96  
97      public void testBeanTracking() throws Exception
98      {
99          OsgiContainerManager osgiContainerManager = mock(OsgiContainerManager.class);
100         when(osgiContainerManager.getRegisteredServices()).thenReturn(new ServiceReference[0]);
101 
102         final File plugin = new PluginJarBuilder("plugin")
103                 .addFormattedResource("atlassian-plugin.xml",
104                         "<atlassian-plugin name='Test Bundle instruction plugin 2' key='test.plugin'>",
105                         "    <plugin-info>",
106                         "        <version>1.0</version>",
107                         "    </plugin-info>",
108                         "    <module-type key='mod1' class='my.FooDescriptor' />",
109                         "</atlassian-plugin>")
110                 .build();
111 
112         ModuleTypeSpringStage stage = new ModuleTypeSpringStage();
113         final TransformContext context = new TransformContext(Collections.<HostComponentRegistration> emptyList(), SystemExports.NONE, new JarPluginArtifact(plugin),
114             null, PluginAccessor.Descriptor.FILENAME, osgiContainerManager);
115         stage.execute(context);
116 
117         // springHostContainer should always exist if a module type is declared.
118         assertTrue(context.beanExists("springHostContainer"));
119         // these two names are generated from the base key, one for the module type itself and the other for service export.
120         assertTrue(context.beanExists("moduleType-mod1"));
121         assertTrue(context.beanExists("moduleType-mod1_osgiService"));
122     }
123 }