summaryrefslogtreecommitdiffstats
path: root/pf4j/src
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2019-11-14 01:54:16 +0200
committerDecebal Suiu <decebal.suiu@gmail.com>2019-11-14 01:54:16 +0200
commita7bf2eb382c8a8be74f71bca1c44686929535114 (patch)
treed319a87adace39f54eee882a385d722083b441a6 /pf4j/src
parent4ba6bdc829341d1e1b545d37b613ba4ed3a7a5d8 (diff)
downloadpf4j-a7bf2eb382c8a8be74f71bca1c44686929535114.tar.gz
pf4j-a7bf2eb382c8a8be74f71bca1c44686929535114.zip
Ad more tests in ExtensionAnnotationProcessorTest
Diffstat (limited to 'pf4j/src')
-rw-r--r--pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java15
-rw-r--r--pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java109
2 files changed, 111 insertions, 13 deletions
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<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;
}
}
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<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());
}
}