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 private static final HashSet<Class> FOOABLE_BEAN_INTERFACES = new HashSet<Class>(Arrays.asList(Serializable.class, Map.class, Cloneable.class, Fooable.class, Barable.class));
24 private static final HashSet<Class> FOO_BARABLE_INTERFACES = new HashSet<Class>(Arrays.asList(Fooable.class, Barable.class));
25
26 public void testProvide() {
27 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test.xml"));
28
29 HostComponentProvider provider = getHostProvider(factory);
30
31 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
32 provider.provide(registrar);
33
34 List<HostComponentRegistration> list = registrar.getRegistry();
35 assertNotNull(list);
36 assertEquals(1, list.size());
37 assertEquals("foo", list.get(0).getProperties().get("bean-name"));
38 assertEquals(5, list.get(0).getMainInterfaces().length);
39 assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
40 assertEquals(ContextClassLoaderStrategy.USE_PLUGIN.name(), list.get(0).getProperties().get(PropertyBuilder.CONTEXT_CLASS_LOADER_STRATEGY));
41 }
42
43 public void testProvideWithDeprecations() {
44 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-deprecations.xml"));
45
46 HostComponentProvider provider = getHostProvider(factory);
47
48 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
49 provider.provide(registrar);
50
51 List<HostComponentRegistration> list = registrar.getRegistry();
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 public void testProvideWithPrototype() {
67 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-prototype.xml"));
68
69 HostComponentProvider provider = getHostProvider(factory);
70
71
72 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
73 provider.provide(registrar);
74
75 List<HostComponentRegistration> list = registrar.getRegistry();
76 assertNotNull(list);
77 assertEquals(1, list.size());
78 assertEquals("foo", list.get(0).getProperties().get("bean-name"));
79 assertEquals(5, list.get(0).getMainInterfaces().length);
80 assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
81 }
82
83 public void testProvideWithCustomInterface() {
84 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-interface.xml"));
85
86 HostComponentProvider provider = getHostProvider(factory);
87
88 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
89 provider.provide(registrar);
90
91 List<HostComponentRegistration> list = registrar.getRegistry();
92 assertNotNull(list);
93 assertEquals(2, list.size());
94
95 if ("foo".equals(list.get(0).getProperties().get("bean-name"))) {
96 assertFoo(list.get(0));
97 assertFooMultipleInterfaces(list.get(1));
98 } else {
99 assertFoo(list.get(1));
100 assertFooMultipleInterfaces(list.get(0));
101 }
102 }
103
104 private void assertFoo(HostComponentRegistration registration) {
105 assertEquals("foo", registration.getProperties().get("bean-name"));
106 assertEquals(1, registration.getMainInterfaces().length);
107 assertEquals(BeanFactoryAware.class.getName(), registration.getMainInterfaces()[0]);
108 }
109
110 private void assertFooMultipleInterfaces(HostComponentRegistration registration) {
111 assertEquals("fooMultipleInterface", registration.getProperties().get("bean-name"));
112 assertEquals(2, registration.getMainInterfaces().length);
113 assertEquals(BeanFactoryAware.class.getName(), registration.getMainInterfaces()[0]);
114 assertEquals(Barable.class.getName(), registration.getMainInterfaces()[1]);
115 }
116
117 public void testProvideWithInterfaceOnSuperClass() {
118 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-super-interface.xml"));
119
120 HostComponentProvider provider = getHostProvider(factory);
121
122 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
123 provider.provide(registrar);
124
125 List<HostComponentRegistration> list = registrar.getRegistry();
126 assertNotNull(list);
127 assertEquals(1, list.size());
128 assertEquals("foobarable", list.get(0).getProperties().get("bean-name"));
129 assertEquals(2, list.get(0).getMainInterfaces().length);
130 assertEquals(FOO_BARABLE_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
131 }
132
133 public void testProvideWithNestedContexts() {
134 XmlBeanFactory parentFactory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test.xml"));
135 XmlBeanFactory childFactory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-child.xml"), parentFactory);
136
137 HostComponentProvider provider = getHostProvider(childFactory);
138
139 assertTrue(parentFactory.containsBeanDefinition(HOST_COMPONENT_PROVIDER));
140 assertTrue(childFactory.containsBeanDefinition(HOST_COMPONENT_PROVIDER));
141
142
143 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
144 provider.provide(registrar);
145
146 List<HostComponentRegistration> list = registrar.getRegistry();
147 assertNotNull(list);
148 assertEquals(2, list.size());
149 }
150 }