Add support for annotation processors with TestCompiler
Closes gh-28582
This commit is contained in:
committed by
Phillip Webb
parent
4128a71657
commit
45ef21f900
@@ -16,8 +16,17 @@
|
||||
|
||||
package org.springframework.aot.test.generator.compile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
import javax.annotation.processing.Processor;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
||||
import com.example.PublicInterface;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -33,9 +42,9 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link TestCompiler}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class TestCompilerTests {
|
||||
|
||||
@@ -140,6 +149,29 @@ class TestCompilerTests {
|
||||
this::assertHasResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withProcessorsArrayAddsProcessors() {
|
||||
SourceFile sourceFile = SourceFile.of(HELLO_WORLD);
|
||||
TestProcessor processor = new TestProcessor();
|
||||
TestCompiler.forSystem().withSources(sourceFile).withProcessors(processor).compile((compiled -> {
|
||||
assertThat(processor.getProcessedAnnotations()).isNotEmpty();
|
||||
assertThat(processor.getProcessedAnnotations()).satisfiesExactly(element ->
|
||||
assertThat(element.getQualifiedName().toString()).isEqualTo("java.lang.Deprecated"));
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void withProcessorsAddsProcessors() {
|
||||
SourceFile sourceFile = SourceFile.of(HELLO_WORLD);
|
||||
TestProcessor processor = new TestProcessor();
|
||||
List<Processor> processors = List.of(processor);
|
||||
TestCompiler.forSystem().withSources(sourceFile).withProcessors(processors).compile((compiled -> {
|
||||
assertThat(processor.getProcessedAnnotations()).isNotEmpty();
|
||||
assertThat(processor.getProcessedAnnotations()).satisfiesExactly(element ->
|
||||
assertThat(element.getQualifiedName().toString()).isEqualTo("java.lang.Deprecated"));
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void compileWithWritableContent() {
|
||||
WritableContent content = appendable -> appendable.append(HELLO_WORLD);
|
||||
@@ -217,4 +249,20 @@ class TestCompilerTests {
|
||||
"META-INF/myfile")).hasContent("test");
|
||||
}
|
||||
|
||||
@SupportedAnnotationTypes("java.lang.Deprecated")
|
||||
static class TestProcessor extends AbstractProcessor {
|
||||
|
||||
private final List<TypeElement> processedAnnotations = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
this.processedAnnotations.addAll(annotations);
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<TypeElement> getProcessedAnnotations() {
|
||||
return this.processedAnnotations;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user