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