View Javadoc
1   package com.atlassian.plugin;
2   
3   import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
4   import com.atlassian.plugin.impl.StaticPlugin;
5   import com.atlassian.plugin.loaders.PluginLoader;
6   import com.atlassian.plugin.manager.store.MemoryPluginPersistentStateStore;
7   import com.google.common.collect.ImmutableList;
8   import org.apache.log4j.Level;
9   import org.apache.log4j.Logger;
10  import org.junit.Before;
11  import org.junit.Test;
12  import org.junit.runner.RunWith;
13  import org.mockito.Mock;
14  import org.mockito.Mockito;
15  import org.mockito.junit.MockitoJUnitRunner;
16  
17  import static org.mockito.Mockito.mock;
18  import static org.mockito.Mockito.verify;
19  import static org.mockito.Mockito.when;
20  
21  /**
22   * Tests that the plugin manager properly notifies StateAware plugin modules of state
23   * transitions.
24   */
25  @RunWith(MockitoJUnitRunner.class)
26  public class TestStateAware {
27      private Combination mockEnabling;
28      private Combination mockDisabled;
29      private ModuleDescriptor mockThwarted;
30      private com.atlassian.plugin.manager.DefaultPluginManager manager;
31      private Plugin plugin1;
32  
33      @Mock
34      private PluginLoader pluginLoader;
35  
36      interface Combination extends StateAware, ModuleDescriptor {
37      }
38  
39      @Before
40      public void setUp() throws Exception {
41          // FIXME - the next line is here to prevent a null pointer exception caused by a debug logging
42          // a variable in the lifecycle is not initialized, which is fine for testing, but a debug logging causes an NPE
43  
44          Logger.getRootLogger().setLevel(Level.INFO);
45          mockEnabling = makeMockModule(Combination.class, "key1", "enabling", true);
46          mockDisabled = makeMockModule(Combination.class, "key1", "disabled", false);
47          mockThwarted = makeMockModule(ModuleDescriptor.class, "key1", "thwarted", true);
48  
49          plugin1 = new StaticPlugin();
50          plugin1.setPluginInformation(new PluginInformation());
51          plugin1.setKey("key1");
52          plugin1.enable();
53  
54          ModuleDescriptorFactory moduleDescriptorFactory = mock(ModuleDescriptorFactory.class);
55  
56          when(pluginLoader.loadAllPlugins(moduleDescriptorFactory)).thenReturn(ImmutableList.of(plugin1));
57  
58          manager = new com.atlassian.plugin.manager.DefaultPluginManager(new MemoryPluginPersistentStateStore(), ImmutableList.of(pluginLoader), moduleDescriptorFactory, new DefaultPluginEventManager());
59  
60      }
61  
62      /**
63       * Any StateAware plugin module that is active when the plugin manager is initialised should
64       * recieve an enabled message
65       */
66      @Test
67      public void stateAwareOnInit() throws PluginParseException {
68          plugin1.addModuleDescriptor(mockEnabling);
69          plugin1.addModuleDescriptor(mockThwarted);
70          plugin1.addModuleDescriptor(mockDisabled);
71          manager.init();
72          verify(mockEnabling).enabled();
73      }
74  
75      /**
76       * Any StateAware plugin moudle that is explicitly enabled or disabled through the plugin manager
77       * should receive the appropriate message
78       */
79      @Test
80      public void stateAwareOnPluginModule() throws PluginParseException {
81          plugin1.addModuleDescriptor(mockDisabled);
82          manager.init();
83  
84          when(mockDisabled.satisfiesMinJavaVersion()).thenReturn(true);
85          manager.enablePluginModule(mockDisabled.getCompleteKey());
86          verify(mockDisabled).enabled();
87  
88          manager.disablePluginModule(mockDisabled.getCompleteKey());
89          verify(mockDisabled).disabled();
90      }
91  
92      /**
93       * If a plugin is disabled, any modules that are currently enabled should be sent the disabled
94       * message
95       */
96      @Test
97      public void stateAwareOnPluginDisable() throws PluginParseException {
98          plugin1.addModuleDescriptor(mockEnabling);
99          plugin1.addModuleDescriptor(mockDisabled);
100 
101         manager.init();
102         verify(mockEnabling).enabled();
103 
104         manager.disablePlugin(plugin1.getKey());
105         verify(mockEnabling).disabled();
106     }
107 
108     /**
109      * If a plugin is enabled, any modules that are currently enabled should be sent the enabled
110      * message, but modules which are disabled should not.
111      */
112     @Test
113     public void disabledModuleDescriptorsAreEnabled() throws PluginParseException {
114         plugin1.addModuleDescriptor(mockEnabling);
115         plugin1.addModuleDescriptor(mockDisabled);
116         plugin1.setEnabledByDefault(false);
117 
118         manager.init();
119 
120         manager.enablePlugins(plugin1.getKey());
121         verify(mockEnabling).enabled();
122     }
123 
124     private <T extends ModuleDescriptor> T makeMockModule(Class<T> moduleClass, String pluginKey, String moduleKey, boolean enabledByDefault) {
125         ModuleDescriptor mock = Mockito.mock(moduleClass);
126         when(mock.getPluginKey()).thenReturn(pluginKey);
127         when(mock.getKey()).thenReturn(moduleKey);
128         when(mock.getCompleteKey()).thenReturn(pluginKey + ":" + moduleKey);
129         when(mock.isEnabledByDefault()).thenReturn(enabledByDefault);
130         when(mock.isEnabled()).thenReturn(true);
131         return (T) mock;
132     }
133 }