Home | History | Annotate | Download | only in codegen
      1 /*
      2  * Copyright (C) 2015 Google, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package dagger.internal.codegen;
     17 
     18 import com.google.common.collect.ImmutableSet;
     19 import com.google.testing.compile.JavaFileObjects;
     20 import dagger.internal.codegen.ValidationReport.Builder;
     21 import java.util.Set;
     22 import javax.annotation.processing.AbstractProcessor;
     23 import javax.annotation.processing.RoundEnvironment;
     24 import javax.lang.model.element.TypeElement;
     25 import javax.tools.JavaFileObject;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 import org.junit.runners.JUnit4;
     29 
     30 import static com.google.common.truth.Truth.assertAbout;
     31 import static com.google.common.truth.Truth.assertThat;
     32 import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
     33 
     34 @RunWith(JUnit4.class)
     35 public class ValidationReportTest {
     36   private static final JavaFileObject TEST_CLASS_FILE =
     37       JavaFileObjects.forSourceLines("test.TestClass",
     38           "package test;",
     39           "",
     40           "final class TestClass {}");
     41 
     42   @Test
     43   public void basicReport() {
     44     assertAbout(javaSource())
     45         .that(TEST_CLASS_FILE)
     46         .processedWith(
     47             new SimpleTestProcessor() {
     48               @Override
     49               void test() {
     50                 Builder<TypeElement> reportBuilder =
     51                     ValidationReport.about(getTypeElement("test.TestClass"));
     52                 reportBuilder.addError("simple error");
     53                 reportBuilder.build().printMessagesTo(processingEnv.getMessager());
     54               }
     55             })
     56         .failsToCompile()
     57         .withErrorContaining("simple error")
     58         .in(TEST_CLASS_FILE)
     59         .onLine(3);
     60   }
     61 
     62   @Test
     63   public void messageOnDifferentElement() {
     64     assertAbout(javaSource())
     65         .that(TEST_CLASS_FILE)
     66         .processedWith(
     67             new SimpleTestProcessor() {
     68               @Override
     69               void test() {
     70                 Builder<TypeElement> reportBuilder =
     71                     ValidationReport.about(getTypeElement("test.TestClass"));
     72                 reportBuilder.addError("simple error", getTypeElement(String.class));
     73                 reportBuilder.build().printMessagesTo(processingEnv.getMessager());
     74               }
     75             })
     76         .failsToCompile()
     77         .withErrorContaining("[java.lang.String] simple error")
     78         .in(TEST_CLASS_FILE)
     79         .onLine(3);
     80   }
     81 
     82   @Test
     83   public void subreport() {
     84     assertAbout(javaSource())
     85         .that(TEST_CLASS_FILE)
     86         .processedWith(
     87             new SimpleTestProcessor() {
     88               @Override
     89               void test() {
     90                 Builder<TypeElement> reportBuilder =
     91                     ValidationReport.about(getTypeElement("test.TestClass"));
     92                 reportBuilder.addError("simple error");
     93                 ValidationReport<TypeElement> parentReport =
     94                     ValidationReport.about(getTypeElement(String.class))
     95                         .addSubreport(reportBuilder.build())
     96                         .build();
     97                 assertThat(parentReport.isClean()).isFalse();
     98                 parentReport.printMessagesTo(processingEnv.getMessager());
     99               }
    100             })
    101         .failsToCompile()
    102         .withErrorContaining("simple error")
    103         .in(TEST_CLASS_FILE)
    104         .onLine(3);
    105   }
    106 
    107   private static abstract class SimpleTestProcessor extends AbstractProcessor {
    108     @Override
    109     public Set<String> getSupportedAnnotationTypes() {
    110       return ImmutableSet.of("*");
    111     }
    112 
    113     @Override
    114     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    115       test();
    116       return false;
    117     }
    118 
    119     protected final TypeElement getTypeElement(Class<?> clazz) {
    120       return getTypeElement(clazz.getCanonicalName());
    121     }
    122 
    123     protected final TypeElement getTypeElement(String canonicalName) {
    124       return processingEnv.getElementUtils().getTypeElement(canonicalName);
    125     }
    126 
    127     abstract void test();
    128   }
    129 }
    130