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