Browse Source

Ad more tests in ExtensionAnnotationProcessorTest

pull/349/head
Decebal Suiu 5 years ago
parent
commit
a7bf2eb382
  1. 12
      pf4j/pom.xml
  2. 15
      pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java
  3. 109
      pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java

12
pf4j/pom.xml

@ -90,15 +90,15 @@
<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.
@ -109,20 +109,24 @@
<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>

15
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;
}
}

109
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());
}
}

Loading…
Cancel
Save