br.com.objectos.code.AnnotationProcessorAssert Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.code;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.processing.Processor;
import javax.tools.JavaFileObject;
import br.com.objectos.io.Resource;
import com.google.common.truth.Truth;
import com.google.testing.compile.JavaFileObjects;
import com.google.testing.compile.JavaSourcesSubjectFactory;
/**
* @author [email protected] (Patricia Nascimento)
*/
public class AnnotationProcessorAssert {
private final List inputList = new ArrayList<>();
private final List outputList = new ArrayList<>();
private final List processorList = new ArrayList<>();
private String dir;
private String pack;
private AnnotationProcessorAssert() {
}
public static AnnotationProcessorAssert using(Processor processor, Processor... rest) {
return new AnnotationProcessorAssert().processor(processor, rest);
}
public AnnotationProcessorAssert dir(String dir) {
this.dir = dir;
return this;
}
public void failsWithMessage(String message) {
Truth.assertAbout(JavaSourcesSubjectFactory.javaSources())
.that(inputList)
.processedWith(processorList)
.failsToCompile()
.withErrorContaining(message);
}
public AnnotationProcessorAssert input(String... paths) {
for (String path : paths) {
inputSingle(path);
}
return this;
}
public AnnotationProcessorAssert output(String... paths) {
for (String path : paths) {
String qname = pack + "." + path;
String source = contentsOf("/" + dir + "/" + path + ".java");
JavaFileObject object = JavaFileObjects.forSourceString(qname, source);
outputList.add(object);
}
return this;
}
public AnnotationProcessorAssert pack(String pack) {
this.pack = pack;
return this;
}
public void verify() {
JavaFileObject first = outputList.get(0);
JavaFileObject[] rest = outputList.subList(1, outputList.size()).toArray(new JavaFileObject[] {});
Truth.assertAbout(JavaSourcesSubjectFactory.javaSources())
.that(inputList)
.processedWith(processorList)
.compilesWithoutError()
.and().generatesSources(first, rest);
}
private String contentsOf(String resourceName) {
try {
return Resource.of(resourceName).readToString();
} catch (IOException e) {
return "";
}
}
private void inputSingle(String path) {
String name = dir + "/" + path + ".java";
JavaFileObject object = JavaFileObjects.forResource(name);
inputList.add(object);
}
private AnnotationProcessorAssert processor(Processor processor, Processor... rest) {
processorList.add(processor);
for (Processor oneOfTheRest : rest) {
processorList.add(oneOfTheRest);
}
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy