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