diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java index 564c644ae7..401cad6645 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java @@ -18,15 +18,9 @@ package org.springframework.boot.configurationprocessor; import java.io.IOException; import java.time.Duration; -import java.util.Set; import java.util.function.BiConsumer; -import javax.annotation.processing.AbstractProcessor; -import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; -import javax.annotation.processing.SupportedAnnotationTypes; -import javax.annotation.processing.SupportedSourceVersion; -import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; @@ -35,6 +29,7 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.springframework.boot.configurationprocessor.TypeUtils.TypeDescriptor; +import org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor; import org.springframework.boot.configurationsample.generic.AbstractGenericProperties; import org.springframework.boot.configurationsample.generic.AbstractIntermediateGenericProperties; import org.springframework.boot.configurationsample.generic.SimpleGenericProperties; @@ -102,35 +97,10 @@ public class TypeUtilsTests { private void process(Class target, BiConsumer consumer) throws IOException { - TestProcessor processor = new TestProcessor(consumer); + TestableAnnotationProcessor processor = new TestableAnnotationProcessor<>( + consumer, TypeUtils::new); TestCompiler compiler = new TestCompiler(this.temporaryFolder); compiler.getTask(target).call(processor); } - @SupportedAnnotationTypes("*") - @SupportedSourceVersion(SourceVersion.RELEASE_8) - private final class TestProcessor extends AbstractProcessor { - - private final BiConsumer typeUtilsConsumer; - - private TypeUtils typeUtils; - - private TestProcessor(BiConsumer typeUtils) { - this.typeUtilsConsumer = typeUtils; - } - - @Override - public synchronized void init(ProcessingEnvironment env) { - this.typeUtils = new TypeUtils(env); - } - - @Override - public boolean process(Set annotations, - RoundEnvironment roundEnv) { - this.typeUtilsConsumer.accept(roundEnv, this.typeUtils); - return false; - } - - } - } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestableAnnotationProcessor.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestableAnnotationProcessor.java new file mode 100644 index 0000000000..4d40b6bc59 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestableAnnotationProcessor.java @@ -0,0 +1,66 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.configurationprocessor.test; + +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; + +/** + * A testable {@link Processor}. + * + * @param the type of element to help writing assertions + * @author Stephane Nicoll + */ +@SupportedAnnotationTypes("*") +@SupportedSourceVersion(SourceVersion.RELEASE_8) +public class TestableAnnotationProcessor extends AbstractProcessor { + + private final BiConsumer consumer; + + private final Function factory; + + private T target; + + public TestableAnnotationProcessor(BiConsumer consumer, + Function factory) { + this.consumer = consumer; + this.factory = factory; + } + + @Override + public synchronized void init(ProcessingEnvironment env) { + this.target = this.factory.apply(env); + } + + @Override + public boolean process(Set annotations, + RoundEnvironment roundEnv) { + this.consumer.accept(roundEnv, this.target); + return false; + } + +}