Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DefaultPluginFactoryTest.java 3.6KB

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