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