From 1715cb3d24962024cb2afc2f2d2c9417111a8490 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Tue, 21 Apr 2020 13:59:56 +0300 Subject: [PATCH] Add test for findExtensionAnnotation (preparation for #363) --- .../org/pf4j/AbstractExtensionFinder.java | 2 +- .../org/pf4j/AbstractExtensionFinderTest.java | 54 +++++++++++++++++++ .../ExtensionAnnotationProcessorTest.java | 10 ++-- 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 3cfbc7e..903e2cb 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 generatedFiles = compilation.generatedFiles(); + assertEquals(2, generatedFiles.size()); + + JavaFileObjectClassLoader classLoader = new JavaFileObjectClassLoader(); + Map> 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> loadClasses(List classes) throws IOException { + // Sort generated ".class" by lastModified field + classes.sort((c1, c2) -> (int) (c1.getLastModified() - c2.getLastModified())); + + // Load classes + Map> 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 11467c8..1dc8f60 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;", "", -- 2.39.5