View Javadoc
1   package com.atlassian.plugin.spring;
2   
3   import com.atlassian.plugin.osgi.hostcomponents.ContextClassLoaderStrategy;
4   import com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider;
5   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
6   import com.atlassian.plugin.osgi.hostcomponents.PropertyBuilder;
7   import com.atlassian.plugin.osgi.hostcomponents.impl.DefaultComponentRegistrar;
8   import com.google.common.collect.Iterables;
9   import org.junit.Test;
10  import org.springframework.beans.factory.BeanFactory;
11  import org.springframework.beans.factory.BeanFactoryAware;
12  import org.springframework.beans.factory.xml.XmlBeanFactory;
13  import org.springframework.core.io.ClassPathResource;
14  
15  import java.io.Serializable;
16  import java.util.Arrays;
17  import java.util.HashSet;
18  import java.util.List;
19  import java.util.Map;
20  
21  import static com.atlassian.plugin.spring.PluginBeanDefinitionRegistry.HOST_COMPONENT_PROVIDER;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  public class TestSpringHostComponentProviderFactoryBeanWithXmlConfiguration {
27      private static final HashSet<Class> FOOABLE_BEAN_INTERFACES = new HashSet<Class>(Arrays.asList(Serializable.class, Map.class, Cloneable.class, Fooable.class, Barable.class));
28      private static final HashSet<Class> FOO_BARABLE_INTERFACES = new HashSet<Class>(Arrays.asList(Fooable.class, Barable.class));
29  
30      @Test
31      public void testProvide() {
32          List<HostComponentRegistration> list = registerHostComponentsFromXml("com/atlassian/plugin/spring/pluginns/plugins-spring-test.xml");
33          assertNotNull(list);
34          assertEquals(1, list.size());
35          assertEquals("foo", list.get(0).getProperties().get("bean-name"));
36          assertEquals(5, list.get(0).getMainInterfaces().length);
37          assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
38          assertEquals(ContextClassLoaderStrategy.USE_PLUGIN.name(), list.get(0).getProperties().get(PropertyBuilder.CONTEXT_CLASS_LOADER_STRATEGY));
39      }
40  
41      @Test
42      public void testProvideTrackBundle() throws Exception {
43          List<HostComponentRegistration> registrations = registerHostComponentsFromXml("com/atlassian/plugin/spring/pluginns/plugins-spring-test-track-bundle.xml");
44          final HostComponentRegistration registration = Iterables.getOnlyElement(registrations);
45  
46          assertEquals(registration.getProperties().get(PropertyBuilder.TRACK_BUNDLE), "true");
47      }
48  
49      @Test
50      public void testProvideWithDeprecations() {
51          List<HostComponentRegistration> list = registerHostComponentsFromXml("com/atlassian/plugin/spring/pluginns/plugins-spring-deprecations.xml");
52          assertNotNull(list);
53          assertEquals(1, list.size());
54          assertEquals("foo", list.get(0).getProperties().get("bean-name"));
55          assertEquals(5, list.get(0).getMainInterfaces().length);
56          assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
57          assertEquals(ContextClassLoaderStrategy.USE_PLUGIN.name(), list.get(0).getProperties().get(PropertyBuilder.CONTEXT_CLASS_LOADER_STRATEGY));
58      }
59  
60      private HostComponentProvider getHostProvider(BeanFactory factory) {
61          final HostComponentProvider provider = (HostComponentProvider) factory.getBean(HOST_COMPONENT_PROVIDER);
62          assertNotNull(provider);
63          return provider;
64      }
65  
66      @Test
67      public void testProvideWithPrototype() {
68          List<HostComponentRegistration> list = registerHostComponentsFromXml("com/atlassian/plugin/spring/pluginns/plugins-spring-test-prototype.xml");
69          assertNotNull(list);
70          assertEquals(1, list.size());
71          assertEquals("foo", list.get(0).getProperties().get("bean-name"));
72          assertEquals(5, list.get(0).getMainInterfaces().length);
73          assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
74      }
75  
76      @Test
77      public void testProvideWithCustomInterface() {
78          List<HostComponentRegistration> list = registerHostComponentsFromXml("com/atlassian/plugin/spring/pluginns/plugins-spring-test-interface.xml");
79          assertNotNull(list);
80          assertEquals(2, list.size());
81  
82          if ("foo".equals(list.get(0).getProperties().get("bean-name"))) {
83              assertFoo(list.get(0));
84              assertFooMultipleInterfaces(list.get(1));
85          } else {
86              assertFoo(list.get(1));
87              assertFooMultipleInterfaces(list.get(0));
88          }
89      }
90  
91      private void assertFoo(HostComponentRegistration registration) {
92          assertEquals("foo", registration.getProperties().get("bean-name"));
93          assertEquals(1, registration.getMainInterfaces().length);
94          assertEquals(BeanFactoryAware.class.getName(), registration.getMainInterfaces()[0]);
95      }
96  
97      private void assertFooMultipleInterfaces(HostComponentRegistration registration) {
98          assertEquals("fooMultipleInterface", registration.getProperties().get("bean-name"));
99          assertEquals(2, registration.getMainInterfaces().length);
100         assertEquals(BeanFactoryAware.class.getName(), registration.getMainInterfaces()[0]);
101         assertEquals(Barable.class.getName(), registration.getMainInterfaces()[1]);
102     }
103 
104     @Test
105     public void testProvideWithInterfaceOnSuperClass() {
106         List<HostComponentRegistration> list = registerHostComponentsFromXml("com/atlassian/plugin/spring/pluginns/plugins-spring-test-super-interface.xml");
107         assertNotNull(list);
108         assertEquals(1, list.size());
109         assertEquals("foobarable", list.get(0).getProperties().get("bean-name"));
110         assertEquals(2, list.get(0).getMainInterfaces().length);
111         assertEquals(FOO_BARABLE_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
112     }
113 
114     @Test
115     public void testProvideWithNestedContexts() {
116         XmlBeanFactory parentFactory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test.xml"));
117         XmlBeanFactory childFactory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-child.xml"), parentFactory);
118 
119         HostComponentProvider provider = getHostProvider(childFactory);
120 
121         assertTrue(parentFactory.containsBeanDefinition(HOST_COMPONENT_PROVIDER));
122         assertTrue(childFactory.containsBeanDefinition(HOST_COMPONENT_PROVIDER));
123 
124 
125         DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
126         provider.provide(registrar);
127 
128         List<HostComponentRegistration> list = registrar.getRegistry();
129         assertNotNull(list);
130         assertEquals(2, list.size());
131     }
132 
133     private List<HostComponentRegistration> registerHostComponentsFromXml(String path) {
134         XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(path));
135 
136         HostComponentProvider provider = getHostProvider(factory);
137 
138         DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
139         provider.provide(registrar);
140 
141         return registrar.getRegistry();
142     }
143 }