Browse Source

Formatting, rename fields refactoring, add comments

pull/225/merge
Decebal Suiu 7 years ago
parent
commit
c421a3b4e0
  1. 10
      pf4j/src/main/java/org/pf4j/DefaultPluginStatusProvider.java
  2. 40
      pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java
  3. 37
      pf4j/src/test/java/org/pf4j/DefaultPluginFactoryTest.java
  4. 78
      pf4j/src/test/java/org/pf4j/DefaultPluginRepositoryTest.java
  5. 122
      pf4j/src/test/java/org/pf4j/DefaultPluginStatusProviderTest.java
  6. 36
      pf4j/src/test/java/org/pf4j/LoadPluginsTest.java
  7. 31
      pf4j/src/test/java/org/pf4j/ManifestPluginDescriptorFinderTest.java
  8. 37
      pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java

10
pf4j/src/main/java/org/pf4j/DefaultPluginStatusProvider.java

@ -25,6 +25,8 @@ import java.util.List;
/** /**
* The default implementation for {@link PluginStatusProvider}. * The default implementation for {@link PluginStatusProvider}.
* The enabled plugins are read from {@code enabled.txt} file and
* the disabled plugins are read from {@code disabled.txt} file.
* *
* @author Decebal Suiu * @author Decebal Suiu
* @author Mário Franco * @author Mário Franco
@ -41,10 +43,6 @@ public class DefaultPluginStatusProvider implements PluginStatusProvider {
public DefaultPluginStatusProvider(Path pluginsRoot) { public DefaultPluginStatusProvider(Path pluginsRoot) {
this.pluginsRoot = pluginsRoot; this.pluginsRoot = pluginsRoot;
initialize();
}
private void initialize() {
try { try {
// create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file) // create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file)
enabledPlugins = FileUtils.readLines(pluginsRoot.resolve("enabled.txt"), true); enabledPlugins = FileUtils.readLines(pluginsRoot.resolve("enabled.txt"), true);
@ -83,14 +81,14 @@ public class DefaultPluginStatusProvider implements PluginStatusProvider {
@Override @Override
public boolean enablePlugin(String pluginId) { public boolean enablePlugin(String pluginId) {
try {
if (disabledPlugins.remove(pluginId)) { if (disabledPlugins.remove(pluginId)) {
try {
FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile()); FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile());
}
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to enable plugin {}", pluginId, e); log.error("Failed to enable plugin {}", pluginId, e);
return false; return false;
} }
}
return true; return true;
} }

40
pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java

@ -27,7 +27,8 @@ import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/** /**
* @author Decebal Suiu * @author Decebal Suiu
@ -35,26 +36,26 @@ import static org.junit.Assert.*;
public class CompoundPluginDescriptorFinderTest { public class CompoundPluginDescriptorFinderTest {
@Rule @Rule
public TemporaryFolder testFolder = new TemporaryFolder(); public TemporaryFolder pluginsFolder = new TemporaryFolder();
@Test @Test
public void add() { public void add() {
CompoundPluginDescriptorFinder instance = new CompoundPluginDescriptorFinder(); CompoundPluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder();
assertEquals(0, instance.size()); assertEquals(0, descriptorFinder.size());
instance.add(new PropertiesPluginDescriptorFinder()); descriptorFinder.add(new PropertiesPluginDescriptorFinder());
assertEquals(1, instance.size()); assertEquals(1, descriptorFinder.size());
} }
@Test @Test
public void find() throws Exception { public void find() throws Exception {
Path pluginPath = testFolder.newFolder("test-plugin-1").toPath(); Path pluginPath = pluginsFolder.newFolder("test-plugin-1").toPath();
Files.write(pluginPath.resolve("plugin.properties"), getPlugin1Properties(), StandardCharsets.UTF_8); Files.write(pluginPath.resolve("plugin.properties"), getPlugin1Properties(), StandardCharsets.UTF_8);
PluginDescriptorFinder instance = new CompoundPluginDescriptorFinder() PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder()
.add(new PropertiesPluginDescriptorFinder()); .add(new PropertiesPluginDescriptorFinder());
PluginDescriptor pluginDescriptor = instance.find(pluginPath); PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginPath);
assertNotNull(pluginDescriptor); assertNotNull(pluginDescriptor);
assertEquals("test-plugin-1", pluginDescriptor.getPluginId()); assertEquals("test-plugin-1", pluginDescriptor.getPluginId());
assertEquals("0.0.1", pluginDescriptor.getVersion()); assertEquals("0.0.1", pluginDescriptor.getVersion());
@ -62,14 +63,14 @@ public class CompoundPluginDescriptorFinderTest {
@Test @Test
public void findInJar() throws Exception { public void findInJar() throws Exception {
PluginDescriptorFinder instance = new CompoundPluginDescriptorFinder() PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder()
.add(new PropertiesPluginDescriptorFinder()); .add(new PropertiesPluginDescriptorFinder());
PluginZip pluginJar = new PluginZip.Builder(testFolder.newFile("my-plugin-1.2.3.jar"), "myPlugin") PluginZip pluginJar = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.jar"), "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
PluginDescriptor pluginDescriptor = instance.find(pluginJar.path()); PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginJar.path());
assertNotNull(pluginDescriptor); assertNotNull(pluginDescriptor);
assertEquals("myPlugin", pluginJar.pluginId()); assertEquals("myPlugin", pluginJar.pluginId());
assertEquals("1.2.3", pluginJar.pluginVersion()); assertEquals("1.2.3", pluginJar.pluginVersion());
@ -77,20 +78,21 @@ public class CompoundPluginDescriptorFinderTest {
@Test(expected = PluginException.class) @Test(expected = PluginException.class)
public void testNotFound() throws Exception { public void testNotFound() throws Exception {
PluginDescriptorFinder instance = new CompoundPluginDescriptorFinder(); PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder();
instance.find(getPluginsRoot().resolve("test-plugin-3")); Path pluginsPath = pluginsFolder.getRoot().toPath();
descriptorFinder.find(pluginsPath.resolve("test-plugin-3"));
} }
@Test @Test
public void testSpaceCharacterInFileName() throws Exception { public void testSpaceCharacterInFileName() throws Exception {
PluginDescriptorFinder instance = new PropertiesPluginDescriptorFinder(); PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
File jar = testFolder.newFile("my plugin-1.2.3.jar"); File jar = pluginsFolder.newFile("my plugin-1.2.3.jar");
PluginZip pluginJar = new PluginZip.Builder(jar, "myPlugin") PluginZip pluginJar = new PluginZip.Builder(jar, "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
PluginDescriptor pluginDescriptor = instance.find(pluginJar.path()); PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginJar.path());
assertNotNull(pluginDescriptor); assertNotNull(pluginDescriptor);
} }
@ -111,8 +113,4 @@ public class CompoundPluginDescriptorFinderTest {
return Arrays.asList(lines); return Arrays.asList(lines);
} }
private Path getPluginsRoot() {
return testFolder.getRoot().toPath();
}
} }

37
pf4j/src/test/java/org/pf4j/DefaultPluginFactoryTest.java

@ -16,9 +16,9 @@
package org.pf4j; package org.pf4j;
import org.junit.Test; import org.junit.Test;
import org.pf4j.plugin.AnotherFailTestPlugin;
import org.pf4j.plugin.FailTestPlugin; import org.pf4j.plugin.FailTestPlugin;
import org.pf4j.plugin.TestPlugin; import org.pf4j.plugin.TestPlugin;
import org.pf4j.plugin.AnotherFailTestPlugin;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -28,14 +28,10 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
*
* @author Mario Franco * @author Mario Franco
*/ */
public class DefaultPluginFactoryTest { public class DefaultPluginFactoryTest {
/**
* Test of create method, of class DefaultPluginFactory.
*/
@Test @Test
public void testCreate() { public void testCreate() {
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
@ -45,16 +41,13 @@ public class DefaultPluginFactoryTest {
when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
DefaultPluginFactory instance = new DefaultPluginFactory(); PluginFactory pluginFactory = new DefaultPluginFactory();
Plugin result = instance.create(pluginWrapper); Plugin result = pluginFactory.create(pluginWrapper);
assertNotNull(result); assertNotNull(result);
assertThat(result, instanceOf(TestPlugin.class)); assertThat(result, instanceOf(TestPlugin.class));
} }
/**
* Test of create method, of class DefaultPluginFactory.
*/
@Test @Test
public void testCreateFail() { public void testCreateFail() {
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
@ -64,15 +57,12 @@ public class DefaultPluginFactoryTest {
when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
DefaultPluginFactory instance = new DefaultPluginFactory(); PluginFactory pluginFactory = new DefaultPluginFactory();
Plugin result = instance.create(pluginWrapper); Plugin plugin = pluginFactory.create(pluginWrapper);
assertNull(result); assertNull(plugin);
} }
/**
* Test of create method, of class DefaultPluginFactory.
*/
@Test @Test
public void testCreateFailNotFound() { public void testCreateFailNotFound() {
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
@ -82,15 +72,12 @@ public class DefaultPluginFactoryTest {
when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
DefaultPluginFactory instance = new DefaultPluginFactory(); PluginFactory pluginFactory = new DefaultPluginFactory();
Plugin result = instance.create(pluginWrapper); Plugin plugin = pluginFactory.create(pluginWrapper);
assertNull(result); assertNull(plugin);
} }
/**
* Test of create method, of class DefaultPluginFactory.
*/
@Test @Test
public void testCreateFailConstructor() { public void testCreateFailConstructor() {
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class);
@ -100,10 +87,10 @@ public class DefaultPluginFactoryTest {
when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor); when(pluginWrapper.getDescriptor()).thenReturn(pluginDescriptor);
when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader()); when(pluginWrapper.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
DefaultPluginFactory instance = new DefaultPluginFactory(); PluginFactory pluginFactory = new DefaultPluginFactory();
Plugin result = instance.create(pluginWrapper); Plugin plugin = pluginFactory.create(pluginWrapper);
assertNull(result); assertNull(plugin);
} }
} }

78
pf4j/src/test/java/org/pf4j/DefaultPluginRepositoryTest.java

@ -36,21 +36,25 @@ import static org.junit.Assert.assertTrue;
*/ */
public class DefaultPluginRepositoryTest { public class DefaultPluginRepositoryTest {
private Path pluginsPath;
@Rule @Rule
public TemporaryFolder testFolder = new TemporaryFolder(); public TemporaryFolder pluginsFolder = new TemporaryFolder();
@Before @Before
public void setUp() throws IOException { public void setUp() throws IOException {
testFolder.newFolder("plugin-1"); pluginsPath = pluginsFolder.getRoot().toPath();
pluginsFolder.newFolder("plugin-1");
// Prove that we can delete a folder with a file inside // Prove that we can delete a folder with a file inside
Files.createFile(Paths.get(testFolder.getRoot().getAbsolutePath()).resolve("plugin-1").resolve("myfile")); Files.createFile(Paths.get(pluginsFolder.getRoot().getAbsolutePath()).resolve("plugin-1").resolve("myfile"));
// Create a zip file for plugin-1 to test that it is deleted when plugin is deleted // Create a zip file for plugin-1 to test that it is deleted when plugin is deleted
Files.createFile(Paths.get(testFolder.getRoot().getAbsolutePath()).resolve("plugin-1.zip")); Files.createFile(Paths.get(pluginsFolder.getRoot().getAbsolutePath()).resolve("plugin-1.zip"));
testFolder.newFolder("plugin-2"); pluginsFolder.newFolder("plugin-2");
testFolder.newFolder("plugin-3"); pluginsFolder.newFolder("plugin-3");
// standard maven/gradle bin folder - these should be skipped in development mode because the cause errors // standard maven/gradle bin folder - these should be skipped in development mode because the cause errors
testFolder.newFolder("target"); pluginsFolder.newFolder("target");
testFolder.newFolder("build"); pluginsFolder.newFolder("build");
} }
/** /**
@ -58,33 +62,29 @@ public class DefaultPluginRepositoryTest {
*/ */
@Test @Test
public void testGetPluginArchives() { public void testGetPluginArchives() {
Path pluginsRoot = getPluginsRoot(); PluginRepository repository = new DefaultPluginRepository(pluginsPath, false);
PluginRepository instance = new DefaultPluginRepository(pluginsRoot, false); List<Path> pluginPaths = repository.getPluginPaths();
List<Path> result = instance.getPluginPaths(); assertEquals(5, pluginPaths.size());
assertPathExists(pluginPaths, pluginsPath.resolve("plugin-1"));
assertEquals(5, result.size()); assertPathExists(pluginPaths, pluginsPath.resolve("plugin-2"));
assertPathExists(result, pluginsRoot.resolve("plugin-1")); assertPathExists(pluginPaths, pluginsPath.resolve("plugin-3"));
assertPathExists(result, pluginsRoot.resolve("plugin-2"));
assertPathExists(result, pluginsRoot.resolve("plugin-3"));
// when not in development mode we will honor these folders // when not in development mode we will honor these folders
assertPathExists(result, pluginsRoot.resolve("target")); assertPathExists(pluginPaths, pluginsPath.resolve("target"));
assertPathExists(result, pluginsRoot.resolve("build")); assertPathExists(pluginPaths, pluginsPath.resolve("build"));
} }
@Test @Test
public void testGetPluginArchivesInDevelopmentMode() { public void testGetPluginArchivesInDevelopmentMode() {
Path pluginsRoot = getPluginsRoot(); PluginRepository repository = new DefaultPluginRepository(pluginsPath, true);
PluginRepository instance = new DefaultPluginRepository(pluginsRoot, true);
List<Path> result = instance.getPluginPaths(); List<Path> pluginPaths = repository.getPluginPaths();
// target and build should be ignored // target and build should be ignored
assertEquals(3, result.size()); assertEquals(3, pluginPaths.size());
assertPathDoesNotExists(result, pluginsRoot.resolve("target")); assertPathDoesNotExists(pluginPaths, pluginsPath.resolve("target"));
assertPathDoesNotExists(result, pluginsRoot.resolve("build")); assertPathDoesNotExists(pluginPaths, pluginsPath.resolve("build"));
} }
/** /**
@ -92,22 +92,20 @@ public class DefaultPluginRepositoryTest {
*/ */
@Test @Test
public void testDeletePluginPath() { public void testDeletePluginPath() {
Path pluginsRoot = getPluginsRoot(); PluginRepository repository = new DefaultPluginRepository(pluginsPath, false);
PluginRepository instance = new DefaultPluginRepository(pluginsRoot, false); assertTrue(Files.exists(pluginsPath.resolve("plugin-1.zip")));
assertTrue(repository.deletePluginPath(pluginsPath.resolve("plugin-1")));
assertFalse(Files.exists(pluginsPath.resolve("plugin-1.zip")));
assertTrue(repository.deletePluginPath(pluginsPath.resolve("plugin-3")));
assertFalse(repository.deletePluginPath(pluginsPath.resolve("plugin-4")));
assertTrue(repository.deletePluginPath(pluginsPath.resolve("target")));
assertTrue(repository.deletePluginPath(pluginsPath.resolve("build")));
assertTrue(Files.exists(pluginsRoot.resolve("plugin-1.zip"))); List<Path> pluginPaths = repository.getPluginPaths();
assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-1")));
assertFalse(Files.exists(pluginsRoot.resolve("plugin-1.zip")));
assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-3")));
assertFalse(instance.deletePluginPath(pluginsRoot.resolve("plugin-4")));
assertTrue(instance.deletePluginPath(pluginsRoot.resolve("target")));
assertTrue(instance.deletePluginPath(pluginsRoot.resolve("build")));
List<Path> result = instance.getPluginPaths(); assertEquals(1, pluginPaths.size());
assertEquals(pluginsPath.relativize(pluginPaths.get(0)).toString(), "plugin-2");
assertEquals(1, result.size());
assertEquals(pluginsRoot.relativize(result.get(0)).toString(), "plugin-2");
} }
private void assertPathExists(List<Path> paths, Path path) { private void assertPathExists(List<Path> paths, Path path) {
@ -118,8 +116,4 @@ public class DefaultPluginRepositoryTest {
assertFalse("The directory must not contain the file " + path, paths.contains(path)); assertFalse("The directory must not contain the file " + path, paths.contains(path));
} }
private Path getPluginsRoot() {
return testFolder.getRoot().toPath();
}
} }

122
pf4j/src/test/java/org/pf4j/DefaultPluginStatusProviderTest.java

@ -15,6 +15,7 @@
*/ */
package org.pf4j; package org.pf4j;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
@ -35,131 +36,110 @@ import static org.junit.Assert.assertTrue;
*/ */
public class DefaultPluginStatusProviderTest { public class DefaultPluginStatusProviderTest {
private Path pluginsPath;
@Rule @Rule
public TemporaryFolder testFolder = new TemporaryFolder(); public TemporaryFolder pluginsFolder = new TemporaryFolder();
@Before
public void setUp() {
pluginsPath = pluginsFolder.getRoot().toPath();
}
/**
* Test of isPluginDisabled method, of class DefaultPluginStatusProvider.
*/
@Test @Test
public void testIsPluginDisabled() throws IOException { public void testIsPluginDisabled() throws IOException {
createEnabledFile(); createEnabledFile();
createDisabledFile(); createDisabledFile();
PluginStatusProvider instance = new DefaultPluginStatusProvider(getPluginsRoot()); PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
assertFalse(instance.isPluginDisabled("plugin-1")); assertFalse(statusProvider.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2")); assertTrue(statusProvider.isPluginDisabled("plugin-2"));
assertTrue(instance.isPluginDisabled("plugin-3")); assertTrue(statusProvider.isPluginDisabled("plugin-3"));
} }
/**
* Test of isPluginDisabled method, of class DefaultPluginStatusProvider.
*/
@Test @Test
public void testIsPluginDisabledWithEnableEmpty() throws IOException { public void testIsPluginDisabledWithEnableEmpty() throws IOException {
createDisabledFile(); createDisabledFile();
PluginStatusProvider instance = new DefaultPluginStatusProvider(getPluginsRoot()); PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
assertFalse(instance.isPluginDisabled("plugin-1")); assertFalse(statusProvider.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2")); assertTrue(statusProvider.isPluginDisabled("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-3")); assertFalse(statusProvider.isPluginDisabled("plugin-3"));
} }
/**
* Test of disablePlugin method, of class DefaultPluginStatusProvider.
*/
@Test @Test
public void testDisablePlugin() throws IOException { public void testDisablePlugin() throws IOException {
createEnabledFile(); createEnabledFile();
createDisabledFile(); createDisabledFile();
PluginStatusProvider instance = new DefaultPluginStatusProvider(getPluginsRoot()); PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
assertTrue(instance.disablePlugin("plugin-1")); assertTrue(statusProvider.disablePlugin("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-1")); assertTrue(statusProvider.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2")); assertTrue(statusProvider.isPluginDisabled("plugin-2"));
assertTrue(instance.isPluginDisabled("plugin-3")); assertTrue(statusProvider.isPluginDisabled("plugin-3"));
} }
/**
* Test of disablePlugin method, of class DefaultPluginStatusProvider.
*/
@Test @Test
public void testDisablePluginWithEnableEmpty() throws IOException { public void testDisablePluginWithEnableEmpty() throws IOException {
createDisabledFile(); createDisabledFile();
PluginStatusProvider instance = new DefaultPluginStatusProvider(getPluginsRoot()); PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
assertTrue(instance.disablePlugin("plugin-1")); assertTrue(statusProvider.disablePlugin("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-1")); assertTrue(statusProvider.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2")); assertTrue(statusProvider.isPluginDisabled("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-3")); assertFalse(statusProvider.isPluginDisabled("plugin-3"));
} }
/**
* Test of enablePlugin method, of class DefaultPluginStatusProvider.
*/
@Test @Test
public void testEnablePlugin() throws IOException { public void testEnablePlugin() throws IOException {
createEnabledFile(); createEnabledFile();
PluginStatusProvider instance = new DefaultPluginStatusProvider(getPluginsRoot()); PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
assertTrue(instance.enablePlugin("plugin-2")); assertTrue(statusProvider.enablePlugin("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-1")); assertFalse(statusProvider.isPluginDisabled("plugin-1"));
assertFalse(instance.isPluginDisabled("plugin-2")); assertFalse(statusProvider.isPluginDisabled("plugin-2"));
assertTrue(instance.isPluginDisabled("plugin-3")); assertTrue(statusProvider.isPluginDisabled("plugin-3"));
} }
/**
* Test of enablePlugin method, of class DefaultPluginStatusProvider.
*/
@Test @Test
public void testEnablePluginWithEnableEmpty() { public void testEnablePluginWithEnableEmpty() {
PluginStatusProvider instance = new DefaultPluginStatusProvider(getPluginsRoot()); PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
assertTrue(instance.enablePlugin("plugin-2")); assertTrue(statusProvider.enablePlugin("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-1")); assertFalse(statusProvider.isPluginDisabled("plugin-1"));
assertFalse(instance.isPluginDisabled("plugin-2")); assertFalse(statusProvider.isPluginDisabled("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-3")); assertFalse(statusProvider.isPluginDisabled("plugin-3"));
} }
/**
* Test of disablePlugin method without a disabled.txt file.
*/
@Test @Test
public void testDisablePluginWithoutDisabledFile() throws IOException { public void testDisablePluginWithoutDisabledFile() {
PluginStatusProvider instance = new DefaultPluginStatusProvider(getPluginsRoot()); PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
assertFalse(instance.isPluginDisabled("plugin-1")); assertFalse(statusProvider.isPluginDisabled("plugin-1"));
assertTrue(instance.disablePlugin("plugin-1")); assertTrue(statusProvider.disablePlugin("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-1")); assertTrue(statusProvider.isPluginDisabled("plugin-1"));
} }
private void createDisabledFile() throws IOException { private void createDisabledFile() throws IOException {
List<String> plugins = new ArrayList<>(); List<String> disabledPlugins = new ArrayList<>();
plugins.add("plugin-2"); disabledPlugins.add("plugin-2");
writeLines(plugins, "disabled.txt"); File disabledFile = pluginsFolder.newFile("disabled.txt");
FileUtils.writeLines(disabledPlugins, disabledFile);
} }
private void createEnabledFile() throws IOException { private void createEnabledFile() throws IOException {
List<String> plugins = new ArrayList<>(); List<String> enabledPlugins = new ArrayList<>();
plugins.add("plugin-1"); enabledPlugins.add("plugin-1");
plugins.add("plugin-2"); enabledPlugins.add("plugin-2");
writeLines(plugins, "enabled.txt");
}
private void writeLines(List<String> lines, String fileName) throws IOException {
File file = testFolder.newFile(fileName);
FileUtils.writeLines(lines, file);
}
private Path getPluginsRoot() { File enabledFile = pluginsFolder.newFile("enabled.txt");
return testFolder.getRoot().toPath(); FileUtils.writeLines(enabledPlugins, enabledFile);
} }
} }

36
pf4j/src/test/java/org/pf4j/LoadPluginsTest.java

@ -21,33 +21,37 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.pf4j.plugin.PluginZip; import org.pf4j.plugin.PluginZip;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class LoadPluginsTest { public class LoadPluginsTest {
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
private DefaultPluginManager pluginManager; private DefaultPluginManager pluginManager;
@Rule
public TemporaryFolder pluginsFolder = new TemporaryFolder();
@Before @Before
public void setup() { public void setUp() {
pluginManager = new DefaultPluginManager(testFolder.getRoot().toPath()); pluginManager = new DefaultPluginManager(pluginsFolder.getRoot().toPath());
} }
@Test @Test
public void load() throws Exception { public void load() throws Exception {
PluginZip pluginZip = new PluginZip.Builder(testFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin") PluginZip pluginZip = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
assertTrue(Files.exists(pluginZip.path())); assertTrue(Files.exists(pluginZip.path()));
assertEquals(0, pluginManager.getPlugins().size()); assertEquals(0, pluginManager.getPlugins().size());
pluginManager.loadPlugins(); pluginManager.loadPlugins();
assertTrue(Files.exists(pluginZip.path())); assertTrue(Files.exists(pluginZip.path()));
assertTrue(Files.exists(pluginZip.unzippedPath())); assertTrue(Files.exists(pluginZip.unzippedPath()));
assertEquals(1, pluginManager.getPlugins().size()); assertEquals(1, pluginManager.getPlugins().size());
@ -61,7 +65,7 @@ public class LoadPluginsTest {
@Test(expected = PluginAlreadyLoadedException.class) @Test(expected = PluginAlreadyLoadedException.class)
public void loadTwiceFails() throws Exception { public void loadTwiceFails() throws Exception {
PluginZip pluginZip = new PluginZip.Builder(testFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin") PluginZip pluginZip = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
@ -71,7 +75,7 @@ public class LoadPluginsTest {
@Test @Test
public void loadUnloadLoad() throws Exception { public void loadUnloadLoad() throws Exception {
PluginZip pluginZip = new PluginZip.Builder(testFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin") PluginZip pluginZip = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
@ -88,7 +92,7 @@ public class LoadPluginsTest {
@Test @Test
public void upgrade() throws Exception { public void upgrade() throws Exception {
new PluginZip.Builder(testFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin") new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
@ -98,7 +102,7 @@ public class LoadPluginsTest {
assertEquals(1, pluginManager.getPlugins().size()); assertEquals(1, pluginManager.getPlugins().size());
assertEquals(1, pluginManager.getStartedPlugins().size()); assertEquals(1, pluginManager.getStartedPlugins().size());
PluginZip pluginZip2 = new PluginZip.Builder(testFolder.newFile("my-plugin-2.0.0.ZIP"), "myPlugin") PluginZip pluginZip2 = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-2.0.0.ZIP"), "myPlugin")
.pluginVersion("2.0.0") .pluginVersion("2.0.0")
.build(); .build();
@ -114,12 +118,12 @@ public class LoadPluginsTest {
@Test @Test
public void getRoot() { public void getRoot() {
assertEquals(testFolder.getRoot().toPath(), pluginManager.getPluginsRoot()); assertEquals(pluginsFolder.getRoot().toPath(), pluginManager.getPluginsRoot());
} }
@Test @Test
public void notAPlugin() throws Exception { public void notAPlugin() throws Exception {
testFolder.newFile("not-a-zip"); pluginsFolder.newFile("not-a-zip");
pluginManager.loadPlugins(); pluginManager.loadPlugins();
@ -128,11 +132,11 @@ public class LoadPluginsTest {
@Test @Test
public void deletePlugin() throws Exception { public void deletePlugin() throws Exception {
PluginZip pluginZip1 = new PluginZip.Builder(testFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin") PluginZip pluginZip1 = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
PluginZip pluginZip3 = new PluginZip.Builder(testFolder.newFile("other-3.0.0.Zip"), "other") PluginZip pluginZip3 = new PluginZip.Builder(pluginsFolder.newFile("other-3.0.0.Zip"), "other")
.pluginVersion("3.0.0") .pluginVersion("3.0.0")
.build(); .build();

31
pf4j/src/test/java/org/pf4j/ManifestPluginDescriptorFinderTest.java

@ -36,37 +36,40 @@ import static org.junit.Assert.*;
public class ManifestPluginDescriptorFinderTest { public class ManifestPluginDescriptorFinderTest {
private VersionManager versionManager; private VersionManager versionManager;
private Path pluginsPath;
@Rule @Rule
public TemporaryFolder testFolder = new TemporaryFolder(); public TemporaryFolder pluginsFolder = new TemporaryFolder();
@Before @Before
public void setUp() throws IOException { public void setUp() throws IOException {
pluginsPath = pluginsFolder.getRoot().toPath();
Charset charset = Charset.forName("UTF-8"); Charset charset = Charset.forName("UTF-8");
Path pluginPath = testFolder.newFolder("test-plugin-1", "classes", "META-INF").toPath(); Path pluginPath = pluginsFolder.newFolder("test-plugin-1", "classes", "META-INF").toPath();
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes());
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin1Manifest(), charset); Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin1Manifest(), charset);
pluginPath = testFolder.newFolder("test-plugin-2", "classes", "META-INF").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-2", "classes", "META-INF").toPath();
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes());
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin2Manifest(), charset); Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin2Manifest(), charset);
// empty plugin // empty plugin
testFolder.newFolder("test-plugin-3"); pluginsFolder.newFolder("test-plugin-3");
// no plugin class // no plugin class
pluginPath = testFolder.newFolder("test-plugin-4", "classes", "META-INF").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-4", "classes", "META-INF").toPath();
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes());
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin4Manifest(), charset); Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin4Manifest(), charset);
// no plugin version // no plugin version
pluginPath = testFolder.newFolder("test-plugin-5", "classes", "META-INF").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-5", "classes", "META-INF").toPath();
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes());
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin5Manifest(), charset); Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin5Manifest(), charset);
// no plugin id // no plugin id
pluginPath = testFolder.newFolder("test-plugin-6", "classes", "META-INF").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-6", "classes", "META-INF").toPath();
Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); Files.write(pluginPath.resolve("extensions.idx"), "org.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes());
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin6Manifest(), charset); Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin6Manifest(), charset);
@ -78,10 +81,10 @@ public class ManifestPluginDescriptorFinderTest {
*/ */
@Test @Test
public void testFind() throws Exception { public void testFind() throws Exception {
PluginDescriptorFinder instance = new ManifestPluginDescriptorFinder(); PluginDescriptorFinder descriptorFinder = new ManifestPluginDescriptorFinder();
PluginDescriptor plugin1 = instance.find(getPluginsRoot().resolve("test-plugin-1")); PluginDescriptor plugin1 = descriptorFinder.find(pluginsPath.resolve("test-plugin-1"));
PluginDescriptor plugin2 = instance.find(getPluginsRoot().resolve("test-plugin-2")); PluginDescriptor plugin2 = descriptorFinder.find(pluginsPath.resolve("test-plugin-2"));
assertEquals("test-plugin-1", plugin1.getPluginId()); assertEquals("test-plugin-1", plugin1.getPluginId());
assertEquals("Test Plugin 1", plugin1.getPluginDescription()); assertEquals("Test Plugin 1", plugin1.getPluginDescription());
@ -109,8 +112,8 @@ public class ManifestPluginDescriptorFinderTest {
*/ */
@Test(expected = PluginException.class) @Test(expected = PluginException.class)
public void testFindNotFound() throws Exception { public void testFindNotFound() throws Exception {
PluginDescriptorFinder instance = new ManifestPluginDescriptorFinder(); PluginDescriptorFinder descriptorFinder = new ManifestPluginDescriptorFinder();
instance.find(getPluginsRoot().resolve("test-plugin-3")); descriptorFinder.find(pluginsPath.resolve("test-plugin-3"));
} }
private List<String> getPlugin1Manifest() { private List<String> getPlugin1Manifest() {
@ -229,8 +232,4 @@ public class ManifestPluginDescriptorFinderTest {
return Arrays.asList(lines); return Arrays.asList(lines);
} }
private Path getPluginsRoot() {
return testFolder.getRoot().toPath();
}
} }

37
pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java

@ -33,33 +33,36 @@ import static org.junit.Assert.*;
public class PropertiesPluginDescriptorFinderTest { public class PropertiesPluginDescriptorFinderTest {
private VersionManager versionManager; private VersionManager versionManager;
private Path pluginsPath;
@Rule @Rule
public TemporaryFolder testFolder = new TemporaryFolder(); public TemporaryFolder pluginsFolder = new TemporaryFolder();
@Before @Before
public void setUp() throws IOException { public void setUp() throws IOException {
pluginsPath = pluginsFolder.getRoot().toPath();
Charset charset = Charset.forName("UTF-8"); Charset charset = Charset.forName("UTF-8");
Path pluginPath = testFolder.newFolder("test-plugin-1").toPath(); Path pluginPath = pluginsFolder.newFolder("test-plugin-1").toPath();
Files.write(pluginPath.resolve("plugin.properties"), getPlugin1Properties(), charset); Files.write(pluginPath.resolve("plugin.properties"), getPlugin1Properties(), charset);
pluginPath = testFolder.newFolder("test-plugin-2").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-2").toPath();
Files.write(pluginPath.resolve("plugin.properties"), getPlugin2Properties(), charset); Files.write(pluginPath.resolve("plugin.properties"), getPlugin2Properties(), charset);
// empty plugin // empty plugin
testFolder.newFolder("test-plugin-3"); pluginsFolder.newFolder("test-plugin-3");
// no plugin class // no plugin class
pluginPath = testFolder.newFolder("test-plugin-4").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-4").toPath();
Files.write(pluginPath.resolve("plugin.properties"), getPlugin4Properties(), charset); Files.write(pluginPath.resolve("plugin.properties"), getPlugin4Properties(), charset);
// no plugin version // no plugin version
pluginPath = testFolder.newFolder("test-plugin-5").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-5").toPath();
Files.write(pluginPath.resolve("plugin.properties"), getPlugin5Properties(), charset); Files.write(pluginPath.resolve("plugin.properties"), getPlugin5Properties(), charset);
// no plugin id // no plugin id
pluginPath = testFolder.newFolder("test-plugin-6").toPath(); pluginPath = pluginsFolder.newFolder("test-plugin-6").toPath();
Files.write(pluginPath.resolve("plugin.properties"), getPlugin6Properties(), charset); Files.write(pluginPath.resolve("plugin.properties"), getPlugin6Properties(), charset);
versionManager = new DefaultVersionManager(); versionManager = new DefaultVersionManager();
@ -67,10 +70,10 @@ public class PropertiesPluginDescriptorFinderTest {
@Test @Test
public void testFind() throws Exception { public void testFind() throws Exception {
PluginDescriptorFinder instance = new PropertiesPluginDescriptorFinder(); PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
PluginDescriptor plugin1 = instance.find(getPluginsRoot().resolve("test-plugin-1")); PluginDescriptor plugin1 = descriptorFinder.find(pluginsPath.resolve("test-plugin-1"));
PluginDescriptor plugin2 = instance.find(getPluginsRoot().resolve("test-plugin-2")); PluginDescriptor plugin2 = descriptorFinder.find(pluginsPath.resolve("test-plugin-2"));
assertEquals("test-plugin-1", plugin1.getPluginId()); assertEquals("test-plugin-1", plugin1.getPluginId());
assertEquals("Test Plugin 1", plugin1.getPluginDescription()); assertEquals("Test Plugin 1", plugin1.getPluginDescription());
@ -98,20 +101,20 @@ public class PropertiesPluginDescriptorFinderTest {
@Test(expected = PluginException.class) @Test(expected = PluginException.class)
public void testNotFound() throws Exception { public void testNotFound() throws Exception {
PluginDescriptorFinder instance = new PropertiesPluginDescriptorFinder(); PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
instance.find(getPluginsRoot().resolve("test-plugin-3")); descriptorFinder.find(pluginsPath.resolve("test-plugin-3"));
} }
@Test @Test
public void findInJar() throws Exception { public void findInJar() throws Exception {
PluginZip pluginJar = new PluginZip.Builder(testFolder.newFile("my-plugin-1.2.3.jar"), "myPlugin") PluginZip pluginJar = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.jar"), "myPlugin")
.pluginVersion("1.2.3") .pluginVersion("1.2.3")
.build(); .build();
assertTrue(Files.exists(pluginJar.path())); assertTrue(Files.exists(pluginJar.path()));
PluginDescriptorFinder instance = new PropertiesPluginDescriptorFinder(); PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
PluginDescriptor pluginDescriptor = instance.find(pluginJar.path()); PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginJar.path());
assertNotNull(pluginDescriptor); assertNotNull(pluginDescriptor);
assertEquals("myPlugin", pluginJar.pluginId()); assertEquals("myPlugin", pluginJar.pluginId());
assertEquals("1.2.3", pluginJar.pluginVersion()); assertEquals("1.2.3", pluginJar.pluginVersion());
@ -190,8 +193,4 @@ public class PropertiesPluginDescriptorFinderTest {
return Arrays.asList(lines); return Arrays.asList(lines);
} }
private Path getPluginsRoot() {
return testFolder.getRoot().toPath();
}
} }

Loading…
Cancel
Save