/atlassian-plugins-spring/src/test/java/com/atlassian/plugin/spring/TestSpringHostComponentProviderFactoryBeanWithXmlConfiguration.java

https://bitbucket.org/purewind/atlassian-plugins · Java · 150 lines · 115 code · 35 blank · 0 comment · 2 complexity · b1e28c66bfc64cd1d31c4ae5c2df9964 MD5 · raw file

  1. package com.atlassian.plugin.spring;
  2. import com.atlassian.plugin.osgi.hostcomponents.ContextClassLoaderStrategy;
  3. import com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider;
  4. import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
  5. import com.atlassian.plugin.osgi.hostcomponents.PropertyBuilder;
  6. import com.atlassian.plugin.osgi.hostcomponents.impl.DefaultComponentRegistrar;
  7. import junit.framework.TestCase;
  8. import org.springframework.beans.factory.BeanFactory;
  9. import org.springframework.beans.factory.BeanFactoryAware;
  10. import org.springframework.beans.factory.xml.XmlBeanFactory;
  11. import org.springframework.core.io.ClassPathResource;
  12. import java.io.Serializable;
  13. import java.util.Arrays;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Map;
  17. import static com.atlassian.plugin.spring.PluginBeanDefinitionRegistry.HOST_COMPONENT_PROVIDER;
  18. public class TestSpringHostComponentProviderFactoryBeanWithXmlConfiguration extends TestCase {
  19. private static final HashSet<Class> FOOABLE_BEAN_INTERFACES = new HashSet<Class>(Arrays.asList(Serializable.class, Map.class, Cloneable.class, Fooable.class, Barable.class));
  20. private static final HashSet<Class> FOO_BARABLE_INTERFACES = new HashSet<Class>(Arrays.asList(Fooable.class, Barable.class));
  21. public void testProvide() {
  22. XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test.xml"));
  23. HostComponentProvider provider = getHostProvider(factory);
  24. DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
  25. provider.provide(registrar);
  26. List<HostComponentRegistration> list = registrar.getRegistry();
  27. assertNotNull(list);
  28. assertEquals(1, list.size());
  29. assertEquals("foo", list.get(0).getProperties().get("bean-name"));
  30. assertEquals(5, list.get(0).getMainInterfaces().length);
  31. assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
  32. assertEquals(ContextClassLoaderStrategy.USE_PLUGIN.name(), list.get(0).getProperties().get(PropertyBuilder.CONTEXT_CLASS_LOADER_STRATEGY));
  33. }
  34. public void testProvideWithDeprecations() {
  35. XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-deprecations.xml"));
  36. HostComponentProvider provider = getHostProvider(factory);
  37. DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
  38. provider.provide(registrar);
  39. List<HostComponentRegistration> list = registrar.getRegistry();
  40. assertNotNull(list);
  41. assertEquals(1, list.size());
  42. assertEquals("foo", list.get(0).getProperties().get("bean-name"));
  43. assertEquals(5, list.get(0).getMainInterfaces().length);
  44. assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
  45. assertEquals(ContextClassLoaderStrategy.USE_PLUGIN.name(), list.get(0).getProperties().get(PropertyBuilder.CONTEXT_CLASS_LOADER_STRATEGY));
  46. }
  47. private HostComponentProvider getHostProvider(BeanFactory factory) {
  48. final HostComponentProvider provider = (HostComponentProvider) factory.getBean(HOST_COMPONENT_PROVIDER);
  49. assertNotNull(provider);
  50. return provider;
  51. }
  52. public void testProvideWithPrototype() {
  53. XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-prototype.xml"));
  54. HostComponentProvider provider = getHostProvider(factory);
  55. DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
  56. provider.provide(registrar);
  57. List<HostComponentRegistration> list = registrar.getRegistry();
  58. assertNotNull(list);
  59. assertEquals(1, list.size());
  60. assertEquals("foo", list.get(0).getProperties().get("bean-name"));
  61. assertEquals(5, list.get(0).getMainInterfaces().length);
  62. assertEquals(FOOABLE_BEAN_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
  63. }
  64. public void testProvideWithCustomInterface() {
  65. XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-interface.xml"));
  66. HostComponentProvider provider = getHostProvider(factory);
  67. DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
  68. provider.provide(registrar);
  69. List<HostComponentRegistration> list = registrar.getRegistry();
  70. assertNotNull(list);
  71. assertEquals(2, list.size());
  72. if ("foo".equals(list.get(0).getProperties().get("bean-name"))) {
  73. assertFoo(list.get(0));
  74. assertFooMultipleInterfaces(list.get(1));
  75. } else {
  76. assertFoo(list.get(1));
  77. assertFooMultipleInterfaces(list.get(0));
  78. }
  79. }
  80. private void assertFoo(HostComponentRegistration registration) {
  81. assertEquals("foo", registration.getProperties().get("bean-name"));
  82. assertEquals(1, registration.getMainInterfaces().length);
  83. assertEquals(BeanFactoryAware.class.getName(), registration.getMainInterfaces()[0]);
  84. }
  85. private void assertFooMultipleInterfaces(HostComponentRegistration registration) {
  86. assertEquals("fooMultipleInterface", registration.getProperties().get("bean-name"));
  87. assertEquals(2, registration.getMainInterfaces().length);
  88. assertEquals(BeanFactoryAware.class.getName(), registration.getMainInterfaces()[0]);
  89. assertEquals(Barable.class.getName(), registration.getMainInterfaces()[1]);
  90. }
  91. public void testProvideWithInterfaceOnSuperClass() {
  92. XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-super-interface.xml"));
  93. HostComponentProvider provider = getHostProvider(factory);
  94. DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
  95. provider.provide(registrar);
  96. List<HostComponentRegistration> list = registrar.getRegistry();
  97. assertNotNull(list);
  98. assertEquals(1, list.size());
  99. assertEquals("foobarable", list.get(0).getProperties().get("bean-name"));
  100. assertEquals(2, list.get(0).getMainInterfaces().length);
  101. assertEquals(FOO_BARABLE_INTERFACES, new HashSet<Class>(Arrays.asList(list.get(0).getMainInterfaceClasses())));
  102. }
  103. public void testProvideWithNestedContexts() {
  104. XmlBeanFactory parentFactory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test.xml"));
  105. XmlBeanFactory childFactory = new XmlBeanFactory(new ClassPathResource("com/atlassian/plugin/spring/pluginns/plugins-spring-test-child.xml"), parentFactory);
  106. HostComponentProvider provider = getHostProvider(childFactory);
  107. assertTrue(parentFactory.containsBeanDefinition(HOST_COMPONENT_PROVIDER));
  108. assertTrue(childFactory.containsBeanDefinition(HOST_COMPONENT_PROVIDER));
  109. DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
  110. provider.provide(registrar);
  111. List<HostComponentRegistration> list = registrar.getRegistry();
  112. assertNotNull(list);
  113. assertEquals(2, list.size());
  114. }
  115. }