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 static org.mockito.Mockito.*;
8   
9   import com.google.common.collect.ImmutableList;
10  import junit.framework.TestCase;
11  import org.apache.log4j.Level;
12  import org.apache.log4j.Logger;
13  import org.mockito.Mockito;
14  
15  import java.util.ArrayList;
16  import java.util.Collection;
17  
18  /**
19   * Tests that the plugin manager properly notifies StateAware plugin modules of state
20   * transitions.
21   */
22  public class TestStateAware extends TestCase
23  {
24      private Combination mockEnabling;
25      private Combination mockDisabled;
26      private ModuleDescriptor mockThwarted;
27      private com.atlassian.plugin.manager.DefaultPluginManager manager;
28      private Plugin plugin1;
29  
30      interface Combination extends StateAware, ModuleDescriptor{};
31  
32      protected void setUp() throws Exception
33      {
34      	// FIXME - the next line is here to prevent a null pointer exception caused by a debug logging
35      	// a variable in the lifecycle is not initialized, which is fine for testing, but a debug logging causes an NPE
36      	
37      	Logger.getRootLogger().setLevel(Level.INFO);
38          mockEnabling = makeMockModule(Combination.class, "key1", "enabling", true);
39          mockDisabled = makeMockModule(Combination.class, "key1", "disabled", false);
40          mockThwarted = makeMockModule(ModuleDescriptor.class, "key1", "thwarted", true);
41  
42          plugin1 = new StaticPlugin();
43          plugin1.setPluginInformation(new PluginInformation());
44          plugin1.setKey("key1");
45          plugin1.enable();
46  
47          PluginLoader pluginLoader = setupPluginLoader(plugin1);
48          ArrayList pluginLoaders = new ArrayList();
49          pluginLoaders.add(pluginLoader);
50  
51          ModuleDescriptorFactory moduleDescriptorFactory = mock(ModuleDescriptorFactory.class);
52  
53          manager = new com.atlassian.plugin.manager.DefaultPluginManager(new MemoryPluginPersistentStateStore(), pluginLoaders, moduleDescriptorFactory, new DefaultPluginEventManager());
54  
55      }
56  
57      /**
58       * Any StateAware plugin module that is active when the plugin manager is initialised should
59       * recieve an enabled message
60       */
61      public void testStateAwareOnInit() throws PluginParseException
62      {
63          plugin1.addModuleDescriptor(mockEnabling);
64          plugin1.addModuleDescriptor(mockThwarted);
65          plugin1.addModuleDescriptor(mockDisabled);
66          manager.init();
67          verify(mockEnabling).enabled();
68      }
69  
70      /**
71       * Any StateAware plugin moudle that is explicitly enabled or disabled through the plugin manager
72       * should receive the appropriate message
73       */
74      public void testStateAwareOnPluginModule() throws PluginParseException
75      {
76          plugin1.addModuleDescriptor(mockDisabled);
77          manager.init();
78  
79          when(mockDisabled.satisfiesMinJavaVersion()).thenReturn(true);
80          manager.enablePluginModule(mockDisabled.getCompleteKey());
81          verify(mockDisabled).enabled();
82  
83          manager.disablePluginModule(mockDisabled.getCompleteKey());
84          verify(mockDisabled).disabled();
85      }
86  
87      /**
88       * If a plugin is disabled, any modules that are currently enabled should be sent the disabled
89       * message
90       */
91      public void testStateAwareOnPluginDisable() throws PluginParseException
92      {
93          plugin1.addModuleDescriptor(mockEnabling);
94          plugin1.addModuleDescriptor(mockDisabled);
95  
96          manager.init();
97          verify(mockEnabling).enabled();
98  
99          manager.disablePlugin(plugin1.getKey());
100         verify(mockEnabling).disabled();
101     }
102 
103     /**
104      * If a plugin is enabled, any modules that are currently enabled should be sent the enabled
105      * message, but modules which are disabled should not.
106      */
107     public void testDisabledModuleDescriptorsAreEnabled() throws PluginParseException
108     {
109         plugin1.addModuleDescriptor(mockEnabling);
110         plugin1.addModuleDescriptor(mockDisabled);
111         plugin1.setEnabledByDefault(false);
112 
113         manager.init();
114 
115         manager.enablePlugin(plugin1.getKey());
116         verify(mockEnabling).enabled();
117     }
118 
119     private <T extends ModuleDescriptor> T makeMockModule(Class<T> moduleClass, String pluginKey, String moduleKey, boolean enabledByDefault)
120     {
121         ModuleDescriptor mock = Mockito.mock(moduleClass);
122         when(mock.getKey()).thenReturn(moduleKey);
123         when(mock.getCompleteKey()).thenReturn(pluginKey + ":" + moduleKey);
124         when(mock.isEnabledByDefault()).thenReturn(enabledByDefault);
125         return (T) mock;
126     }
127 
128     private PluginLoader setupPluginLoader(final Plugin plugin1)
129     {
130         final PluginLoader pluginLoader = new PluginLoader() //TODO: should this deployer support removal and addition?
131         {
132             public Iterable<Plugin> loadAllPlugins(ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
133             {
134                 return ImmutableList.of(plugin1);
135             }
136 
137             public boolean supportsAddition()
138             {
139                 return false;
140             }
141 
142             public boolean supportsRemoval()
143             {
144                 return false;
145             }
146 
147             public Collection removeMissingPlugins()
148             {
149                 return null;
150             }
151 
152             public Iterable<Plugin> loadFoundPlugins(ModuleDescriptorFactory moduleDescriptorFactory)
153             {
154                 return null;
155             }
156 
157             @Override
158             public boolean isDynamicPluginLoader()
159             {
160                 return false;
161             }
162 
163             public void removePlugin(Plugin plugin) throws PluginException
164             {
165                 throw new PluginException("This PluginLoader does not support removal");
166             }
167         };
168         return pluginLoader;
169     }
170 }