aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/hilt/processor/internal/root
diff options
context:
space:
mode:
Diffstat (limited to 'javatests/dagger/hilt/processor/internal/root')
-rw-r--r--javatests/dagger/hilt/processor/internal/root/BUILD127
-rw-r--r--javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilation.java29
-rw-r--r--javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilationTest.java100
-rw-r--r--javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilation.java28
-rw-r--r--javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilationTest.java109
-rw-r--r--javatests/dagger/hilt/processor/internal/root/RootFileFormatterTest.java173
-rw-r--r--javatests/dagger/hilt/processor/internal/root/RootProcessorErrorsTest.java122
7 files changed, 0 insertions, 688 deletions
diff --git a/javatests/dagger/hilt/processor/internal/root/BUILD b/javatests/dagger/hilt/processor/internal/root/BUILD
deleted file mode 100644
index 0dba875c7..000000000
--- a/javatests/dagger/hilt/processor/internal/root/BUILD
+++ /dev/null
@@ -1,127 +0,0 @@
-# Copyright (C) 2020 The Dagger Authors.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-# Description:
-# Tests for internal code for implementing Hilt processors.
-
-load("//java/dagger/testing/compile:macros.bzl", "compiler_test")
-
-package(default_visibility = ["//:src"])
-
-android_library(
- name = "MyAppPreviousCompilation",
- srcs = ["MyAppPreviousCompilation.java"],
- deps = [
- "//java/dagger/hilt/android:hilt_android_app",
- ],
-)
-
-compiler_test(
- name = "MyAppPreviousCompilationTest",
- srcs = ["MyAppPreviousCompilationTest.java"],
- compiler_deps = [
- ":MyAppPreviousCompilation",
- "//java/dagger/hilt/android:hilt_android_app",
- "//java/dagger/hilt/android/testing:hilt_android_test",
- "@androidsdk//:platforms/android-30/android.jar",
- "@maven//:androidx_annotation_annotation",
- "@maven//:org_robolectric_robolectric",
- "@maven//:androidx_test_ext_junit",
- "@maven//:androidx_test_core",
- ],
- deps = [
- "//java/dagger/internal/guava:collect",
- "//javatests/dagger/hilt/android/processor:android_compilers",
- "@google_bazel_common//third_party/java/compile_testing",
- "@google_bazel_common//third_party/java/junit",
- "@google_bazel_common//third_party/java/truth",
- ],
-)
-
-android_library(
- name = "MyTestPreviousCompilation",
- srcs = ["MyTestPreviousCompilation.java"],
- deps = [
- "//:android_local_test_exports",
- "//java/dagger/hilt/android/testing:hilt_android_test",
- ],
-)
-
-compiler_test(
- name = "MyTestPreviousCompilationTest",
- srcs = ["MyTestPreviousCompilationTest.java"],
- compiler_deps = [
- ":MyTestPreviousCompilation",
- "//java/dagger/hilt/android:hilt_android_app",
- "//java/dagger/hilt/android/testing:hilt_android_test",
- "@androidsdk//:platforms/android-30/android.jar",
- "@maven//:androidx_annotation_annotation",
- "@maven//:org_robolectric_robolectric",
- "@maven//:androidx_test_ext_junit",
- "@maven//:androidx_test_core",
- ],
- deps = [
- "//java/dagger/internal/guava:collect",
- "//javatests/dagger/hilt/android/processor:android_compilers",
- "@google_bazel_common//third_party/java/compile_testing",
- "@google_bazel_common//third_party/java/junit",
- "@google_bazel_common//third_party/java/truth",
- ],
-)
-
-compiler_test(
- name = "RootProcessorErrorsTest",
- srcs = ["RootProcessorErrorsTest.java"],
- compiler_deps = [
- "//java/dagger/hilt/android:hilt_android_app",
- "//java/dagger/hilt/android/testing:hilt_android_test",
- "@androidsdk//:platforms/android-30/android.jar",
- "@maven//:androidx_annotation_annotation",
- "@maven//:org_robolectric_robolectric",
- "@maven//:androidx_test_ext_junit",
- "@maven//:androidx_test_core",
- ],
- deps = [
- "//java/dagger/internal/guava:collect",
- "//javatests/dagger/hilt/android/processor:android_compilers",
- "@google_bazel_common//third_party/java/compile_testing",
- "@google_bazel_common//third_party/java/junit",
- "@google_bazel_common//third_party/java/truth",
- ],
-)
-
-compiler_test(
- name = "RootFileFormatterTest",
- srcs = ["RootFileFormatterTest.java"],
- compiler_deps = [
- "//java/dagger/hilt/android:hilt_android_app",
- "//java/dagger/hilt/android/testing:hilt_android_test",
- "@androidsdk//:platforms/android-30/android.jar",
- "@maven//:androidx_annotation_annotation",
- "@maven//:org_robolectric_robolectric",
- "@maven//:androidx_test_ext_junit",
- "@maven//:androidx_test_core",
- ],
- deps = [
- "//java/dagger/internal/guava:base",
- "//javatests/dagger/hilt/android/processor:android_compilers",
- "@google_bazel_common//third_party/java/compile_testing",
- "@google_bazel_common//third_party/java/junit",
- "@google_bazel_common//third_party/java/truth",
- ],
-)
-
-filegroup(
- name = "srcs_filegroup",
- srcs = glob(["*"]),
-)
diff --git a/javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilation.java b/javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilation.java
deleted file mode 100644
index 5830803b4..000000000
--- a/javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilation.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (C) 2021 The Dagger Authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package dagger.hilt.processor.internal.root;
-
-import android.app.Application;
-import dagger.hilt.android.HiltAndroidApp;
-
-/** Defines a {@link HiltAndroidApp} for {@link MyAppPreviousCompilationTest}. */
-public final class MyAppPreviousCompilation {
-
- @HiltAndroidApp(Application.class)
- public static final class MyApp extends Hilt_MyAppPreviousCompilation_MyApp {}
-
- private MyAppPreviousCompilation() {}
-}
diff --git a/javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilationTest.java b/javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilationTest.java
deleted file mode 100644
index adf6d9b2d..000000000
--- a/javatests/dagger/hilt/processor/internal/root/MyAppPreviousCompilationTest.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright (C) 2021 The Dagger Authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package dagger.hilt.processor.internal.root;
-
-import static com.google.testing.compile.CompilationSubject.assertThat;
-
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableList;
-import com.google.testing.compile.Compilation;
-import com.google.testing.compile.Compiler;
-import com.google.testing.compile.JavaFileObjects;
-import dagger.hilt.android.processor.AndroidCompilers;
-import javax.tools.JavaFileObject;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-@RunWith(Parameterized.class)
-public final class MyAppPreviousCompilationTest {
-
- @Parameters(name = "{0}")
- public static ImmutableCollection<Object[]> parameters() {
- return ImmutableList.copyOf(new Object[][] {{true}, {false}});
- }
-
- private final boolean disableCrossCompilationRootValidation;
-
- public MyAppPreviousCompilationTest(boolean disableCrossCompilationRootValidation) {
- this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation;
- }
-
- private Compiler compiler() {
- return AndroidCompilers.compiler()
- .withOptions(
- String.format(
- "-Adagger.hilt.disableCrossCompilationRootValidation=%s",
- disableCrossCompilationRootValidation));
- }
-
- @Test
- public void testRootTest() {
- JavaFileObject testRoot =
- JavaFileObjects.forSourceLines(
- "test.TestRoot",
- "package test;",
- "",
- "import dagger.hilt.android.testing.HiltAndroidTest;",
- "",
- "@HiltAndroidTest",
- "public class TestRoot {}");
-
- // This test case should succeed independent of disableCrossCompilationRootValidation.
- Compilation compilation = compiler().compile(testRoot);
- assertThat(compilation).succeeded();
- }
-
- @Test
- public void appRootTest() {
- JavaFileObject appRoot =
- JavaFileObjects.forSourceLines(
- "test.AppRoot",
- "package test;",
- "",
- "import android.app.Application;",
- "import dagger.hilt.android.HiltAndroidApp;",
- "",
- "@HiltAndroidApp(Application.class)",
- "public class AppRoot extends Hilt_AppRoot {}");
-
- Compilation compilation = compiler().compile(appRoot);
- if (disableCrossCompilationRootValidation) {
- assertThat(compilation).succeeded();
- } else {
- assertThat(compilation).failed();
- assertThat(compilation).hadErrorCount(1);
- assertThat(compilation)
- .hadErrorContaining(
- "Cannot process app roots in this compilation unit since there are app roots in a "
- + "previous compilation unit:"
- + "\n \tApp roots in previous compilation unit: ["
- + "dagger.hilt.processor.internal.root.MyAppPreviousCompilation.MyApp]"
- + "\n \tApp roots in this compilation unit: [test.AppRoot]");
- }
- }
-}
diff --git a/javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilation.java b/javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilation.java
deleted file mode 100644
index f704df5f5..000000000
--- a/javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilation.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (C) 2021 The Dagger Authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package dagger.hilt.processor.internal.root;
-
-import dagger.hilt.android.testing.HiltAndroidTest;
-
-/** Defines a {@link HiltAndroidTest} for {@link MyTestPreviousCompilationTest}. */
-public final class MyTestPreviousCompilation {
-
- @HiltAndroidTest
- public static final class MyTest {}
-
- private MyTestPreviousCompilation() {}
-}
diff --git a/javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilationTest.java b/javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilationTest.java
deleted file mode 100644
index 9e9fae57e..000000000
--- a/javatests/dagger/hilt/processor/internal/root/MyTestPreviousCompilationTest.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (C) 2021 The Dagger Authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package dagger.hilt.processor.internal.root;
-
-import static com.google.testing.compile.CompilationSubject.assertThat;
-
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableList;
-import com.google.testing.compile.Compilation;
-import com.google.testing.compile.Compiler;
-import com.google.testing.compile.JavaFileObjects;
-import dagger.hilt.android.processor.AndroidCompilers;
-import javax.tools.JavaFileObject;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-@RunWith(Parameterized.class)
-public final class MyTestPreviousCompilationTest {
-
- @Parameters(name = "{0}")
- public static ImmutableCollection<Object[]> parameters() {
- return ImmutableList.copyOf(new Object[][] {{true}, {false}});
- }
-
- private final boolean disableCrossCompilationRootValidation;
-
- public MyTestPreviousCompilationTest(boolean disableCrossCompilationRootValidation) {
- this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation;
- }
-
- private Compiler compiler() {
- return AndroidCompilers.compiler()
- .withOptions(
- String.format(
- "-Adagger.hilt.disableCrossCompilationRootValidation=%s",
- disableCrossCompilationRootValidation));
- }
-
- @Test
- public void testRootTest() {
- JavaFileObject testRoot =
- JavaFileObjects.forSourceLines(
- "test.TestRoot",
- "package test;",
- "",
- "import dagger.hilt.android.testing.HiltAndroidTest;",
- "",
- "@HiltAndroidTest",
- "public class TestRoot {}");
-
- Compilation compilation = compiler().compile(testRoot);
- if (disableCrossCompilationRootValidation) {
- assertThat(compilation).succeeded();
- } else {
- assertThat(compilation).failed();
- assertThat(compilation).hadErrorCount(1);
- assertThat(compilation)
- .hadErrorContaining(
- "Cannot process new roots when there are test roots from a previous compilation unit:"
- + "\n \tTest roots from previous compilation unit: "
- + "[dagger.hilt.processor.internal.root.MyTestPreviousCompilation.MyTest]"
- + "\n \tAll roots from this compilation unit: [test.TestRoot]");
- }
- }
-
- @Test
- public void appRootTest() {
- JavaFileObject appRoot =
- JavaFileObjects.forSourceLines(
- "test.AppRoot",
- "package test;",
- "",
- "import android.app.Application;",
- "import dagger.hilt.android.HiltAndroidApp;",
- "",
- "@HiltAndroidApp(Application.class)",
- "public class AppRoot extends Hilt_AppRoot {}");
-
- Compilation compilation = compiler().compile(appRoot);
- if (disableCrossCompilationRootValidation) {
- assertThat(compilation).succeeded();
- } else {
- assertThat(compilation).failed();
- assertThat(compilation).hadErrorCount(1);
- assertThat(compilation)
- .hadErrorContaining(
- "Cannot process new roots when there are test roots from a previous compilation unit:"
- + "\n \tTest roots from previous compilation unit: "
- + "[dagger.hilt.processor.internal.root.MyTestPreviousCompilation.MyTest]"
- + "\n \tAll roots from this compilation unit: [test.AppRoot]");
- }
- }
-}
diff --git a/javatests/dagger/hilt/processor/internal/root/RootFileFormatterTest.java b/javatests/dagger/hilt/processor/internal/root/RootFileFormatterTest.java
deleted file mode 100644
index 6d598c83f..000000000
--- a/javatests/dagger/hilt/processor/internal/root/RootFileFormatterTest.java
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright (C) 2020 The Dagger Authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package dagger.hilt.processor.internal.root;
-
-import static com.google.testing.compile.CompilationSubject.assertThat;
-import static dagger.hilt.android.processor.AndroidCompilers.compiler;
-
-import com.google.common.base.Joiner;
-import com.google.testing.compile.Compilation;
-import com.google.testing.compile.JavaFileObjects;
-import javax.tools.JavaFileObject;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-// This test makes sure we don't regress the formatting in the components file.
-@RunWith(JUnit4.class)
-public final class RootFileFormatterTest {
- private static final Joiner JOINER = Joiner.on("\n");
-
- @Test
- public void testProdComponents() {
- Compilation compilation =
- compiler()
- .compile(
- JavaFileObjects.forSourceLines(
- "test.TestApplication",
- "package test;",
- "",
- "import android.app.Application;",
- "import dagger.hilt.android.HiltAndroidApp;",
- "",
- "@HiltAndroidApp(Application.class)",
- "public class TestApplication extends Hilt_TestApplication {}"),
- entryPoint("SingletonComponent", "EntryPoint1"),
- entryPoint("SingletonComponent", "EntryPoint2"),
- entryPoint("ActivityComponent", "EntryPoint3"),
- entryPoint("ActivityComponent", "EntryPoint4"));
- assertThat(compilation).succeeded();
- assertThat(compilation)
- .generatedSourceFile("test/TestApplication_HiltComponents")
- .contentsAsUtf8String()
- .contains(
- JOINER.join(
- " public abstract static class SingletonC implements"
- + " HiltWrapper_ActivityRetainedComponentManager"
- + "_ActivityRetainedComponentBuilderEntryPoint,",
- " ServiceComponentManager.ServiceComponentBuilderEntryPoint,",
- " SingletonComponent,",
- " GeneratedComponent,",
- " EntryPoint1,",
- " EntryPoint2,",
- " TestApplication_GeneratedInjector {"));
-
- assertThat(compilation)
- .generatedSourceFile("test/TestApplication_HiltComponents")
- .contentsAsUtf8String()
- .contains(
- JOINER.join(
- " public abstract static class ActivityC implements ActivityComponent,",
- " FragmentComponentManager.FragmentComponentBuilderEntryPoint,",
- " ViewComponentManager.ViewComponentBuilderEntryPoint,",
- " GeneratedComponent,",
- " EntryPoint3,",
- " EntryPoint4 {"));
- }
-
- @Test
- public void testTestComponents() {
- Compilation compilation =
- compiler()
- .compile(
- JavaFileObjects.forSourceLines(
- "test.MyTest",
- "package test;",
- "",
- "import dagger.hilt.android.testing.HiltAndroidTest;",
- "",
- "@HiltAndroidTest",
- "public class MyTest {}"),
- entryPoint("SingletonComponent", "EntryPoint1"),
- entryPoint("SingletonComponent", "EntryPoint2"),
- entryPoint("ActivityComponent", "EntryPoint3"),
- entryPoint("ActivityComponent", "EntryPoint4"));
- assertThat(compilation).succeeded();
- assertThat(compilation)
- .generatedSourceFile("test/MyTest_HiltComponents")
- .contentsAsUtf8String()
- .contains(
- JOINER.join(
- " public abstract static class SingletonC implements"
- + " HiltWrapper_ActivityRetainedComponentManager"
- + "_ActivityRetainedComponentBuilderEntryPoint,",
- " ServiceComponentManager.ServiceComponentBuilderEntryPoint,",
- " SingletonComponent,",
- " TestSingletonComponent,",
- " EntryPoint1,",
- " EntryPoint2,",
- " MyTest_GeneratedInjector {"));
-
- assertThat(compilation)
- .generatedSourceFile("test/MyTest_HiltComponents")
- .contentsAsUtf8String()
- .contains(
- JOINER.join(
- " public abstract static class ActivityC implements ActivityComponent,",
- " FragmentComponentManager.FragmentComponentBuilderEntryPoint,",
- " ViewComponentManager.ViewComponentBuilderEntryPoint,",
- " GeneratedComponent,",
- " EntryPoint3,",
- " EntryPoint4 {"));
- }
-
- @Test
- public void testSharedTestComponents() {
- Compilation compilation =
- compiler()
- .withOptions("-Adagger.hilt.shareTestComponents=true")
- .compile(
- JavaFileObjects.forSourceLines(
- "test.MyTest",
- "package test;",
- "",
- "import dagger.hilt.android.testing.HiltAndroidTest;",
- "",
- "@HiltAndroidTest",
- "public class MyTest {}"),
- entryPoint("SingletonComponent", "EntryPoint1"));
- assertThat(compilation).succeeded();
- assertThat(compilation)
- .generatedSourceFile("dagger/hilt/android/internal/testing/root/Default_HiltComponents")
- .contentsAsUtf8String()
- .contains(
- JOINER.join(
- " public abstract static class SingletonC implements"
- + " HiltWrapper_ActivityRetainedComponentManager"
- + "_ActivityRetainedComponentBuilderEntryPoint,",
- " ServiceComponentManager.ServiceComponentBuilderEntryPoint,",
- " SingletonComponent,",
- " TestSingletonComponent,",
- " EntryPoint1,",
- " MyTest_GeneratedInjector {"));
- }
-
- private static JavaFileObject entryPoint(String component, String name) {
- return JavaFileObjects.forSourceLines(
- "test." + name,
- "package test;",
- "",
- "import dagger.hilt.EntryPoint;",
- "import dagger.hilt.InstallIn;",
- component.equals("SingletonComponent") ? "import dagger.hilt.components.SingletonComponent;"
- : "import dagger.hilt.android.components." + component + ";",
- "",
- "@EntryPoint",
- "@InstallIn(" + component + ".class)",
- "public interface " + name + " {}");
- }
-}
diff --git a/javatests/dagger/hilt/processor/internal/root/RootProcessorErrorsTest.java b/javatests/dagger/hilt/processor/internal/root/RootProcessorErrorsTest.java
deleted file mode 100644
index e9ed8c418..000000000
--- a/javatests/dagger/hilt/processor/internal/root/RootProcessorErrorsTest.java
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Copyright (C) 2021 The Dagger Authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package dagger.hilt.processor.internal.root;
-
-import static com.google.testing.compile.CompilationSubject.assertThat;
-
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableList;
-import com.google.testing.compile.Compilation;
-import com.google.testing.compile.Compiler;
-import com.google.testing.compile.JavaFileObjects;
-import dagger.hilt.android.processor.AndroidCompilers;
-import javax.tools.JavaFileObject;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-@RunWith(Parameterized.class)
-public final class RootProcessorErrorsTest {
-
- @Parameters(name = "{0}")
- public static ImmutableCollection<Object[]> parameters() {
- return ImmutableList.copyOf(new Object[][] {{true}, {false}});
- }
-
- private final boolean disableCrossCompilationRootValidation;
-
- public RootProcessorErrorsTest(boolean disableCrossCompilationRootValidation) {
- this.disableCrossCompilationRootValidation = disableCrossCompilationRootValidation;
- }
-
- private Compiler compiler() {
- return AndroidCompilers.compiler()
- .withOptions(
- String.format(
- "-Adagger.hilt.disableCrossCompilationRootValidation=%s",
- disableCrossCompilationRootValidation));
- }
-
- @Test
- public void multipleAppRootsTest() {
- JavaFileObject appRoot1 =
- JavaFileObjects.forSourceLines(
- "test.AppRoot1",
- "package test;",
- "",
- "import android.app.Application;",
- "import dagger.hilt.android.HiltAndroidApp;",
- "",
- "@HiltAndroidApp(Application.class)",
- "public class AppRoot1 extends Hilt_AppRoot1 {}");
-
- JavaFileObject appRoot2 =
- JavaFileObjects.forSourceLines(
- "test.AppRoot2",
- "package test;",
- "",
- "import android.app.Application;",
- "import dagger.hilt.android.HiltAndroidApp;",
- "",
- "@HiltAndroidApp(Application.class)",
- "public class AppRoot2 extends Hilt_AppRoot2 {}");
-
- // This test case should fail independent of disableCrossCompilationRootValidation.
- Compilation compilation = compiler().compile(appRoot1, appRoot2);
- assertThat(compilation).failed();
- assertThat(compilation).hadErrorCount(1);
- assertThat(compilation)
- .hadErrorContaining(
- "Cannot process multiple app roots in the same compilation unit: "
- + "[test.AppRoot1, test.AppRoot2]");
- }
-
- @Test
- public void appRootWithTestRootTest() {
- JavaFileObject appRoot =
- JavaFileObjects.forSourceLines(
- "test.AppRoot",
- "package test;",
- "",
- "import android.app.Application;",
- "import dagger.hilt.android.HiltAndroidApp;",
- "",
- "@HiltAndroidApp(Application.class)",
- "public class AppRoot extends Hilt_AppRoot {}");
-
- JavaFileObject testRoot =
- JavaFileObjects.forSourceLines(
- "test.TestRoot",
- "package test;",
- "",
- "import dagger.hilt.android.testing.HiltAndroidTest;",
- "",
- "@HiltAndroidTest",
- "public class TestRoot {}");
-
- // This test case should fail independent of disableCrossCompilationRootValidation.
- Compilation compilation = compiler().compile(appRoot, testRoot);
- assertThat(compilation).failed();
- assertThat(compilation).hadErrorCount(1);
- assertThat(compilation)
- .hadErrorContaining(
- "Cannot process test roots and app roots in the same compilation unit:"
- + "\n \tApp root in this compilation unit: [test.AppRoot]"
- + "\n \tTest roots in this compilation unit: [test.TestRoot]");
- }
-}