1 /* 2 * Copyright (C) 2015 The Dagger Authors. 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 17 package dagger.internal.codegen; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.testing.compile.CompilationSubject.assertThat; 21 import static com.google.testing.compile.Compiler.javac; 22 23 import com.google.common.collect.ImmutableSet; 24 import com.google.testing.compile.Compilation; 25 import com.google.testing.compile.JavaFileObjects; 26 import dagger.internal.codegen.validation.ValidationReport; 27 import dagger.internal.codegen.validation.ValidationReport.Builder; 28 import java.util.Set; 29 import javax.annotation.processing.AbstractProcessor; 30 import javax.annotation.processing.RoundEnvironment; 31 import javax.lang.model.element.TypeElement; 32 import javax.tools.JavaFileObject; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.junit.runners.JUnit4; 36 37 @RunWith(JUnit4.class) 38 public class ValidationReportTest { 39 private static final JavaFileObject TEST_CLASS_FILE = 40 JavaFileObjects.forSourceLines("test.TestClass", 41 "package test;", 42 "", 43 "final class TestClass {}"); 44 45 @Test basicReport()46 public void basicReport() { 47 Compilation compilation = 48 javac() 49 .withProcessors( 50 new SimpleTestProcessor() { 51 @Override 52 void test() { 53 Builder<TypeElement> reportBuilder = 54 ValidationReport.about(getTypeElement("test.TestClass")); 55 reportBuilder.addError("simple error"); 56 reportBuilder.build().printMessagesTo(processingEnv.getMessager()); 57 } 58 }) 59 .compile(TEST_CLASS_FILE); 60 assertThat(compilation).failed(); 61 assertThat(compilation).hadErrorContaining("simple error").inFile(TEST_CLASS_FILE).onLine(3); 62 } 63 64 @Test messageOnDifferentElement()65 public void messageOnDifferentElement() { 66 Compilation compilation = 67 javac() 68 .withProcessors( 69 new SimpleTestProcessor() { 70 @Override 71 void test() { 72 Builder<TypeElement> reportBuilder = 73 ValidationReport.about(getTypeElement("test.TestClass")); 74 reportBuilder.addError("simple error", getTypeElement(String.class)); 75 reportBuilder.build().printMessagesTo(processingEnv.getMessager()); 76 } 77 }) 78 .compile(TEST_CLASS_FILE); 79 assertThat(compilation).failed(); 80 assertThat(compilation) 81 .hadErrorContaining("[java.lang.String] simple error") 82 .inFile(TEST_CLASS_FILE) 83 .onLine(3); 84 } 85 86 @Test subreport()87 public void subreport() { 88 Compilation compilation = 89 javac() 90 .withProcessors( 91 new SimpleTestProcessor() { 92 @Override 93 void test() { 94 Builder<TypeElement> reportBuilder = 95 ValidationReport.about(getTypeElement("test.TestClass")); 96 reportBuilder.addError("simple error"); 97 ValidationReport<TypeElement> parentReport = 98 ValidationReport.about(getTypeElement(String.class)) 99 .addSubreport(reportBuilder.build()) 100 .build(); 101 assertThat(parentReport.isClean()).isFalse(); 102 parentReport.printMessagesTo(processingEnv.getMessager()); 103 } 104 }) 105 .compile(TEST_CLASS_FILE); 106 assertThat(compilation).failed(); 107 assertThat(compilation).hadErrorContaining("simple error").inFile(TEST_CLASS_FILE).onLine(3); 108 } 109 110 private static abstract class SimpleTestProcessor extends AbstractProcessor { 111 @Override getSupportedAnnotationTypes()112 public Set<String> getSupportedAnnotationTypes() { 113 return ImmutableSet.of("*"); 114 } 115 116 @Override process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)117 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 118 test(); 119 return false; 120 } 121 getTypeElement(Class<?> clazz)122 protected final TypeElement getTypeElement(Class<?> clazz) { 123 return getTypeElement(clazz.getCanonicalName()); 124 } 125 getTypeElement(String canonicalName)126 protected final TypeElement getTypeElement(String canonicalName) { 127 return processingEnv.getElementUtils().getTypeElement(canonicalName); 128 } 129 test()130 abstract void test(); 131 } 132 } 133