Browse Source

Add test for findExtensionAnnotation (preparation for #363)

pull/376/head
Decebal Suiu 5 years ago
parent
commit
42bb7976de
  1. 2
      pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java
  2. 54
      pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java
  3. 10
      pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java

2
pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java

@ -348,7 +348,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
return new ExtensionWrapper<>(descriptor, pluginManager.getExtensionFactory()); return new ExtensionWrapper<>(descriptor, pluginManager.getExtensionFactory());
} }
private Extension findExtensionAnnotation(Class<?> clazz) { public static Extension findExtensionAnnotation(Class<?> clazz) {
if (clazz.isAnnotationPresent(Extension.class)) { if (clazz.isAnnotationPresent(Extension.class)) {
return clazz.getAnnotation(Extension.class); return clazz.getAnnotation(Extension.class);
} }

54
pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java

@ -15,20 +15,30 @@
*/ */
package org.pf4j; package org.pf4j;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import com.google.testing.compile.Compilation;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pf4j.plugin.FailTestPlugin; import org.pf4j.plugin.FailTestPlugin;
import org.pf4j.plugin.TestExtensionPoint; import org.pf4j.plugin.TestExtensionPoint;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.eq; import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -192,4 +202,48 @@ public class AbstractExtensionFinderTest {
assertEquals(1, result.size()); assertEquals(1, result.size());
} }
@Test
public void findExtensionAnnotation() throws Exception {
Compilation compilation = javac().compile(ExtensionAnnotationProcessorTest.Greeting,
ExtensionAnnotationProcessorTest.WhazzupGreeting);
assertThat(compilation).succeededWithoutWarnings();
ImmutableList<JavaFileObject> generatedFiles = compilation.generatedFiles();
assertEquals(2, generatedFiles.size());
JavaFileObjectClassLoader classLoader = new JavaFileObjectClassLoader();
Map<String, Class<?>> loadedClasses = classLoader.loadClasses(new ArrayList<>(generatedFiles));
Class<?> clazz = loadedClasses.get("test.WhazzupGreeting");
Extension extension = AbstractExtensionFinder.findExtensionAnnotation(clazz);
assertNotNull(extension);
}
static class JavaFileObjectClassLoader extends ClassLoader {
public Map<String, Class<?>> loadClasses(List<JavaFileObject> classes) throws IOException {
// Sort generated ".class" by lastModified field
classes.sort((c1, c2) -> (int) (c1.getLastModified() - c2.getLastModified()));
// Load classes
Map<String, Class<?>> loadedClasses = new HashMap<>(classes.size());
for (JavaFileObject clazz : classes) {
String className = getClassName(clazz);
byte[] data = ByteStreams.toByteArray(clazz.openInputStream());
Class<?> loadedClass = defineClass(className, data,0, data.length);
loadedClasses.put(className, loadedClass);
}
return loadedClasses;
}
private static String getClassName(JavaFileObject object) {
String name = object.getName();
// Remove "/CLASS_OUT/" from head and ".class" from tail
name = name.substring(14, name.length() - 6);
name = name.replace('/', '.');
return name;
}
}
} }

10
pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java

@ -38,7 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/ */
public class ExtensionAnnotationProcessorTest { public class ExtensionAnnotationProcessorTest {
private static final JavaFileObject Greeting = JavaFileObjects.forSourceLines( public static final JavaFileObject Greeting = JavaFileObjects.forSourceLines(
"Greeting", "Greeting",
"package test;", "package test;",
"import org.pf4j.ExtensionPoint;", "import org.pf4j.ExtensionPoint;",
@ -47,7 +47,7 @@ public class ExtensionAnnotationProcessorTest {
" String getGreeting();", " String getGreeting();",
"}"); "}");
private static final JavaFileObject WhazzupGreeting = JavaFileObjects.forSourceLines( public static final JavaFileObject WhazzupGreeting = JavaFileObjects.forSourceLines(
"WhazzupGreeting", "WhazzupGreeting",
"package test;", "package test;",
"import org.pf4j.Extension;", "import org.pf4j.Extension;",
@ -60,7 +60,7 @@ public class ExtensionAnnotationProcessorTest {
" }", " }",
"}"); "}");
private static final JavaFileObject WhazzupGreeting_NoExtensionPoint = JavaFileObjects.forSourceLines( public static final JavaFileObject WhazzupGreeting_NoExtensionPoint = JavaFileObjects.forSourceLines(
"WhazzupGreeting", "WhazzupGreeting",
"package test;", "package test;",
"import org.pf4j.Extension;", "import org.pf4j.Extension;",
@ -73,7 +73,7 @@ public class ExtensionAnnotationProcessorTest {
" }", " }",
"}"); "}");
private static final JavaFileObject SpinnakerExtension = JavaFileObjects.forSourceLines( public static final JavaFileObject SpinnakerExtension = JavaFileObjects.forSourceLines(
"SpinnakerExtension", "SpinnakerExtension",
"package test;", "package test;",
"", "",
@ -91,7 +91,7 @@ public class ExtensionAnnotationProcessorTest {
"public @interface SpinnakerExtension {", "public @interface SpinnakerExtension {",
"}"); "}");
private static final JavaFileObject WhazzupGreeting_SpinnakerExtension = JavaFileObjects.forSourceLines( public static final JavaFileObject WhazzupGreeting_SpinnakerExtension = JavaFileObjects.forSourceLines(
"WhazzupGreeting", "WhazzupGreeting",
"package test;", "package test;",
"", "",

Loading…
Cancel
Save