View Javadoc
1   package com.atlassian.plugin.manager;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.PluginArtifact;
5   import com.atlassian.plugin.PluginInformation;
6   import com.atlassian.plugin.jmx.PluginManagerMXBean;
7   import com.google.common.collect.ImmutableList;
8   import org.hamcrest.Description;
9   import org.hamcrest.Matcher;
10  import org.hamcrest.TypeSafeMatcher;
11  import org.junit.Before;
12  import org.junit.Test;
13  import org.junit.runner.RunWith;
14  import org.mockito.Mock;
15  import org.mockito.junit.MockitoJUnitRunner;
16  
17  import java.io.File;
18  import java.util.Date;
19  
20  import static org.hamcrest.MatcherAssert.assertThat;
21  import static org.hamcrest.Matchers.arrayContaining;
22  import static org.hamcrest.Matchers.equalTo;
23  import static org.hamcrest.Matchers.is;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.when;
26  
27  @RunWith(MockitoJUnitRunner.class)
28  public class TestDefaultPluginManagerJmxBridge {
29      @Mock
30      private DefaultPluginManager defaultPluginManager;
31  
32      // Hold this via the interface class so we provide usages of its methods
33      private PluginManagerMXBean defaultPluginManagerJmxBridge;
34  
35      @Before
36      public void setUp() throws Exception {
37          defaultPluginManagerJmxBridge = new DefaultPluginManagerJmxBridge(defaultPluginManager);
38      }
39  
40      @Test
41      public void getPluginsReturnsPlugins() {
42          final String pluginKey = "alpha";
43          final Date dateLoaded = new Date(123L);
44          final Date dateInstalled = new Date(456L);
45          final boolean enabledByDefault = true;
46          final boolean isBundledPlugin = true;
47          final Plugin plugin = mock(Plugin.class);
48          when(plugin.getKey()).thenReturn(pluginKey);
49          when(plugin.isEnabledByDefault()).thenReturn(enabledByDefault);
50          when(plugin.getDateInstalled()).thenReturn(dateInstalled);
51          when(plugin.getDateLoaded()).thenReturn(dateLoaded);
52          when(plugin.isBundledPlugin()).thenReturn(isBundledPlugin);
53  
54          final String version = "1.2.3";
55  
56          final PluginInformation pluginInformation = mock(PluginInformation.class);
57  
58          when(pluginInformation.getVersion()).thenReturn(version);
59          when(plugin.getPluginInformation()).thenReturn(pluginInformation);
60          when(defaultPluginManager.isPluginEnabled(pluginKey)).thenReturn(true);
61  
62          final Plugin nullPlugin = mock(Plugin.class);
63  
64          when(defaultPluginManager.getPlugins()).thenReturn(ImmutableList.of(plugin, nullPlugin));
65          final PluginManagerMXBean.PluginData[] pluginDatas = defaultPluginManagerJmxBridge.getPlugins();
66          assertThat(pluginDatas, arrayContaining(
67                  pluginData(pluginKey, version, true, true, null, dateInstalled.getTime(), dateLoaded.getTime(), isBundledPlugin),
68                  pluginData(null, null, false, false, null, null, null, false)
69          ));
70      }
71  
72      @Test
73      public void getPluginsForArtifactBasedPluginReturnsLocation() {
74          // Construct the Plugin object
75          final String pluginKey = "alpha";
76          final Date dateLoaded = new Date(123L);
77          final Date dateInstalled = new Date(456L);
78          final boolean enabledByDefault = true;
79          final boolean isBundledPlugin = true;
80          final Plugin plugin = mock(Plugin.class);
81  
82          when(plugin.getKey()).thenReturn(pluginKey);
83          when(plugin.isEnabledByDefault()).thenReturn(enabledByDefault);
84          when(plugin.getDateInstalled()).thenReturn(dateInstalled);
85          when(plugin.getDateLoaded()).thenReturn(dateLoaded);
86          when(plugin.isBundledPlugin()).thenReturn(isBundledPlugin);
87  
88          // Construct the PluginArtifact inside the PluginArtifactBackedPlugin
89          final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
90          File file = new File("/sw/ondemand/plugin/plugin-5.3.jar");
91          final String location = file.getAbsolutePath();
92          when(pluginArtifact.toFile()).thenReturn(file);
93          when(plugin.getPluginArtifact()).thenReturn(pluginArtifact);
94  
95          // Construct the PluginInformation inside the Plugin
96          final String version = "1.2.3";
97          final PluginInformation pluginInformation = mock(PluginInformation.class);
98          when(pluginInformation.getVersion()).thenReturn(version);
99          when(plugin.getPluginInformation()).thenReturn(pluginInformation);
100 
101         // Set the DefaultPluginManager values
102         when(defaultPluginManager.isPluginEnabled(pluginKey)).thenReturn(true);
103         when(defaultPluginManager.getPlugins()).thenReturn(ImmutableList.of(plugin));
104         final PluginManagerMXBean.PluginData[] pluginDatas = defaultPluginManagerJmxBridge.getPlugins();
105 
106         assertThat(pluginDatas, arrayContaining(
107                 pluginData(pluginKey, version, true, enabledByDefault, location, dateInstalled.getTime(), dateLoaded.getTime(), isBundledPlugin)
108         ));
109     }
110 
111     private Matcher<PluginManagerMXBean.PluginData> pluginData(final String key, final String version, final boolean enabled,
112                                                                final boolean enabledByDefault, final String location, final Long dateInstalled, final Long dateLoaded,
113                                                                final boolean isBundledPlugin) {
114         final Matcher<String> keyMatcher = equalTo(key);
115         final Matcher<String> versionMatcher = equalTo(version);
116         final Matcher<Boolean> enabledMatcher = equalTo(enabled);
117         final Matcher<String> locationMatcher = equalTo(location);
118         final Matcher<Long> dateInstalledMatcher = equalTo(dateInstalled);
119         final Matcher<Long> dateLoadedMatcher = equalTo(dateLoaded);
120         final Matcher<Boolean> enabledByDefaultMatcher = equalTo(enabledByDefault);
121         final Matcher<Boolean> isBundledPluginMatcher = equalTo(isBundledPlugin);
122 
123         return new TypeSafeMatcher<PluginManagerMXBean.PluginData>() {
124             @Override
125             protected boolean matchesSafely(final PluginManagerMXBean.PluginData item) {
126                 return keyMatcher.matches(item.getKey())
127                         && versionMatcher.matches(item.getVersion())
128                         && enabledMatcher.matches(item.isEnabled())
129                         && locationMatcher.matches(item.getLocation())
130                         && dateInstalledMatcher.matches(item.getDateInstalled())
131                         && dateLoadedMatcher.matches(item.getDateLoaded())
132                         && enabledByDefaultMatcher.matches(item.isEnabledByDefault())
133                         && isBundledPluginMatcher.matches(item.isBundledPlugin());
134             }
135 
136             @Override
137             public void describeTo(final Description description) {
138                 description.appendText("PluginData with key ");
139                 keyMatcher.describeTo(description);
140                 description.appendText(" and version ");
141                 versionMatcher.describeTo(description);
142                 description.appendText(" and enabled ");
143                 enabledMatcher.describeTo(description);
144                 description.appendText(" and location ");
145                 locationMatcher.describeTo(description);
146                 description.appendText(" and dateInstalled ");
147                 dateInstalledMatcher.describeTo(description);
148                 description.appendText(" and dateLoaded ");
149                 dateLoadedMatcher.describeTo(description);
150                 description.appendText(" and enabledByDefault ");
151                 enabledByDefaultMatcher.describeTo(description);
152                 description.appendText(" and isBundledPlugin ");
153                 isBundledPluginMatcher.describeTo(description);
154             }
155         };
156     }
157 
158     @Test
159     public void scanForNewPluginsDoesScanForNewPlugins() {
160         final int expected = 1;
161         when(defaultPluginManager.scanForNewPlugins()).thenReturn(expected);
162         final int actual = defaultPluginManagerJmxBridge.scanForNewPlugins();
163         assertThat(actual, is(expected));
164     }
165 }