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.ImmutableMap;
9   import com.google.common.collect.ImmutableSet;
10  import junit.framework.TestCase;
11  import org.aopalliance.aop.Advice;
12  import org.springframework.aop.framework.ProxyFactory;
13  import org.springframework.beans.factory.BeanFactory;
14  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
15  import org.springframework.beans.factory.support.StaticListableBeanFactory;
16  
17  import java.io.Serializable;
18  import java.util.Arrays;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  public class TestSpringHostComponentProviderFactoryBeanWithAnnotations extends TestCase {
25      public void testProvide() throws Exception {
26          List<HostComponentRegistration> list = calculateHostComponentRegistrations(ImmutableMap.<String, Object>of("bean", new FooableBean(),
27                  "string", "hello"));
28          assertNotNull(list);
29          assertEquals(1, list.size());
30          assertEquals("bean", list.get(0).getProperties().get("bean-name"));
31          List<Class<?>> ifs = Arrays.asList(list.get(0).getMainInterfaceClasses());
32  
33          // Test locally declared interface
34          assertTrue(ifs.contains(Fooable.class));
35  
36          // Test super interface of locally declared interface
37          assertTrue(ifs.contains(Barable.class));
38  
39          // Test interface of super class
40          assertTrue(ifs.contains(Map.class));
41      }
42  
43      public void testProvideWithValuesParam() throws Exception {
44          List<HostComponentRegistration> list = calculateHostComponentRegistrations(ImmutableMap.<String, Object>of("bean", new AnotherAnnotatedBean()));
45          List<Class<?>> ifs = Arrays.asList(list.get(0).getMainInterfaceClasses());
46  
47          // Only explicitly specified interface
48          assertEquals(Arrays.asList(Fooable.class), ifs);
49      }
50  
51      public void testProvideWithInterfacesParam() throws Exception {
52          List<HostComponentRegistration> list = calculateHostComponentRegistrations(ImmutableMap.<String, Object>of("bean", new SomeAnnotatedBean()));
53          List<Class<?>> ifs = Arrays.asList(list.get(0).getMainInterfaceClasses());
54  
55          // Only explicitly specified interfaces
56          assertEquals(Arrays.asList(Fooable.class, Serializable.class), ifs);
57      }
58  
59      public void testProvideWithInterfacesTwoDifferentWaysProducesCombinedEffect() throws Exception {
60          List<HostComponentRegistration> list = calculateHostComponentRegistrations(ImmutableMap.<String, Object>of("bean", new WeirdlyAnnotatedBean()));
61          List<Class<?>> ifs = Arrays.asList(list.get(0).getMainInterfaceClasses());
62  
63          // The explicitly specified interfaces overrides 'value'.
64          assertEquals(Arrays.asList(Barable.class, Fooable.class, Serializable.class, Map.class), ifs);
65      }
66  
67      public void testProvideWithCCLStrategy() throws Exception {
68          List<HostComponentRegistration> list = calculateHostComponentRegistrations(ImmutableMap.<String, Object>of("bean", new FooablePluginService()));
69          assertNotNull(list);
70          assertEquals(1, list.size());
71          assertEquals("bean", list.get(0).getProperties().get(PropertyBuilder.BEAN_NAME));
72          assertEquals(ContextClassLoaderStrategy.USE_PLUGIN.name(), list.get(0).getProperties().get(PropertyBuilder.CONTEXT_CLASS_LOADER_STRATEGY));
73      }
74  
75      public void testProvideWithProxy() throws Exception {
76          ProxyFactory pFactory = new ProxyFactory(new FooableBean());
77          pFactory.addInterface(Fooable.class);
78          pFactory.addAdvice(new Advice() {
79          });
80  
81          List<HostComponentRegistration> list = calculateHostComponentRegistrations(ImmutableMap.<String, Object>of("bean", pFactory.getProxy(),
82                  "string", "hello"));
83  
84          assertNotNull(list);
85          assertEquals(1, list.size());
86          assertEquals("bean", list.get(0).getProperties().get("bean-name"));
87          assertEquals(Fooable.class.getName(), list.get(0).getMainInterfaces()[0]);
88      }
89  
90      public void testProvideOnlySingletonBeans() throws Exception {
91          List<HostComponentRegistration> list = calculateHostComponentRegistrations(ImmutableMap.<String, Object>of("bean", new FooableBean(),
92                          "bean2", new FooablePluginService()),
93                  ImmutableSet.<String>of("bean"));
94          assertNotNull(list);
95          assertEquals(1, list.size());
96          assertEquals("bean2", list.get(0).getProperties().get("bean-name"));
97      }
98  
99      private List<HostComponentRegistration> calculateHostComponentRegistrations(Map<String, Object> beanMap) throws Exception {
100         return calculateHostComponentRegistrations(beanMap, Collections.<String>emptySet());
101     }
102 
103     private List<HostComponentRegistration> calculateHostComponentRegistrations(Map<String, Object> beanMap, final Set<String> namesOfScopedBeans) throws Exception {
104         StaticListableBeanFactory factory = new StaticListableBeanFactory() {
105             @Override
106             public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
107                 return !namesOfScopedBeans.contains(name);
108             }
109         };
110 
111         for (Map.Entry<String, Object> entry : beanMap.entrySet()) {
112             factory.addBean(entry.getKey(), entry.getValue());
113         }
114 
115         HostComponentProvider provider = getHostComponentProvider(factory);
116 
117         DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
118         provider.provide(registrar);
119 
120         return registrar.getRegistry();
121     }
122 
123     private HostComponentProvider getHostComponentProvider(BeanFactory factory) throws Exception {
124         SpringHostComponentProviderFactoryBean providerFactoryBean = new SpringHostComponentProviderFactoryBean();
125         SpringHostComponentProviderConfig config = new SpringHostComponentProviderConfig();
126         config.setUseAnnotation(true);
127         providerFactoryBean.setSpringHostComponentProviderConfig(config);
128         providerFactoryBean.setBeanFactory(factory);
129         providerFactoryBean.afterPropertiesSet();
130         return (HostComponentProvider) providerFactoryBean.getObject();
131     }
132 }