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.

DefaultPluginFactoryTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.Test;
  18. import org.pf4j.test.AnotherFailTestPlugin;
  19. import org.pf4j.test.AnotherTestPlugin;
  20. import org.pf4j.test.FailTestPlugin;
  21. import org.pf4j.test.TestPlugin;
  22. import static org.hamcrest.CoreMatchers.instanceOf;
  23. import static org.hamcrest.MatcherAssert.assertThat;
  24. import static org.junit.jupiter.api.Assertions.assertNotNull;
  25. import static org.junit.jupiter.api.Assertions.assertNull;
  26. import static org.mockito.Mockito.mock;
  27. import static org.mockito.Mockito.when;
  28. /**
  29. * @author Mario Franco
  30. */
  31. public class DefaultPluginFactoryTest {
  32. @Test
  33. public void testCreate() {
  34. PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
  35. when(pluginDescriptor.getPluginClass()).thenReturn(TestPlugin.class.getName());
  36. PluginWrapper pluginWrapper = mock(PluginWrapper.class);
  37. when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
  38. when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
  39. PluginFactory pluginFactory = new DefaultPluginFactory();
  40. Plugin result = pluginFactory.create(pluginWrapper);
  41. assertNotNull(result);
  42. assertThat(result, instanceOf(TestPlugin.class));
  43. }
  44. @Test
  45. public void pluginConstructorNoParameters() {
  46. PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
  47. when(pluginDescriptor.getPluginClass()).thenReturn(AnotherTestPlugin.class.getName());
  48. PluginWrapper pluginWrapper = mock(PluginWrapper.class);
  49. when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
  50. when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
  51. PluginFactory pluginFactory = new DefaultPluginFactory();
  52. Plugin result = pluginFactory.create(pluginWrapper);
  53. assertNotNull(result);
  54. assertThat(result, instanceOf(AnotherTestPlugin.class));
  55. }
  56. @Test
  57. public void testCreateFail() {
  58. PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
  59. when(pluginDescriptor.getPluginClass()).thenReturn(FailTestPlugin.class.getName());
  60. PluginWrapper pluginWrapper = mock(PluginWrapper.class);
  61. when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
  62. when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
  63. PluginFactory pluginFactory = new DefaultPluginFactory();
  64. Plugin plugin = pluginFactory.create(pluginWrapper);
  65. assertNull(plugin);
  66. }
  67. @Test
  68. public void testCreateFailNotFound() {
  69. PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
  70. when(pluginDescriptor.getPluginClass()).thenReturn("org.pf4j.plugin.NotFoundTestPlugin");
  71. PluginWrapper pluginWrapper = mock(PluginWrapper.class);
  72. when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
  73. when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
  74. PluginFactory pluginFactory = new DefaultPluginFactory();
  75. Plugin plugin = pluginFactory.create(pluginWrapper);
  76. assertNull(plugin);
  77. }
  78. @Test
  79. public void testCreateFailConstructor() {
  80. PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
  81. when(pluginDescriptor.getPluginClass()).thenReturn(AnotherFailTestPlugin.class.getName());
  82. PluginWrapper pluginWrapper = mock(PluginWrapper.class);
  83. when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
  84. when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
  85. PluginFactory pluginFactory = new DefaultPluginFactory();
  86. Plugin plugin = pluginFactory.create(pluginWrapper);
  87. assertNull(plugin);
  88. }
  89. }