1 /* 2 * Copyright (C) 2020 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.functional.producers; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.util.concurrent.Futures; 22 import com.google.common.util.concurrent.ListenableFuture; 23 import dagger.producers.ProductionComponent; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 /** 29 * Tests component dependencies. 30 */ 31 @RunWith(JUnit4.class) 32 public final class ComponentDependenciesTest { 33 public interface One { getString()34 ListenableFuture<String> getString(); 35 } 36 37 public interface Two { getString()38 ListenableFuture<String> getString(); 39 } 40 41 public interface Merged extends One, Two { 42 } 43 44 @ProductionComponent(dependencies = Merged.class) 45 interface TestProductionComponent { getString()46 ListenableFuture<String> getString(); 47 48 @ProductionComponent.Builder 49 interface Builder { dep(Merged dep)50 Builder dep(Merged dep); 51 build()52 TestProductionComponent build(); 53 } 54 } 55 56 @Test testSameMethodTwiceProduction()57 public void testSameMethodTwiceProduction() throws Exception { 58 TestProductionComponent component = 59 DaggerComponentDependenciesTest_TestProductionComponent.builder().dep( 60 () -> Futures.immediateFuture("test")).build(); 61 assertThat(component.getString().get()).isEqualTo("test"); 62 } 63 64 public interface OneOverride { getString()65 ListenableFuture<?> getString(); 66 } 67 68 public interface TwoOverride { getString()69 ListenableFuture<?> getString(); 70 } 71 72 public interface MergedOverride extends OneOverride, TwoOverride { 73 @Override getString()74 ListenableFuture<String> getString(); 75 } 76 77 @ProductionComponent(dependencies = MergedOverride.class) 78 interface TestOverrideComponent { getString()79 ListenableFuture<String> getString(); 80 81 @ProductionComponent.Builder 82 interface Builder { dep(MergedOverride dep)83 Builder dep(MergedOverride dep); 84 build()85 TestOverrideComponent build(); 86 } 87 } 88 89 @Test testPolymorphicOverridesStillCompiles()90 public void testPolymorphicOverridesStillCompiles() throws Exception { 91 TestOverrideComponent component = 92 DaggerComponentDependenciesTest_TestOverrideComponent.builder().dep( 93 () -> Futures.immediateFuture("test")).build(); 94 assertThat(component.getString().get()).isEqualTo("test"); 95 } 96 } 97