View Javadoc
1   package com.atlassian.plugin.event.events;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.PluginParseException;
6   import com.atlassian.plugin.elements.ResourceDescriptor;
7   import com.atlassian.plugin.elements.ResourceLocation;
8   import com.google.common.collect.ImmutableList;
9   import org.dom4j.Element;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  import javax.annotation.Nonnull;
14  import java.util.List;
15  import java.util.Map;
16  
17  import static org.hamcrest.Matchers.equalTo;
18  import static org.junit.Assert.assertThat;
19  import static org.mockito.Mockito.mock;
20  
21  public class TestPluginTransactionEndEvent {
22  
23      private PluginTransactionEndEvent pluginTransactionStopEvent;
24      private Plugin plugin1Mock;
25      private Plugin plugin2Mock;
26      private ModuleDescriptor moduleDescriptorMock1;
27  
28      @Before
29      public void setUp() throws Exception {
30          plugin1Mock = mock(Plugin.class);
31          plugin2Mock = mock(Plugin.class);
32          moduleDescriptorMock1 = mock(FooModuleDescriptor.class);
33          pluginTransactionStopEvent = new PluginTransactionEndEvent(ImmutableList.of(
34                  new PluginDisablingEvent(plugin1Mock),
35                  new PluginModuleDisablingEvent(moduleDescriptorMock1, false),
36                  new PluginModuleDisabledEvent(moduleDescriptorMock1, false),
37                  new PluginDisabledEvent(plugin1Mock)));
38      }
39  
40      @Test
41      public void numberOfEvents() {
42          assertThat(pluginTransactionStopEvent.numberOfEvents(), equalTo(4));
43      }
44  
45      @Test
46      public void hasAnyEventOfTypeMatching() {
47          assertThat(pluginTransactionStopEvent.hasAnyEventOfTypeMatching(PluginDisablingEvent.class, pluginDisablingEvent -> pluginDisablingEvent.getPlugin().equals(plugin1Mock)),
48                  equalTo(true));
49          assertThat(pluginTransactionStopEvent.hasAnyEventOfTypeMatching(PluginDisablingEvent.class, pluginDisablingEvent -> pluginDisablingEvent.getPlugin().equals(plugin2Mock)),
50                  equalTo(false));
51          assertThat(pluginTransactionStopEvent.hasAnyEventOfTypeMatching(PluginEnablingEvent.class, pluginDisablingEvent -> pluginDisablingEvent.getPlugin().equals(plugin1Mock)),
52                  equalTo(false));
53      }
54  
55      @Test
56      public void hasAnyEventWithModuleDescriptorMatching() {
57          assertThat(pluginTransactionStopEvent.hasAnyEventWithModuleDescriptorMatching(FooModuleDescriptor.class::isInstance),
58                  equalTo(true));
59          assertThat(pluginTransactionStopEvent.hasAnyEventWithModuleDescriptorMatching(SomeModuleDescriptor.class::isInstance),
60                  equalTo(true));
61          assertThat(pluginTransactionStopEvent.hasAnyEventWithModuleDescriptorMatching(BarModuleDescriptor.class::isInstance),
62                  equalTo(false));
63      }
64  
65      private static class SomeModuleDescriptor implements ModuleDescriptor {
66  
67          @Override
68          public String getCompleteKey() {
69              throw new UnsupportedOperationException("Not implemented");
70          }
71  
72          @Override
73          public String getPluginKey() {
74              throw new UnsupportedOperationException("Not implemented");
75          }
76  
77          @Override
78          public String getKey() {
79              throw new UnsupportedOperationException("Not implemented");
80          }
81  
82          @Override
83          public String getName() {
84              throw new UnsupportedOperationException("Not implemented");
85          }
86  
87          @Override
88          public String getDescription() {
89              throw new UnsupportedOperationException("Not implemented");
90          }
91  
92          @Override
93          public Class getModuleClass() {
94              throw new UnsupportedOperationException("Not implemented");
95          }
96  
97          @Override
98          public Object getModule() {
99              throw new UnsupportedOperationException("Not implemented");
100         }
101 
102         @Override
103         public void init(@Nonnull Plugin plugin, @Nonnull Element element) throws PluginParseException {
104             throw new UnsupportedOperationException("Not implemented");
105         }
106 
107         @Override
108         public boolean isEnabledByDefault() {
109             throw new UnsupportedOperationException("Not implemented");
110         }
111 
112         @Override
113         public boolean isSystemModule() {
114             throw new UnsupportedOperationException("Not implemented");
115         }
116 
117         @Override
118         public void destroy() {
119             throw new UnsupportedOperationException("Not implemented");
120         }
121 
122         @Override
123         public Float getMinJavaVersion() {
124             throw new UnsupportedOperationException("Not implemented");
125         }
126 
127         @Override
128         public boolean satisfiesMinJavaVersion() {
129             throw new UnsupportedOperationException("Not implemented");
130         }
131 
132         @Override
133         public Map<String, String> getParams() {
134             throw new UnsupportedOperationException("Not implemented");
135         }
136 
137         @Override
138         public String getI18nNameKey() {
139             throw new UnsupportedOperationException("Not implemented");
140         }
141 
142         @Override
143         public String getDescriptionKey() {
144             throw new UnsupportedOperationException("Not implemented");
145         }
146 
147         @Override
148         public Plugin getPlugin() {
149             throw new UnsupportedOperationException("Not implemented");
150         }
151 
152         @Override
153         public boolean isEnabled() {
154             throw new UnsupportedOperationException("Not implemented");
155         }
156 
157         @Override
158         public List<ResourceDescriptor> getResourceDescriptors() {
159             throw new UnsupportedOperationException("Not implemented");
160         }
161 
162         @Override
163         public ResourceDescriptor getResourceDescriptor(String type, String name) {
164             throw new UnsupportedOperationException("Not implemented");
165         }
166 
167         @Override
168         public ResourceLocation getResourceLocation(String type, String name) {
169             throw new UnsupportedOperationException("Not implemented");
170         }
171     }
172 
173     private static class FooModuleDescriptor extends SomeModuleDescriptor {
174     }
175 
176     private static class BarModuleDescriptor extends SomeModuleDescriptor {
177     }
178 }