View Javadoc
1   package com.atlassian.plugin.module;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.PluginParseException;
6   import junit.framework.TestCase;
7   import org.slf4j.Logger;
8   
9   import java.util.Collections;
10  
11  import static org.mockito.Matchers.anyString;
12  import static org.mockito.Mockito.mock;
13  import static org.mockito.Mockito.never;
14  import static org.mockito.Mockito.verify;
15  import static org.mockito.Mockito.when;
16  
17  /**
18   */
19  public class TestPrefixDelegatingModuleFactory extends TestCase {
20      PrefixDelegatingModuleFactory prefixDelegatingModuleFactory;
21  
22      @Override
23      protected void setUp() throws Exception {
24          super.setUp();
25      }
26  
27      @Override
28      protected void tearDown() throws Exception {
29          super.tearDown();
30      }
31  
32      public void testCreateBean() throws Exception {
33          PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
34          when(moduleFactory.getPrefix()).thenReturn("jira");
35          Object bean = new Object();
36  
37          ModuleDescriptor moduleDescriptor = mock(ModuleDescriptor.class);
38          when(moduleFactory.createModule("doSomething", moduleDescriptor)).thenReturn(bean);
39          this.prefixDelegatingModuleFactory = new PrefixDelegatingModuleFactory(Collections.singleton(moduleFactory));
40  
41          final Object returnedBean = this.prefixDelegatingModuleFactory.createModule("jira:doSomething", moduleDescriptor);
42          assertEquals(bean, returnedBean);
43      }
44  
45      public void testCreateBeanWithDynamicModuleFactory() throws Exception {
46          PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
47          when(moduleFactory.getPrefix()).thenReturn("jira");
48  
49          Object bean = new Object();
50          ModuleDescriptor moduleDescriptor = mock(ModuleDescriptor.class);
51          ContainerAccessor containerAccessor = mock(ContainerAccessor.class);
52          ContainerManagedPlugin plugin = mock(ContainerManagedPlugin.class);
53          when(plugin.getContainerAccessor()).thenReturn(containerAccessor);
54          when(moduleDescriptor.getPlugin()).thenReturn(plugin);
55          when(containerAccessor.getBeansOfType(PrefixModuleFactory.class)).thenReturn(Collections.singleton(moduleFactory));
56  
57          when(moduleFactory.createModule("doSomething", moduleDescriptor)).thenReturn(bean);
58  
59          this.prefixDelegatingModuleFactory = new PrefixDelegatingModuleFactory(Collections.<PrefixModuleFactory>emptySet());
60  
61          final Object returnedBean = this.prefixDelegatingModuleFactory.createModule("jira:doSomething", moduleDescriptor);
62          assertEquals(bean, returnedBean);
63      }
64  
65      public void testCreateBeanThrowsNoClassDefFoundError() throws Exception {
66          _testCreateWithThrowableCausingErrorLogMessage(new NoClassDefFoundError());
67      }
68  
69      public void testCreateBeanThrowsUnsatisfiedDependencyException() throws Exception {
70          _testCreateWithThrowableCausingErrorLogMessage(new UnsatisfiedDependencyException());
71      }
72  
73      public void testCreateBeanThrowsLinkageError() throws Exception {
74          _testCreateWithThrowableCausingErrorLogMessage(new LinkageError());
75      }
76  
77      private void _testCreateWithThrowableCausingErrorLogMessage(Throwable throwable) {
78          PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
79          when(moduleFactory.getPrefix()).thenReturn("jira");
80          Logger log = mock(Logger.class);
81  
82  
83          Plugin plugin = mock(Plugin.class);
84          ModuleDescriptor moduleDescriptor = mock(ModuleDescriptor.class);
85          when(moduleDescriptor.getPlugin()).thenReturn(plugin);
86          when(moduleFactory.createModule("doSomething", moduleDescriptor)).thenThrow(throwable);
87  
88          this.prefixDelegatingModuleFactory = new PrefixDelegatingModuleFactory(Collections.singleton(moduleFactory));
89          this.prefixDelegatingModuleFactory.log = log;
90  
91          try {
92              this.prefixDelegatingModuleFactory.createModule("jira:doSomething", moduleDescriptor);
93  
94              fail("Should not return");
95          } catch (Throwable err) {
96              verify(log).error(anyString());
97          }
98      }
99  
100     public void testCreateBeanFailed() throws Exception {
101         PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
102         when(moduleFactory.getPrefix()).thenReturn("bob");
103         ModuleDescriptor moduleDescriptor = mock(ModuleDescriptor.class);
104 
105         this.prefixDelegatingModuleFactory = new PrefixDelegatingModuleFactory(Collections.singleton(moduleFactory));
106 
107         try {
108             this.prefixDelegatingModuleFactory.createModule("jira:doSomething", moduleDescriptor);
109 
110             fail("Should not return, there is no module prefix provider for jira");
111         } catch (PluginParseException ex) {
112             //Ex
113             assertEquals("Failed to create a module. Prefix 'jira' not supported", ex.getMessage());
114         }
115         verify(moduleFactory, never()).createModule("doSomething", moduleDescriptor);
116     }
117 
118     private static class UnsatisfiedDependencyException extends RuntimeException {
119     }
120 }