super.init(processingEnv);
info("%s init", ExtensionAnnotationProcessor.class);
- storage = createStorage();
+
+ initStorage();
}
@Override
return oldExtensions;
}
+ public ExtensionStorage getStorage() {
+ return storage;
+ }
+
+ @SuppressWarnings("unchecked")
private List<TypeElement> findExtensionPoints(TypeElement extensionElement) {
List<TypeElement> extensionPointElements = new ArrayList<>();
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) {
// default storage
storage = new LegacyExtensionStorage(this);
}
-
- return storage;
}
}
*/
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());
}
}