xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/producers/DependentTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1*f585d8a3SJacky Wang /*
2*f585d8a3SJacky Wang  * Copyright (C) 2015 The Dagger Authors.
3*f585d8a3SJacky Wang  *
4*f585d8a3SJacky Wang  * Licensed under the Apache License, Version 2.0 (the "License");
5*f585d8a3SJacky Wang  * you may not use this file except in compliance with the License.
6*f585d8a3SJacky Wang  * You may obtain a copy of the License at
7*f585d8a3SJacky Wang  *
8*f585d8a3SJacky Wang  * http://www.apache.org/licenses/LICENSE-2.0
9*f585d8a3SJacky Wang  *
10*f585d8a3SJacky Wang  * Unless required by applicable law or agreed to in writing, software
11*f585d8a3SJacky Wang  * distributed under the License is distributed on an "AS IS" BASIS,
12*f585d8a3SJacky Wang  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*f585d8a3SJacky Wang  * See the License for the specific language governing permissions and
14*f585d8a3SJacky Wang  * limitations under the License.
15*f585d8a3SJacky Wang  */
16*f585d8a3SJacky Wang 
17*f585d8a3SJacky Wang package dagger.functional.producers;
18*f585d8a3SJacky Wang 
19*f585d8a3SJacky Wang import static com.google.common.truth.Truth.assertThat;
20*f585d8a3SJacky Wang 
21*f585d8a3SJacky Wang import com.google.common.util.concurrent.Futures;
22*f585d8a3SJacky Wang import com.google.common.util.concurrent.ListenableFuture;
23*f585d8a3SJacky Wang import org.junit.Test;
24*f585d8a3SJacky Wang import org.junit.runner.RunWith;
25*f585d8a3SJacky Wang import org.junit.runners.JUnit4;
26*f585d8a3SJacky Wang 
27*f585d8a3SJacky Wang @RunWith(JUnit4.class)
28*f585d8a3SJacky Wang public class DependentTest {
dependentComponent()29*f585d8a3SJacky Wang   @Test public void dependentComponent() throws Exception {
30*f585d8a3SJacky Wang     DependentComponent dependentComponent =
31*f585d8a3SJacky Wang         DaggerDependentComponent.builder()
32*f585d8a3SJacky Wang             .dependedProductionComponent(DaggerDependedProductionComponent.create())
33*f585d8a3SJacky Wang             .dependedComponent(DaggerDependedComponent.create())
34*f585d8a3SJacky Wang             .build();
35*f585d8a3SJacky Wang     assertThat(dependentComponent).isNotNull();
36*f585d8a3SJacky Wang     assertThat(dependentComponent.greetings().get()).containsExactly(
37*f585d8a3SJacky Wang         "2", "Hello world!", "HELLO WORLD!");
38*f585d8a3SJacky Wang   }
39*f585d8a3SJacky Wang 
reuseBuilderWithDependentComponent()40*f585d8a3SJacky Wang   @Test public void reuseBuilderWithDependentComponent() throws Exception {
41*f585d8a3SJacky Wang     DaggerDependentComponent.Builder dependentComponentBuilder = DaggerDependentComponent.builder();
42*f585d8a3SJacky Wang 
43*f585d8a3SJacky Wang     DependentComponent componentUsingComponents =
44*f585d8a3SJacky Wang         dependentComponentBuilder
45*f585d8a3SJacky Wang             .dependedProductionComponent(DaggerDependedProductionComponent.create())
46*f585d8a3SJacky Wang             .dependedComponent(DaggerDependedComponent.create())
47*f585d8a3SJacky Wang             .build();
48*f585d8a3SJacky Wang 
49*f585d8a3SJacky Wang     DependentComponent componentUsingJavaImpls = dependentComponentBuilder
50*f585d8a3SJacky Wang         .dependedProductionComponent(new DependedProductionComponent() {
51*f585d8a3SJacky Wang           @Override public ListenableFuture<Integer> numGreetings() {
52*f585d8a3SJacky Wang             return Futures.immediateFuture(3);
53*f585d8a3SJacky Wang           }
54*f585d8a3SJacky Wang         })
55*f585d8a3SJacky Wang         .dependedComponent(new DependedComponent() {
56*f585d8a3SJacky Wang           @Override public String getGreeting() {
57*f585d8a3SJacky Wang             return "Goodbye world!";
58*f585d8a3SJacky Wang           }
59*f585d8a3SJacky Wang         })
60*f585d8a3SJacky Wang         .build();
61*f585d8a3SJacky Wang 
62*f585d8a3SJacky Wang     assertThat(componentUsingJavaImpls.greetings().get()).containsExactly(
63*f585d8a3SJacky Wang         "3", "Goodbye world!", "GOODBYE WORLD!");
64*f585d8a3SJacky Wang     assertThat(componentUsingComponents.greetings().get()).containsExactly(
65*f585d8a3SJacky Wang         "2", "Hello world!", "HELLO WORLD!");
66*f585d8a3SJacky Wang 
67*f585d8a3SJacky Wang   }
68*f585d8a3SJacky Wang }
69