1 /* 2 * Copyright (C) 2020 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.hilt.processor.internal.originatingelement; 18 19 import static com.google.testing.compile.CompilationSubject.assertThat; 20 import static dagger.hilt.android.testing.compile.HiltCompilerTests.compiler; 21 22 import com.google.testing.compile.Compilation; 23 import com.google.testing.compile.JavaFileObjects; 24 import javax.tools.JavaFileObject; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 @RunWith(JUnit4.class) 30 public class OriginatingElementProcessorTest { 31 32 @Test originatingElementOnInnerClass_fails()33 public void originatingElementOnInnerClass_fails() { 34 JavaFileObject outer1 = 35 JavaFileObjects.forSourceLines( 36 "test.Outer1", 37 "package test;", 38 "", 39 "class Outer1 {}"); 40 JavaFileObject outer2 = 41 JavaFileObjects.forSourceLines( 42 "test.Outer2", 43 "package test;", 44 "", 45 "import dagger.hilt.codegen.OriginatingElement;", 46 "", 47 "class Outer2 {", 48 " @OriginatingElement(topLevelClass = Outer1.class)", 49 " static class Inner {}", 50 "}"); 51 Compilation compilation = compiler().compile(outer1, outer2); 52 assertThat(compilation).failed(); 53 assertThat(compilation) 54 .hadErrorContaining( 55 "@OriginatingElement should only be used to annotate top-level types, but found: " 56 + "test.Outer2.Inner"); 57 } 58 59 @Test originatingElementValueWithInnerClass_fails()60 public void originatingElementValueWithInnerClass_fails() { 61 JavaFileObject outer1 = 62 JavaFileObjects.forSourceLines( 63 "test.Outer1", 64 "package test;", 65 "", 66 "class Outer1 {", 67 " static class Inner {}", 68 "}"); 69 JavaFileObject outer2 = 70 JavaFileObjects.forSourceLines( 71 "test.Outer2", 72 "package test;", 73 "", 74 "import dagger.hilt.codegen.OriginatingElement;", 75 "", 76 "@OriginatingElement(topLevelClass = Outer1.Inner.class)", 77 "class Outer2 {}"); 78 Compilation compilation = compiler().compile(outer1, outer2); 79 assertThat(compilation).failed(); 80 assertThat(compilation) 81 .hadErrorContaining( 82 "OriginatingElement.topLevelClass value should be a top-level class, but found: " 83 + "test.Outer1.Inner"); 84 } 85 } 86