• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.processor.AndroidCompilers;
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 MyTestPreviousCompilationTest {
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 
MyTestPreviousCompilationTest(boolean disableCrossCompilationRootValidation)43   public MyTestPreviousCompilationTest(boolean disableCrossCompilationRootValidation) {
44     this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation;
45   }
46 
compiler()47   private Compiler compiler() {
48     return AndroidCompilers.compiler()
49         .withOptions(
50             String.format(
51                 "-Adagger.hilt.disableCrossCompilationRootValidation=%s",
52                 disableCrossCompilationRootValidation));
53   }
54 
55   @Test
testRootTest()56   public void testRootTest() {
57     JavaFileObject testRoot =
58         JavaFileObjects.forSourceLines(
59             "test.TestRoot",
60             "package test;",
61             "",
62             "import dagger.hilt.android.testing.HiltAndroidTest;",
63             "",
64             "@HiltAndroidTest",
65             "public class TestRoot {}");
66 
67     Compilation compilation = compiler().compile(testRoot);
68     if (disableCrossCompilationRootValidation) {
69       assertThat(compilation).succeeded();
70     } else {
71       assertThat(compilation).failed();
72       assertThat(compilation).hadErrorCount(1);
73       assertThat(compilation)
74           .hadErrorContaining(
75               "Cannot process new roots when there are test roots from a previous compilation unit:"
76                   + "\n  \tTest roots from previous compilation unit: "
77                   + "[dagger.hilt.processor.internal.root.MyTestPreviousCompilation.MyTest]"
78                   + "\n  \tAll roots from this compilation unit: [test.TestRoot]");
79     }
80   }
81 
82   @Test
appRootTest()83   public void appRootTest() {
84     JavaFileObject appRoot =
85         JavaFileObjects.forSourceLines(
86             "test.AppRoot",
87             "package test;",
88             "",
89             "import android.app.Application;",
90             "import dagger.hilt.android.HiltAndroidApp;",
91             "",
92             "@HiltAndroidApp(Application.class)",
93             "public class AppRoot extends Hilt_AppRoot {}");
94 
95     Compilation compilation = compiler().compile(appRoot);
96     if (disableCrossCompilationRootValidation) {
97       assertThat(compilation).succeeded();
98     } else {
99       assertThat(compilation).failed();
100       assertThat(compilation).hadErrorCount(1);
101       assertThat(compilation)
102           .hadErrorContaining(
103               "Cannot process new roots when there are test roots from a previous compilation unit:"
104                   + "\n  \tTest roots from previous compilation unit: "
105                   + "[dagger.hilt.processor.internal.root.MyTestPreviousCompilation.MyTest]"
106                   + "\n  \tAll roots from this compilation unit: [test.AppRoot]");
107     }
108   }
109 }
110