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 static com.google.testing.compile.CompilationSubject.assertThat; 20 import static dagger.hilt.android.testing.compile.HiltCompilerTests.compiler; 21 22 import com.google.testing.compile.Compilation; 23 import com.google.testing.compile.JavaFileObjects; 24 import javax.tools.JavaFileObject; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Tests for failure on alias scope used on DefineComponent. */ 30 @RunWith(JUnit4.class) 31 public final class AliasOfProcessorTest { 32 @Test fails_componentScopedWithAliasScope()33 public void fails_componentScopedWithAliasScope() { 34 JavaFileObject scope = 35 JavaFileObjects.forSourceLines( 36 "test.AliasScope", 37 "package test;", 38 "", 39 "import javax.inject.Scope;", 40 "import javax.inject.Singleton;", 41 "import dagger.hilt.migration.AliasOf;", 42 "", 43 "@Scope", 44 "@AliasOf(Singleton.class)", 45 "public @interface AliasScope{}"); 46 47 JavaFileObject root = 48 JavaFileObjects.forSourceLines( 49 "test.MyApp", 50 "package test;", 51 "", 52 "import android.app.Application;", 53 "import dagger.hilt.android.HiltAndroidApp;", 54 "", 55 "@HiltAndroidApp(Application.class)", 56 "public final class MyApp extends Hilt_MyApp {}"); 57 58 JavaFileObject defineComponent = 59 JavaFileObjects.forSourceLines( 60 "test.ChildComponent", 61 "package test;", 62 "", 63 "import dagger.hilt.DefineComponent;", 64 "import dagger.hilt.components.SingletonComponent;", 65 "", 66 "@DefineComponent(parent = SingletonComponent.class)", 67 "@AliasScope", 68 "public interface ChildComponent {}"); 69 70 Compilation compilation = 71 compiler() 72 .withOptions("-Xlint:-processing") // Suppresses unclaimed annotation warning 73 .compile(root, defineComponent, scope); 74 75 assertThat(compilation).failed(); 76 // One extra error for the missing Hilt_MyApp reference 77 assertThat(compilation).hadErrorCount(2); 78 assertThat(compilation) 79 .hadErrorContaining( 80 "@DefineComponent test.ChildComponent, references invalid scope(s) annotated with" 81 + " @AliasOf. @DefineComponent scopes cannot be aliases of other scopes:" 82 + " [@test.AliasScope]"); 83 } 84 85 @Test fails_conflictingAliasScope()86 public void fails_conflictingAliasScope() { 87 JavaFileObject scope = 88 JavaFileObjects.forSourceLines( 89 "test.AliasScope", 90 "package test;", 91 "", 92 "import javax.inject.Scope;", 93 "import javax.inject.Singleton;", 94 "import dagger.hilt.android.scopes.ActivityScoped;", 95 "import dagger.hilt.migration.AliasOf;", 96 "", 97 "@Scope", 98 "@AliasOf({Singleton.class, ActivityScoped.class})", 99 "public @interface AliasScope{}"); 100 101 JavaFileObject root = 102 JavaFileObjects.forSourceLines( 103 "test.MyApp", 104 "package test;", 105 "", 106 "import android.app.Application;", 107 "import dagger.hilt.android.HiltAndroidApp;", 108 "", 109 "@HiltAndroidApp(Application.class)", 110 "public final class MyApp extends Hilt_MyApp {}"); 111 112 Compilation compilation = 113 compiler() 114 .withOptions("-Xlint:-processing") // Suppresses unclaimed annotation warning 115 .compile(root, scope); 116 117 assertThat(compilation).failed(); 118 assertThat(compilation).hadErrorCount(1); 119 assertThat(compilation).hadErrorContaining("has conflicting scopes"); 120 } 121 } 122