View Javadoc

1   package com.atlassian.plugin.util;
2   
3   import com.atlassian.plugin.Application;
4   import com.atlassian.plugin.ModuleDescriptor;
5   import com.atlassian.plugin.Plugin;
6   import com.atlassian.plugin.descriptors.MockUnusedModuleDescriptor;
7   import com.atlassian.plugin.descriptors.RequiresRestart;
8   import com.google.common.base.Function;
9   import com.google.common.collect.Lists;
10  import org.dom4j.Element;
11  import org.junit.Test;
12  import org.junit.runner.RunWith;
13  import org.mockito.runners.MockitoJUnitRunner;
14  
15  import java.util.HashSet;
16  import java.util.List;
17  
18  import static com.google.common.collect.Lists.*;
19  import static com.google.common.collect.Sets.*;
20  import static org.junit.Assert.*;
21  import static org.mockito.Mockito.*;
22  
23  @RunWith(MockitoJUnitRunner.class)
24  public final class TestPluginUtils
25  {
26      private static final String ATLASSIAN_DEV_MODE = "atlassian.dev.mode";
27  
28      @Test
29      public void testPluginWithNoModuleDoesNotRequireRestartInDevMode()
30      {
31          try
32          {
33              System.setProperty(ATLASSIAN_DEV_MODE, "true");
34              final Plugin plugin = mockPluginWithModuleDescriptors();
35  
36              assertFalse(PluginUtils.doesPluginRequireRestart(plugin));
37          }
38          finally
39          {
40              System.clearProperty(ATLASSIAN_DEV_MODE);
41          }
42      }
43  
44      @Test
45      public void testPluginWithNoModuleDoesNotRequireRestartNoDevMode()
46      {
47          final Plugin plugin = mockPluginWithModuleDescriptors();
48  
49          assertFalse(PluginUtils.doesPluginRequireRestart(plugin));
50      }
51  
52      @Test
53      public void testPluginWithModuleRequiringRestartDoesNotRequireRestartInDevMode()
54      {
55          try
56          {
57              System.setProperty(ATLASSIAN_DEV_MODE, "true");
58              final Plugin plugin = mockPluginWithModuleDescriptors(new DynamicModuleDescriptor(), new RequiresRestartModuleDescriptor());
59  
60              assertFalse(PluginUtils.doesPluginRequireRestart(plugin));
61          }
62          finally
63          {
64              System.clearProperty(ATLASSIAN_DEV_MODE);
65          }
66      }
67  
68      @Test
69      public void testPluginWithModuleRequiringRestartDoesRequireRestartNoDevMode()
70      {
71          final Plugin plugin = mockPluginWithModuleDescriptors(new DynamicModuleDescriptor(), new RequiresRestartModuleDescriptor());
72  
73          assertTrue(PluginUtils.doesPluginRequireRestart(plugin));
74      }
75  
76      @Test
77      public void testPluginWithNoModuleRequiringRestartDoesNotRequireRestartNoDevMode()
78      {
79          final Plugin plugin = mockPluginWithModuleDescriptors(new DynamicModuleDescriptor());
80  
81          assertFalse(PluginUtils.doesPluginRequireRestart(plugin));
82      }
83  
84      private Plugin mockPluginWithModuleDescriptors(ModuleDescriptor<?>... descriptors)
85      {
86          final Plugin plugin = mock(Plugin.class);
87          when(plugin.getModuleDescriptors()).thenReturn(Lists.<ModuleDescriptor<?>>newArrayList(descriptors));
88          return plugin;
89      }
90  
91      @Test
92      public void testModuleElementAppliesWithNoInformation()
93      {
94          assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(null), applications()));
95      }
96  
97      @Test
98      public void testModuleElementAppliesWithApplicationAttributeJira()
99      {
100         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement("jira"), applications()));
101     }
102 
103     @Test
104     public void testModuleElementAppliesWithApplicationAttributeBamboo()
105     {
106         assertFalse(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement("bamboo"), applications()));
107     }
108 
109     @Test
110     public void testModuleElementAppliesWithApplicationAttributeJiraBamboo()
111     {
112         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement("jira,bamboo"), applications()));
113     }
114 
115     @Test
116     public void testModuleElementAppliesWithApplicationAttributeBambooConfluence()
117     {
118         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement("bamboo,confluence"), applications()));
119     }
120 
121     @Test
122     public void testModuleElementAppliesWithApplicationAttributeBambooCrowd()
123     {
124         assertFalse(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement("bamboo,crowd"), applications()));
125     }
126 
127     @Test
128     public void testModuleElementAppliesWithApplicationAttributeBambooCrowdAndBlank()
129     {
130         assertFalse(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement("bamboo,,crowd, ,"), applications()));
131     }
132 
133     @Test
134     public void testModuleElementAppliesWithApplicationAttributeBambooConfluenceAndBlank()
135     {
136         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement("bamboo,, , confluence, "), applications()));
137     }
138 
139     @Test
140     public void testModuleElementAppliesWithApplicationAttributeBlank()
141     {
142         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(" "), applications()));
143         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(" ,"), applications()));
144         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(" ,, "), applications()));
145         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(" ,, ,,,"), applications()));
146     }
147 
148     @Test
149     public void testModuleElementAppliesWithRestrictElementJira()
150     {
151         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("jira"))), applications()));
152     }
153 
154     @Test
155     public void testModuleElementAppliesWithRestrictElementBamboo()
156     {
157         assertFalse(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("bamboo"))), applications()));
158     }
159 
160     @Test
161     public void testModuleElementAppliesWithRestrictElementJiraBamboo()
162     {
163         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("jira"), newMockRestrictElement("bamboo"))), applications()));
164     }
165 
166     @Test
167     public void testModuleElementAppliesWithRestrictElementJiraConfluence()
168     {
169         assertTrue(PluginUtils.doesModuleElementApplyToApplication(newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("jira"), newMockRestrictElement("confluence"))), applications()));
170     }
171 
172     @Test
173     public void testModuleElementAppliesWithRestrictElementJiraAndVersionInRange()
174     {
175         assertTrue(PluginUtils.doesModuleElementApplyToApplication(
176                 newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("jira", "[4.0,)"))), applications()));
177     }
178 
179     @Test
180     public void testModuleElementAppliesWithRestrictElementJiraAndVersionOutOfRange()
181     {
182         assertFalse(PluginUtils.doesModuleElementApplyToApplication(
183                 newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("jira", "(,5.0)"))), applications()));
184     }
185 
186     @Test
187     public void testModuleElementAppliesWithRestrictElementJiraAndVersionsInRange()
188     {
189         assertTrue(PluginUtils.doesModuleElementApplyToApplication(
190                 newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("jira", "(,4.0)", "5.0"))), applications()));
191     }
192 
193     @Test
194     public void testModuleElementAppliesWithRestrictElementJiraAndVersionsOutOfRange()
195     {
196         assertFalse(PluginUtils.doesModuleElementApplyToApplication(
197                 newMockModuleElement(null, Lists.newArrayList(newMockRestrictElement("jira", "(,4.0]", "(5.0,)"))), applications()));
198     }
199 
200     private Element newMockRestrictElement(String app, String... versions)
201     {
202         final Element restrict = mock(Element.class);
203         when(restrict.attributeValue("application")).thenReturn(app);
204         if (versions != null && versions.length == 1)
205         {
206             when(restrict.attributeValue("version")).thenReturn(versions[0]);
207         }
208         else if (versions != null)
209         {
210             when(restrict.elements("version")).thenReturn(Lists.transform(newArrayList(versions), new Function<String, Element>()
211             {
212                 @Override
213                 public Element apply(String version)
214                 {
215                     final Element versionE = mock(Element.class);
216                     when(versionE.getText()).thenReturn(version);
217                     return versionE;
218                 }
219             }));
220         }
221 
222         return restrict;
223     }
224 
225     private HashSet<Application> applications()
226     {
227         return newHashSet(jira50(), confluence40());
228     }
229 
230     private Application confluence40()
231     {
232         return newApplication("confluence", "4.0");
233     }
234 
235     private Application jira50()
236     {
237         return newApplication("jira", "5.0");
238     }
239 
240     private Element newMockModuleElement(String app)
241     {
242         return newMockModuleElement(app, Lists.<Element>newArrayList());
243     }
244 
245     private Element newMockModuleElement(String app, List<Element> restricts)
246     {
247         final Element element = mock(Element.class);
248         when(element.attributeValue("application")).thenReturn(app);
249         when(element.elements("restrict")).thenReturn(restricts);
250         return element;
251     }
252 
253     private Application newApplication(String name, String version)
254     {
255         final Application app = mock(Application.class);
256         when(app.getKey()).thenReturn(name);
257         when(app.getVersion()).thenReturn(version);
258         return app;
259     }
260 
261     static class DynamicModuleDescriptor extends MockUnusedModuleDescriptor
262     {
263     }
264 
265     @RequiresRestart
266     static class RequiresRestartModuleDescriptor extends MockUnusedModuleDescriptor
267     {
268     }
269 }