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.aliasof; 18 19 import androidx.room.compiler.processing.util.Source; 20 import dagger.hilt.android.testing.compile.HiltCompilerTests; 21 import org.junit.Rule; 22 import org.junit.Test; 23 import org.junit.rules.TemporaryFolder; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 /** Tests for failure on alias scope used on DefineComponent. */ 28 @RunWith(JUnit4.class) 29 public final class AliasOfProcessorTest { 30 @Test fails_componentScopedWithAliasScope()31 public void fails_componentScopedWithAliasScope() { 32 Source scope = 33 HiltCompilerTests.javaSource( 34 "test.AliasScope", 35 "package test;", 36 "", 37 "import javax.inject.Scope;", 38 "import javax.inject.Singleton;", 39 "import dagger.hilt.migration.AliasOf;", 40 "", 41 "@Scope", 42 "@AliasOf(Singleton.class)", 43 "public @interface AliasScope{}"); 44 45 Source root = 46 HiltCompilerTests.javaSource( 47 "test.MyApp", 48 "package test;", 49 "", 50 "import android.app.Application;", 51 "import dagger.hilt.android.HiltAndroidApp;", 52 "", 53 "@HiltAndroidApp(Application.class)", 54 "public final class MyApp extends Hilt_MyApp {}"); 55 56 Source defineComponent = 57 HiltCompilerTests.javaSource( 58 "test.ChildComponent", 59 "package test;", 60 "", 61 "import dagger.hilt.DefineComponent;", 62 "import dagger.hilt.components.SingletonComponent;", 63 "", 64 "@DefineComponent(parent = SingletonComponent.class)", 65 "@AliasScope", 66 "public interface ChildComponent {}"); 67 68 HiltCompilerTests.hiltCompiler(root, defineComponent, scope) 69 .withJavacArguments("-Xlint:-processing") // Suppresses unclaimed annotation warning 70 .compile( 71 subject -> 72 // TODO(user): TAP result inconsistent with local build. 73 // if (HiltCompilerTests.backend(subject) == Backend.JAVAC) { 74 // subject.hasErrorCount(2); 75 // } else { 76 // subject.hasErrorCount(1); 77 // } 78 subject.hasErrorContaining( 79 "@DefineComponent test.ChildComponent, references invalid scope(s) annotated" 80 + " with @AliasOf. @DefineComponent scopes cannot be aliases of other scopes:" 81 + " [@test.AliasScope]")); 82 } 83 84 @Test fails_alisOfOnNonScope()85 public void fails_alisOfOnNonScope() { 86 Source scope = 87 HiltCompilerTests.javaSource( 88 "test.AliasScope", 89 "package test;", 90 "", 91 "import javax.inject.Scope;", 92 "import javax.inject.Singleton;", 93 "import dagger.hilt.migration.AliasOf;", 94 "", 95 "@AliasOf(Singleton.class)", 96 "public @interface AliasScope{}"); 97 98 HiltCompilerTests.hiltCompiler(scope) 99 .compile( 100 subject -> { 101 subject.hasErrorCount(1); 102 subject.hasErrorContaining( 103 "AliasOf should only be used on scopes. However, it was found " 104 + "annotating test.AliasScope"); 105 }); 106 } 107 108 @Rule public TemporaryFolder tempFolderRule = new TemporaryFolder(); 109 110 @Test fails_conflictingAliasScope()111 public void fails_conflictingAliasScope() { 112 Source scope = 113 HiltCompilerTests.javaSource( 114 "test.AliasScope", 115 "package test;", 116 "", 117 "import javax.inject.Scope;", 118 "import javax.inject.Singleton;", 119 "import dagger.hilt.android.scopes.ActivityScoped;", 120 "import dagger.hilt.migration.AliasOf;", 121 "", 122 "@Scope", 123 "@AliasOf({Singleton.class, ActivityScoped.class})", 124 "public @interface AliasScope{}"); 125 126 Source root = 127 HiltCompilerTests.javaSource( 128 "test.MyApp", 129 "package test;", 130 "", 131 "import android.app.Application;", 132 "import dagger.hilt.android.HiltAndroidApp;", 133 "", 134 "@HiltAndroidApp(Application.class)", 135 "public final class MyApp extends Hilt_MyApp {}"); 136 137 HiltCompilerTests.hiltCompiler(root, scope) 138 .compile( 139 subject -> { 140 subject.hasErrorCount(1); 141 subject.hasErrorContaining("has conflicting scopes"); 142 }); 143 } 144 } 145