1 /* 2 * Copyright (C) 2021 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.root; 18 19 import static com.google.testing.compile.CompilationSubject.assertThat; 20 21 import com.google.common.collect.ImmutableCollection; 22 import com.google.common.collect.ImmutableList; 23 import com.google.testing.compile.Compilation; 24 import com.google.testing.compile.Compiler; 25 import com.google.testing.compile.JavaFileObjects; 26 import dagger.hilt.android.testing.compile.HiltCompilerTests; 27 import javax.tools.JavaFileObject; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.Parameterized; 31 import org.junit.runners.Parameterized.Parameters; 32 33 @RunWith(Parameterized.class) 34 public final class RootProcessorErrorsTest { 35 36 @Parameters(name = "{0}") parameters()37 public static ImmutableCollection<Object[]> parameters() { 38 return ImmutableList.copyOf(new Object[][] {{true}, {false}}); 39 } 40 41 private final boolean disableCrossCompilationRootValidation; 42 RootProcessorErrorsTest(boolean disableCrossCompilationRootValidation)43 public RootProcessorErrorsTest(boolean disableCrossCompilationRootValidation) { 44 this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation; 45 } 46 compiler()47 private Compiler compiler() { 48 return HiltCompilerTests.compiler() 49 .withOptions( 50 String.format( 51 "-Adagger.hilt.disableCrossCompilationRootValidation=%s", 52 disableCrossCompilationRootValidation)); 53 } 54 55 @Test multipleAppRootsTest()56 public void multipleAppRootsTest() { 57 JavaFileObject appRoot1 = 58 JavaFileObjects.forSourceLines( 59 "test.AppRoot1", 60 "package test;", 61 "", 62 "import android.app.Application;", 63 "import dagger.hilt.android.HiltAndroidApp;", 64 "", 65 "@HiltAndroidApp(Application.class)", 66 "public class AppRoot1 extends Hilt_AppRoot1 {}"); 67 68 JavaFileObject appRoot2 = 69 JavaFileObjects.forSourceLines( 70 "test.AppRoot2", 71 "package test;", 72 "", 73 "import android.app.Application;", 74 "import dagger.hilt.android.HiltAndroidApp;", 75 "", 76 "@HiltAndroidApp(Application.class)", 77 "public class AppRoot2 extends Hilt_AppRoot2 {}"); 78 79 // This test case should fail independent of disableCrossCompilationRootValidation. 80 Compilation compilation = compiler().compile(appRoot1, appRoot2); 81 assertThat(compilation).failed(); 82 assertThat(compilation).hadErrorCount(1); 83 assertThat(compilation) 84 .hadErrorContaining( 85 "Cannot process multiple app roots in the same compilation unit: " 86 + "test.AppRoot1, test.AppRoot2"); 87 } 88 89 @Test appRootWithTestRootTest()90 public void appRootWithTestRootTest() { 91 JavaFileObject appRoot = 92 JavaFileObjects.forSourceLines( 93 "test.AppRoot", 94 "package test;", 95 "", 96 "import android.app.Application;", 97 "import dagger.hilt.android.HiltAndroidApp;", 98 "", 99 "@HiltAndroidApp(Application.class)", 100 "public class AppRoot extends Hilt_AppRoot {}"); 101 102 JavaFileObject testRoot = 103 JavaFileObjects.forSourceLines( 104 "test.TestRoot", 105 "package test;", 106 "", 107 "import dagger.hilt.android.testing.HiltAndroidTest;", 108 "", 109 "@HiltAndroidTest", 110 "public class TestRoot {}"); 111 112 // This test case should fail independent of disableCrossCompilationRootValidation. 113 Compilation compilation = compiler().compile(appRoot, testRoot); 114 assertThat(compilation).failed(); 115 assertThat(compilation).hadErrorCount(1); 116 assertThat(compilation) 117 .hadErrorContaining( 118 "Cannot process test roots and app roots in the same compilation unit:" 119 + "\n App root in this compilation unit: test.AppRoot" 120 + "\n Test roots in this compilation unit: test.TestRoot"); 121 } 122 } 123