• 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 MyAppPreviousCompilationTest {
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 
MyAppPreviousCompilationTest(boolean disableCrossCompilationRootValidation)43   public MyAppPreviousCompilationTest(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     // This test case should succeed independent of disableCrossCompilationRootValidation.
68     Compilation compilation = compiler().compile(testRoot);
69     assertThat(compilation).succeeded();
70   }
71 
72   @Test
appRootTest()73   public void appRootTest() {
74     JavaFileObject appRoot =
75         JavaFileObjects.forSourceLines(
76             "test.AppRoot",
77             "package test;",
78             "",
79             "import android.app.Application;",
80             "import dagger.hilt.android.HiltAndroidApp;",
81             "",
82             "@HiltAndroidApp(Application.class)",
83             "public class AppRoot extends Hilt_AppRoot {}");
84 
85     Compilation compilation = compiler().compile(appRoot);
86     if (disableCrossCompilationRootValidation) {
87       assertThat(compilation).succeeded();
88     } else {
89       assertThat(compilation).failed();
90       assertThat(compilation).hadErrorCount(1);
91       assertThat(compilation)
92           .hadErrorContaining(
93               "Cannot process app roots in this compilation unit since there are app roots in a "
94                   + "previous compilation unit:"
95                   + "\n  \tApp roots in previous compilation unit: ["
96                   + "dagger.hilt.processor.internal.root.MyAppPreviousCompilation.MyApp]"
97                   + "\n  \tApp roots in this compilation unit: [test.AppRoot]");
98     }
99   }
100 }
101