DefaultPluginFactory implements PluginFactory { @Override public Plugin create(final PluginWrapper pluginWrapper) { String pluginClassName = pluginWrapper.getDescriptor().getPluginClass(); log.debug("Create instance for plugin '{}'", pluginClassName); Class<?> pluginClass; try { pluginClass = pluginWrapper.getPluginClassLoader().loadClass(pluginClassName); } catch (ClassNotFoundException e) { log.error(e.getMessage(), e); return null; } int modifiers = pluginClass.getModifiers(); if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers) || (!Plugin.class.isAssignableFrom(pluginClass))) { log.error("The plugin class '{}' is not valid", pluginClassName); return null; } try { Constructor<?> constructor = pluginClass.getConstructor(PluginWrapper.class); return (Plugin) constructor.newInstance(pluginWrapper); } catch (Exception e) { log.error(e.getMessage(), e); } return null; }  @Override Plugin create(final PluginWrapper pluginWrapper);  }
@Test public void testCreate() { PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); when(pluginDescriptor.getPluginClass()).thenReturn(TestPlugin.class.getName()); PluginWrapper pluginWrapper = mock(PluginWrapper.class); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); PluginFactory pluginFactory = new DefaultPluginFactory(); Plugin result = pluginFactory.create(pluginWrapper); assertNotNull(result); assertThat(result, instanceOf(TestPlugin.class)); }
@Test public void testCreateFail() { PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); when(pluginDescriptor.getPluginClass()).thenReturn(FailTestPlugin.class.getName()); PluginWrapper pluginWrapper = mock(PluginWrapper.class); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); PluginFactory pluginFactory = new DefaultPluginFactory(); Plugin plugin = pluginFactory.create(pluginWrapper); assertNull(plugin); }
@Test public void testCreateFailNotFound() { PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); when(pluginDescriptor.getPluginClass()).thenReturn("org.pf4j.plugin.NotFoundTestPlugin"); PluginWrapper pluginWrapper = mock(PluginWrapper.class); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); PluginFactory pluginFactory = new DefaultPluginFactory(); Plugin plugin = pluginFactory.create(pluginWrapper); assertNull(plugin); }
@Test public void testCreateFailConstructor() { PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); when(pluginDescriptor.getPluginClass()).thenReturn(AnotherFailTestPlugin.class.getName()); PluginWrapper pluginWrapper = mock(PluginWrapper.class); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); PluginFactory pluginFactory = new DefaultPluginFactory(); Plugin plugin = pluginFactory.create(pluginWrapper); assertNull(plugin); }