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.runners.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
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
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
89 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
90 final String location = "/sw/ondemand/plugin/plugin-5.3.jar";
91 when(pluginArtifact.toFile()).thenReturn(new File(location));
92 when(plugin.getPluginArtifact()).thenReturn(pluginArtifact);
93
94
95 final String version = "1.2.3";
96 final PluginInformation pluginInformation = mock(PluginInformation.class);
97 when(pluginInformation.getVersion()).thenReturn(version);
98 when(plugin.getPluginInformation()).thenReturn(pluginInformation);
99
100
101 when(defaultPluginManager.isPluginEnabled(pluginKey)).thenReturn(true);
102 when(defaultPluginManager.getPlugins()).thenReturn(ImmutableList.<Plugin>of(plugin));
103 final PluginManagerMXBean.PluginData[] pluginDatas = defaultPluginManagerJmxBridge.getPlugins();
104
105 assertThat(pluginDatas, arrayContaining(
106 pluginData(pluginKey, version, true, enabledByDefault, location, dateInstalled.getTime(), dateLoaded.getTime(), isBundledPlugin)
107 ));
108 }
109
110 private Matcher<PluginManagerMXBean.PluginData> pluginData(final String key, final String version, final boolean enabled,
111 final boolean enabledByDefault, final String location, final Long dateInstalled, final Long dateLoaded,
112 final boolean isBundledPlugin) {
113 final Matcher<String> keyMatcher = equalTo(key);
114 final Matcher<String> versionMatcher = equalTo(version);
115 final Matcher<Boolean> enabledMatcher = equalTo(enabled);
116 final Matcher<String> locationMatcher = equalTo(location);
117 final Matcher<Long> dateInstalledMatcher = equalTo(dateInstalled);
118 final Matcher<Long> dateLoadedMatcher = equalTo(dateLoaded);
119 final Matcher<Boolean> enabledByDefaultMatcher = equalTo(enabledByDefault);
120 final Matcher<Boolean> isBundledPluginMatcher = equalTo(isBundledPlugin);
121
122 return new TypeSafeMatcher<PluginManagerMXBean.PluginData>() {
123 @Override
124 protected boolean matchesSafely(final PluginManagerMXBean.PluginData item) {
125 return keyMatcher.matches(item.getKey())
126 && versionMatcher.matches(item.getVersion())
127 && enabledMatcher.matches(item.isEnabled())
128 && locationMatcher.matches(item.getLocation())
129 && dateInstalledMatcher.matches(item.getDateInstalled())
130 && dateLoadedMatcher.matches(item.getDateLoaded())
131 && enabledByDefaultMatcher.matches(item.isEnabledByDefault())
132 && isBundledPluginMatcher.matches(item.isBundledPlugin());
133 }
134
135 @Override
136 public void describeTo(final Description description) {
137 description.appendText("PluginData with key ");
138 keyMatcher.describeTo(description);
139 description.appendText(" and version ");
140 versionMatcher.describeTo(description);
141 description.appendText(" and enabled ");
142 enabledMatcher.describeTo(description);
143 description.appendText(" and location ");
144 locationMatcher.describeTo(description);
145 description.appendText(" and dateInstalled ");
146 dateInstalledMatcher.describeTo(description);
147 description.appendText(" and dateLoaded ");
148 dateLoadedMatcher.describeTo(description);
149 description.appendText(" and enabledByDefault ");
150 enabledByDefaultMatcher.describeTo(description);
151 description.appendText(" and isBundledPlugin ");
152 isBundledPluginMatcher.describeTo(description);
153 }
154 };
155 }
156
157 @Test
158 public void scanForNewPluginsDoesScanForNewPlugins() {
159 final int expected = 1;
160 when(defaultPluginManager.scanForNewPlugins()).thenReturn(expected);
161 final int actual = defaultPluginManagerJmxBridge.scanForNewPlugins();
162 assertThat(actual, is(expected));
163 }
164 }