From a7bf2eb382c8a8be74f71bca1c44686929535114 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Thu, 14 Nov 2019 01:54:16 +0200 Subject: Ad more tests in ExtensionAnnotationProcessorTest --- .../processor/ExtensionAnnotationProcessor.java | 15 +-- .../org/pf4j/ExtensionAnnotationProcessorTest.java | 109 +++++++++++++++++++-- 2 files changed, 111 insertions(+), 13 deletions(-) (limited to 'pf4j/src') diff --git a/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java b/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java index 1ac0955..5d05113 100644 --- a/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java +++ b/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java @@ -61,7 +61,8 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { super.init(processingEnv); info("%s init", ExtensionAnnotationProcessor.class); - storage = createStorage(); + + initStorage(); } @Override @@ -170,6 +171,11 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { return oldExtensions; } + public ExtensionStorage getStorage() { + return storage; + } + + @SuppressWarnings("unchecked") private List findExtensionPoints(TypeElement extensionElement) { List extensionPointElements = new ArrayList<>(); @@ -214,15 +220,12 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { return processingEnv.getTypeUtils().isAssignable(typeMirror, getExtensionPointType()); } - private TypeMirror getExtensionPointType() { return processingEnv.getElementUtils().getTypeElement(ExtensionPoint.class.getName()).asType(); } @SuppressWarnings("unchecked") - private ExtensionStorage createStorage() { - ExtensionStorage storage = null; - + private void initStorage() { // search in processing options String storageClassName = processingEnv.getOptions().get(STORAGE_CLASS_NAME); if (storageClassName == null) { @@ -245,8 +248,6 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { // default storage storage = new LegacyExtensionStorage(this); } - - return storage; } } diff --git a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java index 47efca8..1556472 100644 --- a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java +++ b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java @@ -15,28 +15,125 @@ */ package org.pf4j; +import com.google.testing.compile.Compilation; +import com.google.testing.compile.JavaFileObjects; import org.junit.jupiter.api.Test; import org.pf4j.processor.ExtensionAnnotationProcessor; +import org.pf4j.processor.LegacyExtensionStorage; +import javax.tools.JavaFileObject; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +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.assertTrue; /** * @author Mario Franco + * @author Decebal Suiu */ public class ExtensionAnnotationProcessorTest { - /** - * Test of {@link ExtensionAnnotationProcessor#getSupportedAnnotationTypes()}. - */ + private static final JavaFileObject Greeting = JavaFileObjects.forSourceLines( + "Greeting", + "package test;", + "import org.pf4j.ExtensionPoint;", + "", + "public interface Greeting extends ExtensionPoint {", + " String getGreeting();", + "}"); + + private static final JavaFileObject WhazzupGreeting = JavaFileObjects.forSourceLines( + "WhazzupGreeting", + "package test;", + "import org.pf4j.Extension;", + "", + "@Extension", + "public class WhazzupGreeting implements Greeting {", + " @Override", + " public String getGreeting() {", + " return \"Whazzup\";", + " }", + "}"); + + private static final JavaFileObject WhazzupGreeting_No_ExtensionPoint = JavaFileObjects.forSourceLines( + "WhazzupGreeting", + "package test;", + "import org.pf4j.Extension;", + "", + "@Extension", + "public class WhazzupGreeting {", + " @Override", + " public String getGreeting() {", + " return \"Whazzup\";", + " }", + "}"); + @Test - public void testGetSupportedAnnotationTypes() { + public void getSupportedAnnotationTypes() { ExtensionAnnotationProcessor instance = new ExtensionAnnotationProcessor(); Set result = instance.getSupportedAnnotationTypes(); assertEquals(1, result.size()); - assertTrue(result.contains(Extension.class.getName())); + assertEquals(Extension.class.getName(), result.iterator().next()); + } + + @Test + public void getSupportedOptions() { + ExtensionAnnotationProcessor instance = new ExtensionAnnotationProcessor(); + Set result = instance.getSupportedOptions(); + assertEquals(1, result.size()); + } + + @Test + public void options() { + ExtensionAnnotationProcessor processor = new ExtensionAnnotationProcessor(); + Compilation compilation = javac().withProcessors(processor).withOptions("-Ab=2", "-Ac=3") + .compile(Greeting, WhazzupGreeting); + assertEquals(compilation.status(), Compilation.Status.SUCCESS); + Map options = new HashMap<>(); + options.put("b", "2"); + options.put("c", "3"); + assertEquals(options, processor.getProcessingEnvironment().getOptions()); + } + + @Test + public void storage() { + ExtensionAnnotationProcessor processor = new ExtensionAnnotationProcessor(); + Compilation compilation = javac().withProcessors(processor).compile(Greeting, WhazzupGreeting); + assertEquals(compilation.status(), Compilation.Status.SUCCESS); + assertEquals(processor.getStorage().getClass(), LegacyExtensionStorage.class); + } + + @Test + public void compileWithoutError() { + ExtensionAnnotationProcessor processor = new ExtensionAnnotationProcessor(); + Compilation compilation = javac().withProcessors(processor).compile(Greeting, WhazzupGreeting); + assertThat(compilation).succeededWithoutWarnings(); + } + + @Test + public void compileWithError() { + ExtensionAnnotationProcessor processor = new ExtensionAnnotationProcessor(); + Compilation compilation = javac().withProcessors(processor).compile(Greeting, WhazzupGreeting_No_ExtensionPoint); + assertThat(compilation).failed(); + assertThat(compilation).hadErrorContaining("it doesn't implement ExtensionPoint") + .inFile(WhazzupGreeting_No_ExtensionPoint) + .onLine(5) + .atColumn(8); + } + + @Test + public void getExtensions() { + ExtensionAnnotationProcessor processor = new ExtensionAnnotationProcessor(); + Compilation compilation = javac().withProcessors(processor).compile(Greeting, WhazzupGreeting); + assertThat(compilation).succeededWithoutWarnings(); + Map> extensions = new HashMap<>(); + extensions.put("test.Greeting", new HashSet<>(Collections.singletonList("test.WhazzupGreeting"))); + assertEquals(extensions, processor.getExtensions()); } } -- cgit v1.2.3