]> source.dussan.org Git - pf4j.git/commitdiff
Ad more tests in ExtensionAnnotationProcessorTest
authorDecebal Suiu <decebal.suiu@gmail.com>
Wed, 13 Nov 2019 23:54:16 +0000 (01:54 +0200)
committerDecebal Suiu <decebal.suiu@gmail.com>
Wed, 13 Nov 2019 23:54:16 +0000 (01:54 +0200)
pf4j/pom.xml
pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java
pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java

index 22a712148673fcb5d8b49a44b7f561e3a3df5889..c24b30b985f896716377b1c9ed6943793c94821f 100644 (file)
             <optional>true</optional>
         </dependency>
 
+        <!-- Testing -->
         <dependency>
             <groupId>org.hamcrest</groupId>
             <artifactId>hamcrest</artifactId>
             <version>${hamcrest.version}</version>
             <scope>test</scope>
         </dependency>
-
         <dependency>
-            <!-- 
+            <!--
             An empty artifact, required while JUnit 4 is on the classpath to override its
             dependency on hamcrest.
 
             <version>${hamcrest.version}</version>
             <scope>test</scope>
         </dependency>
-
         <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-engine</artifactId>
             <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>
+        <dependency>
+            <groupId>com.google.testing.compile</groupId>
+            <artifactId>compile-testing</artifactId>
+            <version>0.18</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
index 1ac09558de5e3248020423a2a9322ebe70db305e..5d05113f6060827ad48e9119fb0129933429f5f3 100644 (file)
@@ -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<TypeElement> findExtensionPoints(TypeElement extensionElement) {
         List<TypeElement> 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;
     }
 
 }
index 47efca89810cbbe49261ebad4f8728c26444de72..1556472f7a1ff791be1fed0205377a9785834399 100644 (file)
  */
 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<String> 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<String> 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<String, String> 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<String, Set<String>> extensions = new HashMap<>();
+        extensions.put("test.Greeting", new HashSet<>(Collections.singletonList("test.WhazzupGreeting")));
+        assertEquals(extensions, processor.getExtensions());
     }
 
 }