1 package com.atlassian.plugin.webresource;
2
3 import com.atlassian.plugin.ModuleDescriptor;
4 import com.atlassian.plugin.PluginAccessor;
5 import com.atlassian.plugin.PluginController;
6 import com.atlassian.plugin.event.PluginEventManager;
7 import com.atlassian.plugin.event.events.PluginFrameworkShutdownEvent;
8 import com.atlassian.plugin.event.events.PluginFrameworkStartedEvent;
9 import com.atlassian.plugin.event.events.PluginModuleDisabledEvent;
10 import com.atlassian.plugin.event.events.PluginModuleEnabledEvent;
11 import junit.framework.TestCase;
12
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15
16 public class TestWebResouceBatchingStateCounterImpl extends TestCase
17 {
18 private PluginEventManager pluginEventManager = mock(PluginEventManager.class);
19 private WebResourceBatchingStateCounterImpl batchingStateCounter;
20
21 private ModuleDescriptor moduleDescriptor = mock(ModuleDescriptor.class);
22 private PluginController pluginController = mock(PluginController.class);
23 private PluginAccessor pluginAccessor = mock(PluginAccessor.class);
24
25 @Override
26 protected void setUp() throws Exception
27 {
28 super.setUp();
29 batchingStateCounter = new WebResourceBatchingStateCounterImpl(pluginEventManager);
30 }
31
32 public void testNoIncrementDuringStartUp()
33 {
34 batchingStateCounter.onPluginModuleEnabled(new PluginModuleEnabledEvent(moduleDescriptor));
35 batchingStateCounter.onPluginModuleDisabled(new PluginModuleDisabledEvent(moduleDescriptor, false));
36 assertEquals(0, batchingStateCounter.getBatchingStateCounter());
37 }
38
39 public void testIncrementAfterStartUp()
40 {
41 batchingStateCounter.onPluginModuleEnabled(new PluginModuleEnabledEvent(moduleDescriptor));
42 batchingStateCounter.onPluginFrameworkStarted(new PluginFrameworkStartedEvent(pluginController, pluginAccessor));
43
44 batchingStateCounter.onPluginModuleEnabled(new PluginModuleEnabledEvent(moduleDescriptor));
45 batchingStateCounter.onPluginModuleEnabled(new PluginModuleEnabledEvent(moduleDescriptor));
46 assertEquals(3, batchingStateCounter.getBatchingStateCounter());
47 }
48
49 public void testNoIncrementAfterShutdown()
50 {
51 batchingStateCounter.onPluginFrameworkStarted(new PluginFrameworkStartedEvent(pluginController, pluginAccessor));
52 batchingStateCounter.onPluginModuleEnabled(new PluginModuleEnabledEvent(moduleDescriptor));
53 assertEquals(2, batchingStateCounter.getBatchingStateCounter());
54
55 batchingStateCounter.onPluginFrameworkPluginFrameworkShutdown(new PluginFrameworkShutdownEvent(pluginController, pluginAccessor));
56 batchingStateCounter.onPluginModuleEnabled(new PluginModuleEnabledEvent(moduleDescriptor));
57 assertEquals(2, batchingStateCounter.getBatchingStateCounter());
58 }
59
60 public void testUnregisterListenerAfterClose()
61 {
62 batchingStateCounter.close();
63 verify(pluginEventManager).unregister(batchingStateCounter);
64 }
65 }