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