aboutsummaryrefslogtreecommitdiffstats
path: root/pf4j
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2020-04-21 13:59:56 +0300
committerDecebal Suiu <decebal.suiu@gmail.com>2020-04-21 14:21:55 +0300
commit42bb7976de80ac8711f62d2278c234c240987b85 (patch)
tree53ba1d9163d9f322c5b652331f8ac7b7c88b1878 /pf4j
parentc10e9bc60caab66143f271a968d1b6a3babb615d (diff)
downloadpf4j-42bb7976de80ac8711f62d2278c234c240987b85.tar.gz
pf4j-42bb7976de80ac8711f62d2278c234c240987b85.zip
Add test for findExtensionAnnotation (preparation for #363)
Diffstat (limited to 'pf4j')
-rw-r--r--pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java2
-rw-r--r--pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java54
-rw-r--r--pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java10
3 files changed, 60 insertions, 6 deletions
diff --git a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java
index 0862501..e2bd327 100644
--- a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java
+++ b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java
@@ -348,7 +348,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
return new ExtensionWrapper<>(descriptor, pluginManager.getExtensionFactory());
}
- private Extension findExtensionAnnotation(Class<?> clazz) {
+ public static Extension findExtensionAnnotation(Class<?> clazz) {
if (clazz.isAnnotationPresent(Extension.class)) {
return clazz.getAnnotation(Extension.class);
}
diff --git a/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java b/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java
index 6383756..74ccadb 100644
--- a/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java
+++ b/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java
@@ -15,20 +15,30 @@
*/
package org.pf4j;
+import com.google.common.collect.ImmutableList;
+import com.google.common.io.ByteStreams;
+import com.google.testing.compile.Compilation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.pf4j.plugin.FailTestPlugin;
import org.pf4j.plugin.TestExtensionPoint;
+import javax.tools.JavaFileObject;
+import java.io.IOException;
+import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import static com.google.testing.compile.CompilationSubject.assertThat;
+import static com.google.testing.compile.Compiler.javac;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -192,4 +202,48 @@ public class AbstractExtensionFinderTest {
assertEquals(1, result.size());
}
+ @Test
+ public void findExtensionAnnotation() throws Exception {
+ Compilation compilation = javac().compile(ExtensionAnnotationProcessorTest.Greeting,
+ ExtensionAnnotationProcessorTest.WhazzupGreeting);
+ assertThat(compilation).succeededWithoutWarnings();
+ ImmutableList<JavaFileObject> generatedFiles = compilation.generatedFiles();
+ assertEquals(2, generatedFiles.size());
+
+ JavaFileObjectClassLoader classLoader = new JavaFileObjectClassLoader();
+ Map<String, Class<?>> loadedClasses = classLoader.loadClasses(new ArrayList<>(generatedFiles));
+ Class<?> clazz = loadedClasses.get("test.WhazzupGreeting");
+ Extension extension = AbstractExtensionFinder.findExtensionAnnotation(clazz);
+ assertNotNull(extension);
+ }
+
+ static class JavaFileObjectClassLoader extends ClassLoader {
+
+ public Map<String, Class<?>> loadClasses(List<JavaFileObject> classes) throws IOException {
+ // Sort generated ".class" by lastModified field
+ classes.sort((c1, c2) -> (int) (c1.getLastModified() - c2.getLastModified()));
+
+ // Load classes
+ Map<String, Class<?>> loadedClasses = new HashMap<>(classes.size());
+ for (JavaFileObject clazz : classes) {
+ String className = getClassName(clazz);
+ byte[] data = ByteStreams.toByteArray(clazz.openInputStream());
+ Class<?> loadedClass = defineClass(className, data,0, data.length);
+ loadedClasses.put(className, loadedClass);
+ }
+
+ return loadedClasses;
+ }
+
+ private static String getClassName(JavaFileObject object) {
+ String name = object.getName();
+ // Remove "/CLASS_OUT/" from head and ".class" from tail
+ name = name.substring(14, name.length() - 6);
+ name = name.replace('/', '.');
+
+ return name;
+ }
+
+ }
+
}
diff --git a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java
index 081be99..652c754 100644
--- a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java
+++ b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java
@@ -38,7 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/
public class ExtensionAnnotationProcessorTest {
- private static final JavaFileObject Greeting = JavaFileObjects.forSourceLines(
+ public static final JavaFileObject Greeting = JavaFileObjects.forSourceLines(
"Greeting",
"package test;",
"import org.pf4j.ExtensionPoint;",
@@ -47,7 +47,7 @@ public class ExtensionAnnotationProcessorTest {
" String getGreeting();",
"}");
- private static final JavaFileObject WhazzupGreeting = JavaFileObjects.forSourceLines(
+ public static final JavaFileObject WhazzupGreeting = JavaFileObjects.forSourceLines(
"WhazzupGreeting",
"package test;",
"import org.pf4j.Extension;",
@@ -60,7 +60,7 @@ public class ExtensionAnnotationProcessorTest {
" }",
"}");
- private static final JavaFileObject WhazzupGreeting_NoExtensionPoint = JavaFileObjects.forSourceLines(
+ public static final JavaFileObject WhazzupGreeting_NoExtensionPoint = JavaFileObjects.forSourceLines(
"WhazzupGreeting",
"package test;",
"import org.pf4j.Extension;",
@@ -73,7 +73,7 @@ public class ExtensionAnnotationProcessorTest {
" }",
"}");
- private static final JavaFileObject SpinnakerExtension = JavaFileObjects.forSourceLines(
+ public static final JavaFileObject SpinnakerExtension = JavaFileObjects.forSourceLines(
"SpinnakerExtension",
"package test;",
"",
@@ -91,7 +91,7 @@ public class ExtensionAnnotationProcessorTest {
"public @interface SpinnakerExtension {",
"}");
- private static final JavaFileObject WhazzupGreeting_SpinnakerExtension = JavaFileObjects.forSourceLines(
+ public static final JavaFileObject WhazzupGreeting_SpinnakerExtension = JavaFileObjects.forSourceLines(
"WhazzupGreeting",
"package test;",
"",