All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.testing.AnnotationProcessorAssert Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show 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.testing;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.Processor;
import javax.tools.JavaFileObject;

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 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 = MoreAsserts.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.ASSERT
        .about(JavaSourcesSubjectFactory.javaSources())
        .that(inputList)
        .processedWith(processorList)
        .compilesWithoutError()
        .and().generatesSources(first, rest);
  }

  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 - 2024 Weber Informatics LLC | Privacy Policy