PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-core/src/test/java/com/atlassian/plugin/manager/TestDefaultPluginManagerJmxBridge.java

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