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