diff options
Diffstat (limited to 'javatests/dagger/hilt/processor/internal')
12 files changed, 4 insertions, 1024 deletions
diff --git a/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java b/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java index a8c5fbe9c..901c7a3b0 100644 --- a/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java +++ b/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java @@ -64,7 +64,7 @@ public final class DefineComponentProcessorTest { JavaFileObject componentOutput = JavaFileObjects.forSourceLines( - "dagger.hilt.processor.internal.definecomponent.codegen._test_FooComponent", + "dagger.hilt.processor.internal.definecomponent.codegen.test_FooComponent", "package dagger.hilt.processor.internal.definecomponent.codegen;", "", "import dagger.hilt.internal.definecomponent.DefineComponentClasses;", @@ -72,11 +72,11 @@ public final class DefineComponentProcessorTest { "", "@DefineComponentClasses(component = \"test.FooComponent\")", "@Generated(\"" + DefineComponentProcessor.class.getName() + "\")", - "public class _test_FooComponent {}"); + "interface test_FooComponent {}"); JavaFileObject builderOutput = JavaFileObjects.forSourceLines( - "dagger.hilt.processor.internal.definecomponent.codegen._test_FooComponentBuilder", + "dagger.hilt.processor.internal.definecomponent.codegen.test_FooComponentBuilder", "package dagger.hilt.processor.internal.definecomponent.codegen;", "", "import dagger.hilt.internal.definecomponent.DefineComponentClasses;", @@ -84,7 +84,7 @@ public final class DefineComponentProcessorTest { "", "@DefineComponentClasses(builder = \"test.FooComponentBuilder\")", "@Generated(\"" + DefineComponentProcessor.class.getName() + "\")", - "public class _test_FooComponentBuilder {}"); + "interface test_FooComponentBuilder {}"); Compilation compilation = compiler().compile(component, builder); assertThat(compilation).succeeded(); diff --git a/javatests/dagger/hilt/processor/internal/originatingelement/BUILD b/javatests/dagger/hilt/processor/internal/originatingelement/BUILD deleted file mode 100644 index 3135159ce..000000000 --- a/javatests/dagger/hilt/processor/internal/originatingelement/BUILD +++ /dev/null @@ -1,41 +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"]) - -compiler_test( - name = "OriginatingElementProcessorTest", - srcs = ["OriginatingElementProcessorTest.java"], - compiler_deps = [ - "//java/dagger/hilt/codegen:originating_element", - "@androidsdk//:platforms/android-30/android.jar", - "@maven//:androidx_annotation_annotation", - ], - deps = [ - "//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/originatingelement/OriginatingElementProcessorTest.java b/javatests/dagger/hilt/processor/internal/originatingelement/OriginatingElementProcessorTest.java deleted file mode 100644 index 444fb1d67..000000000 --- a/javatests/dagger/hilt/processor/internal/originatingelement/OriginatingElementProcessorTest.java +++ /dev/null @@ -1,85 +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.originatingelement; - -import static com.google.testing.compile.CompilationSubject.assertThat; -import static dagger.hilt.android.processor.AndroidCompilers.compiler; - -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; - -@RunWith(JUnit4.class) -public class OriginatingElementProcessorTest { - - @Test - public void originatingElementOnInnerClass_fails() { - JavaFileObject outer1 = - JavaFileObjects.forSourceLines( - "test.Outer1", - "package test;", - "", - "class Outer1 {}"); - JavaFileObject outer2 = - JavaFileObjects.forSourceLines( - "test.Outer2", - "package test;", - "", - "import dagger.hilt.codegen.OriginatingElement;", - "", - "class Outer2 {", - " @OriginatingElement(topLevelClass = Outer1.class)", - " static class Inner {}", - "}"); - Compilation compilation = compiler().compile(outer1, outer2); - assertThat(compilation).failed(); - assertThat(compilation) - .hadErrorContaining( - "@OriginatingElement should only be used to annotate top-level types, but found: " - + "test.Outer2.Inner"); - } - - @Test - public void originatingElementValueWithInnerClass_fails() { - JavaFileObject outer1 = - JavaFileObjects.forSourceLines( - "test.Outer1", - "package test;", - "", - "class Outer1 {", - " static class Inner {}", - "}"); - JavaFileObject outer2 = - JavaFileObjects.forSourceLines( - "test.Outer2", - "package test;", - "", - "import dagger.hilt.codegen.OriginatingElement;", - "", - "@OriginatingElement(topLevelClass = Outer1.Inner.class)", - "class Outer2 {}"); - Compilation compilation = compiler().compile(outer1, outer2); - assertThat(compilation).failed(); - assertThat(compilation) - .hadErrorContaining( - "OriginatingElement.topLevelClass value should be a top-level class, but found: " - + "test.Outer1.Inner"); - } -} 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]"); - } -} diff --git a/javatests/dagger/hilt/processor/internal/uninstallmodules/BUILD b/javatests/dagger/hilt/processor/internal/uninstallmodules/BUILD deleted file mode 100644 index caceb7ce5..000000000 --- a/javatests/dagger/hilt/processor/internal/uninstallmodules/BUILD +++ /dev/null @@ -1,44 +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"]) - -compiler_test( - name = "UninstallModulesProcessorTest", - srcs = ["UninstallModulesProcessorTest.java"], - compiler_deps = [ - "//java/dagger/hilt/android:android_entry_point", - "//java/dagger/hilt/android/testing:hilt_android_test", - "//java/dagger/hilt/android/testing:uninstall_modules", - "//java/dagger/hilt/migration:disable_install_in_check", - "@androidsdk//:platforms/android-30/android.jar", - "@maven//:androidx_annotation_annotation", - ], - deps = [ - "//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/uninstallmodules/UninstallModulesProcessorTest.java b/javatests/dagger/hilt/processor/internal/uninstallmodules/UninstallModulesProcessorTest.java deleted file mode 100644 index 2be9ab670..000000000 --- a/javatests/dagger/hilt/processor/internal/uninstallmodules/UninstallModulesProcessorTest.java +++ /dev/null @@ -1,162 +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.uninstallmodules; - -import static com.google.testing.compile.CompilationSubject.assertThat; -import static dagger.hilt.android.processor.AndroidCompilers.compiler; - -import com.google.testing.compile.Compilation; -import com.google.testing.compile.JavaFileObjects; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class UninstallModulesProcessorTest { - - @Test - public void testInvalidModuleNoInstallIn_fails() { - Compilation compilation = - compiler() - .compile( - JavaFileObjects.forSourceLines( - "test.MyTest", - "package test;", - "", - "import dagger.hilt.android.testing.HiltAndroidTest;", - "import dagger.hilt.android.testing.UninstallModules;", - "", - "@UninstallModules(InvalidModule.class)", - "@HiltAndroidTest", - "public class MyTest {}"), - JavaFileObjects.forSourceLines( - "test.InvalidModule", - "package test;", - "", - "import dagger.Module;", - "import dagger.hilt.migration.DisableInstallInCheck;", - "", - "@DisableInstallInCheck", - "@Module", - "public class InvalidModule {}")); - assertThat(compilation).failed(); - assertThat(compilation).hadErrorCount(1); - assertThat(compilation) - .hadErrorContaining( - "@UninstallModules should only include modules annotated with both @Module and " - + "@InstallIn, but found: [test.InvalidModule]."); - } - - @Test - public void testInvalidModuleNoModule_fails() { - Compilation compilation = - compiler() - .compile( - JavaFileObjects.forSourceLines( - "test.MyTest", - "package test;", - "", - "import dagger.hilt.android.testing.HiltAndroidTest;", - "import dagger.hilt.android.testing.UninstallModules;", - "", - "@UninstallModules(InvalidModule.class)", - "@HiltAndroidTest", - "public class MyTest {}"), - JavaFileObjects.forSourceLines( - "test.InvalidModule", - "package test;", - "", - "public class InvalidModule {", - "}")); - assertThat(compilation).failed(); - assertThat(compilation).hadErrorCount(1); - assertThat(compilation) - .hadErrorContaining( - "@UninstallModules should only include modules annotated with both @Module and " - + "@InstallIn, but found: [test.InvalidModule]."); - } - - @Test - public void testInvalidTest_fails() { - Compilation compilation = - compiler() - .compile( - JavaFileObjects.forSourceLines( - "test.InvalidTest", - "package test;", - "", - "import dagger.hilt.android.testing.UninstallModules;", - "", - "@UninstallModules(ValidModule.class)", - "public class InvalidTest {}"), - JavaFileObjects.forSourceLines( - "test.ValidModule", - "package test;", - "", - "import dagger.Module;", - "import dagger.hilt.InstallIn;", - "import dagger.hilt.components.SingletonComponent;", - "", - "@Module", - "@InstallIn(SingletonComponent.class)", - "public class ValidModule {}")); - assertThat(compilation).failed(); - assertThat(compilation).hadErrorCount(1); - assertThat(compilation) - .hadErrorContaining( - "@UninstallModules should only be used on test classes annotated with @HiltAndroidTest," - + " but found: test.InvalidTest"); - } - - @Test - public void testInvalidTestModule_fails() { - Compilation compilation = - compiler() - .compile( - JavaFileObjects.forSourceLines( - "test.MyTest", - "package test;", - "", - "import dagger.Module;", - "import dagger.hilt.InstallIn;", - "import dagger.hilt.components.SingletonComponent;", - "import dagger.hilt.android.testing.HiltAndroidTest;", - "import dagger.hilt.android.testing.UninstallModules;", - "", - "@UninstallModules({", - " MyTest.PkgPrivateInvalidModule.class,", - " MyTest.PublicInvalidModule.class,", - "})", - "@HiltAndroidTest", - "public class MyTest {", - " @Module", - " @InstallIn(SingletonComponent.class)", - " interface PkgPrivateInvalidModule {}", - "", - " @Module", - " @InstallIn(SingletonComponent.class)", - " public interface PublicInvalidModule {}", - "}")); - assertThat(compilation).failed(); - assertThat(compilation).hadErrorCount(1); - // TODO(bcorso): Consider unwrapping pkg-private modules before reporting the error. - assertThat(compilation) - .hadErrorContaining( - "@UninstallModules should not contain test modules, but found: " - + "[test.MyTest.PkgPrivateInvalidModule, test.MyTest.PublicInvalidModule]"); - } -} |