]> source.dussan.org Git - pf4j.git/commitdiff
Add more unit tests
authorDecebal Suiu <decebal.suiu@gmail.com>
Thu, 6 Apr 2017 19:10:50 +0000 (22:10 +0300)
committerDecebal Suiu <decebal.suiu@gmail.com>
Thu, 6 Apr 2017 19:10:50 +0000 (22:10 +0300)
pf4j/src/test/java/ro/fortsoft/pf4j/AbstractExtensionFinderTest.java [new file with mode: 0644]
pf4j/src/test/java/ro/fortsoft/pf4j/ExtensionAnnotationProcessorTest.java [new file with mode: 0644]
pf4j/src/test/java/ro/fortsoft/pf4j/LegacyExtensionStorageTest.java [new file with mode: 0644]
pf4j/src/test/java/ro/fortsoft/pf4j/plugin/FailTestExtension.java
pf4j/src/test/java/ro/fortsoft/pf4j/plugin/TestExtension.java
pf4j/src/test/java/ro/fortsoft/pf4j/plugin/TestExtensionPoint.java [new file with mode: 0644]

diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/AbstractExtensionFinderTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/AbstractExtensionFinderTest.java
new file mode 100644 (file)
index 0000000..47a1b20
--- /dev/null
@@ -0,0 +1,195 @@
+/*
+ * 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.TestExtensionPoint;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * @author Mario Franco
+ */
+public class AbstractExtensionFinderTest {
+
+    private PluginManager pluginManager;
+
+    @Before
+    public void setUp() {
+        PluginWrapper pluginStarted = mock(PluginWrapper.class);
+        when(pluginStarted.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
+        when(pluginStarted.getPluginState()).thenReturn(PluginState.STARTED);
+
+        PluginWrapper pluginStopped = mock(PluginWrapper.class);
+        when(pluginStopped.getPluginClassLoader()).thenReturn(getClass().getClassLoader());
+        when(pluginStopped.getPluginState()).thenReturn(PluginState.STOPPED);
+
+        pluginManager = mock(PluginManager.class);
+        when(pluginManager.getPlugin(eq("plugin1"))).thenReturn(pluginStarted);
+        when(pluginManager.getPlugin(eq("plugin2"))).thenReturn(pluginStopped);
+        when(pluginManager.getPluginClassLoader(eq("plugin1"))).thenReturn(getClass().getClassLoader());
+        when(pluginManager.getExtensionFactory()).thenReturn(new DefaultExtensionFactory());
+    }
+
+    @After
+    public void tearDown() {
+        pluginManager = null;
+    }
+
+    /**
+     * Test of {@link AbstractExtensionFinder#find(Class)}.
+     */
+    @Test
+    public void testFindFailType() {
+        ExtensionFinder instance = new AbstractExtensionFinder(pluginManager) {
+
+            @Override
+            public Map<String, Set<String>> readPluginsStorages() {
+                return Collections.emptyMap();
+            }
+
+            @Override
+            public Map<String, Set<String>> readClasspathStorages() {
+                return Collections.emptyMap();
+            }
+
+        };
+        List<ExtensionWrapper<FailTestPlugin>> list = instance.find(FailTestPlugin.class);
+        assertEquals(0, list.size());
+    }
+
+    /**
+     * Test of {@link AbstractExtensionFinder#find(Class)}.
+     */
+    @Test
+    public void testFindFromClasspath() {
+        ExtensionFinder instance = new AbstractExtensionFinder(pluginManager) {
+
+            @Override
+            public Map<String, Set<String>> readPluginsStorages() {
+                return Collections.emptyMap();
+            }
+
+            @Override
+            public Map<String, Set<String>> readClasspathStorages() {
+                Map<String, Set<String>> entries = new LinkedHashMap<>();
+
+                Set<String> bucket = new HashSet<>();
+                bucket.add("ro.fortsoft.pf4j.plugin.TestExtension");
+                bucket.add("ro.fortsoft.pf4j.plugin.FailTestExtension");
+                entries.put(null, bucket);
+
+                return entries;
+            }
+
+        };
+
+        List<ExtensionWrapper<TestExtensionPoint>> list = instance.find(TestExtensionPoint.class);
+        assertEquals(2, list.size());
+    }
+
+    /**
+     * Test of {@link AbstractExtensionFinder#find(Class, String)}.
+     */
+    @Test
+    public void testFindFromPlugin() {
+        ExtensionFinder instance = new AbstractExtensionFinder(pluginManager) {
+
+            @Override
+            public Map<String, Set<String>> readPluginsStorages() {
+                Map<String, Set<String>> entries = new LinkedHashMap<>();
+
+                Set<String> bucket = new HashSet<>();
+                bucket.add("ro.fortsoft.pf4j.plugin.TestExtension");
+                bucket.add("ro.fortsoft.pf4j.plugin.FailTestExtension");
+                entries.put("plugin1", bucket);
+                bucket = new HashSet<>();
+                bucket.add("ro.fortsoft.pf4j.plugin.TestExtension");
+                entries.put("plugin2", bucket);
+
+                return entries;
+            }
+
+            @Override
+            public Map<String, Set<String>> readClasspathStorages() {
+                return Collections.emptyMap();
+            }
+
+        };
+
+        List<ExtensionWrapper<TestExtensionPoint>> list = instance.find(TestExtensionPoint.class);
+        assertEquals(2, list.size());
+
+        list = instance.find(TestExtensionPoint.class, "plugin1");
+        assertEquals(2, list.size());
+
+        list = instance.find(TestExtensionPoint.class, "plugin2");
+        assertEquals(0, list.size());
+    }
+
+    /**
+     * Test of {@link AbstractExtensionFinder#findClassNames(String)}.
+     */
+    @Test
+    public void testFindClassNames() {
+        ExtensionFinder instance = new AbstractExtensionFinder(pluginManager) {
+
+            @Override
+            public Map<String, Set<String>> readPluginsStorages() {
+                Map<String, Set<String>> entries = new LinkedHashMap<>();
+
+                Set<String> bucket = new HashSet<>();
+                bucket.add("ro.fortsoft.pf4j.plugin.TestExtension");
+                entries.put("plugin1", bucket);
+
+                return entries;
+            }
+
+            @Override
+            public Map<String, Set<String>> readClasspathStorages() {
+                Map<String, Set<String>> entries = new LinkedHashMap<>();
+
+                Set<String> bucket = new HashSet<>();
+                bucket.add("ro.fortsoft.pf4j.plugin.TestExtension");
+                bucket.add("ro.fortsoft.pf4j.plugin.FailTestExtension");
+                entries.put(null, bucket);
+
+                return entries;
+            }
+
+        };
+
+        Set<String> result = instance.findClassNames(null);
+        assertEquals(2, result.size());
+
+        result = instance.findClassNames("plugin1");
+        assertEquals(1, result.size());
+    }
+
+}
diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/ExtensionAnnotationProcessorTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/ExtensionAnnotationProcessorTest.java
new file mode 100644 (file)
index 0000000..1c99396
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2017 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.Test;
+import ro.fortsoft.pf4j.processor.ExtensionAnnotationProcessor;
+
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Mario Franco
+ */
+public class ExtensionAnnotationProcessorTest {
+
+    /**
+     * Test of {@link ExtensionAnnotationProcessor#getSupportedAnnotationTypes()}.
+     */
+    @Test
+    public void testGetSupportedAnnotationTypes() {
+        ExtensionAnnotationProcessor instance = new ExtensionAnnotationProcessor();
+        Set<String> result = instance.getSupportedAnnotationTypes();
+        assertEquals(1, result.size());
+        assertTrue(result.contains(Extension.class.getName()));
+    }
+
+}
diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/LegacyExtensionStorageTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/LegacyExtensionStorageTest.java
new file mode 100644 (file)
index 0000000..aeeb371
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2017 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.Test;
+import ro.fortsoft.pf4j.processor.LegacyExtensionStorage;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Decebal Suiu
+ */
+public class LegacyExtensionStorageTest {
+
+    /**
+     * Test of {@link LegacyExtensionStorage#read(Reader, Set)}.
+     */
+    @Test
+    public void testRead() throws IOException {
+        Reader reader = new StringReader(
+            "# comment\n"
+                + "ro.fortsoft.pf4j.demo.hello.HelloPlugin$HelloGreeting\n"
+                + "ro.fortsoft.pf4j.demo.welcome.WelcomePlugin$WelcomeGreeting\n"
+                + "ro.fortsoft.pf4j.demo.welcome.OtherGreeting\n");
+
+        Set<String> entries = new HashSet<>();
+        LegacyExtensionStorage.read(reader, entries);
+        assertEquals(3, entries.size());
+    }
+
+}
index 4bc6110f9c8de5099c1e3a585944fc7fc61ce44e..3832b7f7924fb9c6d4cd62785a7be534f1dc25df 100644 (file)
  */
 package ro.fortsoft.pf4j.plugin;
 
-import ro.fortsoft.pf4j.ExtensionPoint;
+import ro.fortsoft.pf4j.Extension;
 
 /**
- *
  * @author Mario Franco
  */
-public class FailTestExtension implements ExtensionPoint {
+@Extension
+public class FailTestExtension implements TestExtensionPoint {
 
     public FailTestExtension(String name) {
     }
+
 }
index 3d74ffbe65d6d5c7586603cdf45fd89cc0185372..181e90df458fc3116805ea2feaa3e462f399128a 100644 (file)
  */
 package ro.fortsoft.pf4j.plugin;
 
-import ro.fortsoft.pf4j.ExtensionPoint;
+import ro.fortsoft.pf4j.Extension;
 
 /**
- *
  * @author Mario Franco
  */
-public class TestExtension implements ExtensionPoint {
+@Extension
+public class TestExtension implements TestExtensionPoint {
 
 }
diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/plugin/TestExtensionPoint.java b/pf4j/src/test/java/ro/fortsoft/pf4j/plugin/TestExtensionPoint.java
new file mode 100644 (file)
index 0000000..3c1ee1e
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * 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.ExtensionPoint;
+
+/**
+ * @author Mario Franco
+ */
+public interface TestExtensionPoint extends ExtensionPoint {
+
+}