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 org.junit.Rule;
7   import org.junit.Test;
8   import org.junit.rules.ExpectedException;
9   import org.junit.runner.RunWith;
10  import org.mockito.Mock;
11  import org.mockito.junit.MockitoJUnitRunner;
12  import org.slf4j.Logger;
13  
14  import java.util.Collections;
15  
16  import static org.hamcrest.Matchers.equalTo;
17  import static org.junit.Assert.assertEquals;
18  import static org.mockito.ArgumentMatchers.anyString;
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.never;
21  import static org.mockito.Mockito.verify;
22  import static org.mockito.Mockito.when;
23  
24  @RunWith(MockitoJUnitRunner.class)
25  public class TestPrefixDelegatingModuleFactory {
26  
27      @Rule
28      public final ExpectedException expectedException = ExpectedException.none();
29  
30      @Mock
31      private ModuleDescriptor<Object> moduleDescriptor;
32  
33      @Test
34      public void testCreateBean() {
35          PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
36          when(moduleFactory.getPrefix()).thenReturn("jira");
37          Object bean = new Object();
38          when(moduleFactory.createModule("doSomething", moduleDescriptor)).thenReturn(bean);
39  
40          Object returnedBean = new PrefixDelegatingModuleFactory(Collections.singleton(moduleFactory))
41                  .createModule("jira:doSomething", moduleDescriptor);
42  
43          assertEquals(bean, returnedBean);
44      }
45  
46      @Test
47      public void testCreateBeanWithDynamicModuleFactory() {
48          PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
49          when(moduleFactory.getPrefix()).thenReturn("jira");
50  
51          Object bean = new Object();
52          ContainerAccessor containerAccessor = mock(ContainerAccessor.class);
53          ContainerManagedPlugin plugin = mock(ContainerManagedPlugin.class);
54          when(plugin.getContainerAccessor()).thenReturn(containerAccessor);
55          when(moduleDescriptor.getPlugin()).thenReturn(plugin);
56          when(containerAccessor.getBeansOfType(PrefixModuleFactory.class)).thenReturn(Collections.singleton(moduleFactory));
57          when(moduleFactory.createModule("doSomething", moduleDescriptor)).thenReturn(bean);
58  
59          Object returnedBean = new PrefixDelegatingModuleFactory(Collections.emptySet())
60                  .createModule("jira:doSomething", moduleDescriptor);
61  
62          assertEquals(bean, returnedBean);
63      }
64  
65      @Test
66      public void testCreateBeanThrowsNoClassDefFoundError() {
67          _testCreateWithThrowableCausingErrorLogMessage(new NoClassDefFoundError());
68      }
69  
70      @Test
71      public void testCreateBeanThrowsUnsatisfiedDependencyException() {
72          _testCreateWithThrowableCausingErrorLogMessage(new UnsatisfiedDependencyException());
73      }
74  
75      @Test
76      public void testCreateBeanThrowsLinkageError() {
77          _testCreateWithThrowableCausingErrorLogMessage(new LinkageError());
78      }
79  
80      @Test
81      public void testCreateBeanFailed() {
82          PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
83          when(moduleFactory.getPrefix()).thenReturn("bob");
84          expectedException.expect(PluginParseException.class);
85          expectedException.expectMessage("Failed to create a module. Prefix 'jira' not supported");
86  
87          new PrefixDelegatingModuleFactory(Collections.singleton(moduleFactory))
88                  .createModule("jira:doSomething", moduleDescriptor);
89  
90          verify(moduleFactory, never()).createModule("doSomething", moduleDescriptor);
91      }
92  
93      private void _testCreateWithThrowableCausingErrorLogMessage(Throwable throwable) {
94          PrefixModuleFactory moduleFactory = mock(PrefixModuleFactory.class);
95          when(moduleFactory.getPrefix()).thenReturn("jira");
96          Logger log = mock(Logger.class);
97  
98          Plugin plugin = mock(Plugin.class);
99          when(moduleDescriptor.getPlugin()).thenReturn(plugin);
100         when(moduleFactory.createModule("doSomething", moduleDescriptor)).thenThrow(throwable);
101 
102         PrefixDelegatingModuleFactory prefixDelegatingModuleFactory = new PrefixDelegatingModuleFactory(Collections.singleton(moduleFactory));
103         prefixDelegatingModuleFactory.log = log;
104         expectedException.expect(equalTo(throwable));
105 
106         try {
107             prefixDelegatingModuleFactory.createModule("jira:doSomething", moduleDescriptor);
108         } finally {
109             verify(log).error(anyString());
110         }
111     }
112 
113     private static class UnsatisfiedDependencyException extends RuntimeException {
114     }
115 }