You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractPluginManagerTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import org.junit.jupiter.api.AfterEach;
  18. import org.junit.jupiter.api.BeforeEach;
  19. import org.junit.jupiter.api.Test;
  20. import org.pf4j.test.TestExtension;
  21. import org.pf4j.test.TestExtensionPoint;
  22. import java.io.IOException;
  23. import java.nio.file.Paths;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import static org.junit.jupiter.api.Assertions.assertEquals;
  28. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  29. import static org.junit.jupiter.api.Assertions.assertSame;
  30. import static org.junit.jupiter.api.Assertions.assertThrows;
  31. import static org.mockito.Mockito.CALLS_REAL_METHODS;
  32. import static org.mockito.Mockito.doNothing;
  33. import static org.mockito.Mockito.doReturn;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.never;
  36. import static org.mockito.Mockito.verify;
  37. import static org.mockito.Mockito.when;
  38. import static org.mockito.Mockito.withSettings;
  39. /**
  40. * @author Decebal Suiu
  41. */
  42. class AbstractPluginManagerTest {
  43. private AbstractPluginManager pluginManager;
  44. @BeforeEach
  45. void setUp() {
  46. pluginManager = mock(AbstractPluginManager.class, withSettings().useConstructor().defaultAnswer(CALLS_REAL_METHODS));
  47. }
  48. @AfterEach
  49. void tearDown() {
  50. pluginManager = null;
  51. }
  52. @Test
  53. void getExtensionsByType() {
  54. ExtensionFinder extensionFinder = mock(ExtensionFinder.class);
  55. List<ExtensionWrapper<TestExtensionPoint>> extensionList = new ArrayList<>(1);
  56. extensionList.add(new ExtensionWrapper<>(new ExtensionDescriptor(0, TestExtension.class), new DefaultExtensionFactory()));
  57. when(extensionFinder.find(TestExtensionPoint.class)).thenReturn(extensionList);
  58. pluginManager.extensionFinder = extensionFinder;
  59. List<TestExtensionPoint> extensions = pluginManager.getExtensions(TestExtensionPoint.class);
  60. assertEquals(1, extensions.size());
  61. }
  62. @Test
  63. void getVersion() {
  64. assertNotEquals("0.0.0", pluginManager.getVersion());
  65. }
  66. @Test
  67. void stopStartedPlugin() throws IOException {
  68. String pluginId = "plugin1";
  69. PluginWrapper pluginWrapper = createPluginWrapper(pluginId);
  70. pluginWrapper.setPluginState(PluginState.STARTED);
  71. doReturn(pluginWrapper).when(pluginManager).getPlugin(pluginId);
  72. doNothing().when(pluginManager).checkPluginId(pluginId);
  73. doReturn(new ArrayList<>(Arrays.asList(pluginWrapper))).when(pluginManager).getStartedPlugins();
  74. PluginState pluginState = pluginManager.stopPlugin(pluginId, false);
  75. verify(pluginWrapper.getPlugin()).stop();
  76. assertSame(PluginState.STOPPED, pluginState);
  77. }
  78. @Test
  79. void stopCreatedPlugin() {
  80. String pluginId = "plugin1";
  81. PluginWrapper pluginWrapper = createPluginWrapper(pluginId);
  82. doReturn(pluginWrapper).when(pluginManager).getPlugin(pluginId);
  83. doNothing().when(pluginManager).checkPluginId(pluginId);
  84. doReturn(new ArrayList<>(Arrays.asList(pluginWrapper))).when(pluginManager).getStartedPlugins();
  85. PluginState pluginState = pluginManager.stopPlugin(pluginId, false);
  86. verify(pluginWrapper.getPlugin(), never()).stop();
  87. assertSame(PluginState.CREATED, pluginState);
  88. }
  89. @Test
  90. void checkExistedPluginId() {
  91. String pluginId = "plugin1";
  92. PluginWrapper pluginWrapper = createPluginWrapper(pluginId);
  93. pluginManager.plugins.put("plugin1", pluginWrapper);
  94. pluginManager.checkPluginId("plugin1");
  95. }
  96. @Test
  97. void checkNotExistedPluginId() {
  98. assertThrows(IllegalArgumentException.class, () -> pluginManager.checkPluginId("plugin1"));
  99. }
  100. private PluginWrapper createPluginWrapper(String pluginId) {
  101. DefaultPluginDescriptor pluginDescriptor = new DefaultPluginDescriptor();
  102. pluginDescriptor.setPluginId(pluginId);
  103. pluginDescriptor.setPluginVersion("1.2.3");
  104. PluginWrapper pluginWrapper = new PluginWrapper(pluginManager, pluginDescriptor, Paths.get("plugin1"), getClass().getClassLoader());
  105. Plugin plugin= mock(Plugin.class);
  106. pluginWrapper.setPluginFactory(wrapper -> plugin);
  107. return pluginWrapper;
  108. }
  109. }