Browse Source

Added tests for DefaultPluginFactory and multiple improvements

pull/59/head
Mário Franco 9 years ago
parent
commit
5bd40245b2
  1. 6
      pf4j/pom.xml
  2. 2
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginWrapper.java
  3. 84
      pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginFactoryTest.java
  4. 13
      pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java
  5. 51
      pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginStatusProviderTest.java
  6. 24
      pf4j/src/test/java/ro/fortsoft/pf4j/plugin/FailTestPlugin.java
  7. 31
      pf4j/src/test/java/ro/fortsoft/pf4j/plugin/TestPlugin.java
  8. 3
      pom.xml

6
pf4j/pom.xml

@ -43,6 +43,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2
pf4j/src/main/java/ro/fortsoft/pf4j/PluginWrapper.java

@ -54,7 +54,7 @@ public class PluginWrapper {
* for this plug-in. The class loader can be used to directly access
* plug-in resources and classes.
*/
public PluginClassLoader getPluginClassLoader() {
public ClassLoader getPluginClassLoader() {
return pluginClassLoader;
}

84
pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginFactoryTest.java

@ -0,0 +1,84 @@
/*
* Copyright 2015 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.fortsoft.pf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import ro.fortsoft.pf4j.plugin.FailTestPlugin;
import ro.fortsoft.pf4j.plugin.TestPlugin;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
*
* @author Mario Franco
*/
public class DefaultPluginFactoryTest {
public DefaultPluginFactoryTest() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of create method, of class DefaultPluginFactory.
*/
@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());
DefaultPluginFactory instance = new DefaultPluginFactory();
Plugin result = instance.create(pluginWrapper);
assertNotNull(result);
assertThat(result, instanceOf(TestPlugin.class));
}
/**
* Test of create method, of class DefaultPluginFactory.
*/
@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());
DefaultPluginFactory instance = new DefaultPluginFactory();
Plugin result = instance.create(pluginWrapper);
assertNull(result);
}
}

13
pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java

@ -1,5 +1,5 @@
/*
* Copyright 2015 Mario Franco.
* Copyright 2015 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,15 +19,16 @@ import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import ro.fortsoft.pf4j.util.ZipFileFilter;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
*
@ -74,9 +75,9 @@ public class DefaultPluginRepositoryTest {
public void testDeletePluginArchive() {
DefaultPluginRepository instance = new DefaultPluginRepository(testFolder.getRoot(), new ZipFileFilter());
assertEquals(true, instance.deletePluginArchive("/plugin-1"));
assertTrue(instance.deletePluginArchive("/plugin-1"));
assertEquals(false, instance.deletePluginArchive("/plugin-3"));
assertFalse(instance.deletePluginArchive("/plugin-3"));
List<File> result = instance.getPluginArchives();

51
pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginStatusProviderTest.java

@ -1,5 +1,5 @@
/*
* Copyright 2015 Mario Franco.
* Copyright 2015 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,7 +27,10 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
*
@ -71,9 +74,9 @@ public class DefaultPluginStatusProviderTest {
setUpEnabled();
DefaultPluginStatusProvider instance = new DefaultPluginStatusProvider(testFolder.getRoot());
assertEquals(false, instance.isPluginDisabled("plugin-1"));
assertEquals(true, instance.isPluginDisabled("plugin-2"));
assertEquals(true, instance.isPluginDisabled("plugin-3"));
assertFalse(instance.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2"));
assertTrue(instance.isPluginDisabled("plugin-3"));
}
/**
@ -83,9 +86,9 @@ public class DefaultPluginStatusProviderTest {
public void testIsPluginDisabledWithEnableEmpty() {
DefaultPluginStatusProvider instance = new DefaultPluginStatusProvider(testFolder.getRoot());
assertEquals(false, instance.isPluginDisabled("plugin-1"));
assertEquals(true, instance.isPluginDisabled("plugin-2"));
assertEquals(false, instance.isPluginDisabled("plugin-3"));
assertFalse(instance.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-3"));
}
/**
@ -96,10 +99,10 @@ public class DefaultPluginStatusProviderTest {
setUpEnabled();
DefaultPluginStatusProvider instance = new DefaultPluginStatusProvider(testFolder.getRoot());
assertEquals(true, instance.disablePlugin("plugin-1"));
assertEquals(true, instance.isPluginDisabled("plugin-1"));
assertEquals(true, instance.isPluginDisabled("plugin-2"));
assertEquals(true, instance.isPluginDisabled("plugin-3"));
assertTrue(instance.disablePlugin("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2"));
assertTrue(instance.isPluginDisabled("plugin-3"));
}
/**
@ -109,10 +112,10 @@ public class DefaultPluginStatusProviderTest {
public void testDisablePluginWithEnableEmpty() {
DefaultPluginStatusProvider instance = new DefaultPluginStatusProvider(testFolder.getRoot());
assertEquals(true, instance.disablePlugin("plugin-1"));
assertEquals(true, instance.isPluginDisabled("plugin-1"));
assertEquals(true, instance.isPluginDisabled("plugin-2"));
assertEquals(false, instance.isPluginDisabled("plugin-3"));
assertTrue(instance.disablePlugin("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-1"));
assertTrue(instance.isPluginDisabled("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-3"));
}
/**
@ -123,10 +126,10 @@ public class DefaultPluginStatusProviderTest {
setUpEnabled();
DefaultPluginStatusProvider instance = new DefaultPluginStatusProvider(testFolder.getRoot());
assertEquals(true, instance.enablePlugin("plugin-2"));
assertEquals(false, instance.isPluginDisabled("plugin-1"));
assertEquals(false, instance.isPluginDisabled("plugin-2"));
assertEquals(true, instance.isPluginDisabled("plugin-3"));
assertTrue(instance.enablePlugin("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-1"));
assertFalse(instance.isPluginDisabled("plugin-2"));
assertTrue(instance.isPluginDisabled("plugin-3"));
}
/**
@ -136,10 +139,10 @@ public class DefaultPluginStatusProviderTest {
public void testEnablePluginWithEnableEmpty() {
DefaultPluginStatusProvider instance = new DefaultPluginStatusProvider(testFolder.getRoot());
assertEquals(true, instance.enablePlugin("plugin-2"));
assertEquals(false, instance.isPluginDisabled("plugin-1"));
assertEquals(false, instance.isPluginDisabled("plugin-2"));
assertEquals(false, instance.isPluginDisabled("plugin-3"));
assertTrue(instance.enablePlugin("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-1"));
assertFalse(instance.isPluginDisabled("plugin-2"));
assertFalse(instance.isPluginDisabled("plugin-3"));
}
}

24
pf4j/src/test/java/ro/fortsoft/pf4j/plugin/FailTestPlugin.java

@ -0,0 +1,24 @@
/*
* Copyright 2015 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.fortsoft.pf4j.plugin;
/**
*
* @author Mario Franco
*/
public class FailTestPlugin {
}

31
pf4j/src/test/java/ro/fortsoft/pf4j/plugin/TestPlugin.java

@ -0,0 +1,31 @@
/*
* Copyright 2015 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.fortsoft.pf4j.plugin;
import ro.fortsoft.pf4j.Plugin;
import ro.fortsoft.pf4j.PluginWrapper;
/**
*
* @author Mario Franco
*/
public class TestPlugin extends Plugin {
public TestPlugin(PluginWrapper wrapper) {
super(wrapper);
}
}

3
pom.xml

@ -35,7 +35,8 @@
<java.version>1.7</java.version>
<junit.version>4.12</junit.version>
<cobertura.version>2.7</cobertura.version>
<junit.version>4.12</junit.version>
<mockito.version>2.0.28-beta</mockito.version>
<coveralls.version>3.1.0</coveralls.version>
</properties>

Loading…
Cancel
Save