xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/ModelTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2018 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.internal.codegen;
18 
19 import static dagger.spi.model.testing.BindingGraphSubject.assertThat;
20 
21 import androidx.room.compiler.processing.util.Source;
22 import dagger.spi.model.BindingGraph;
23 import dagger.spi.model.BindingGraphPlugin;
24 import dagger.spi.model.DiagnosticReporter;
25 import dagger.testing.compile.CompilerTests;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 @RunWith(JUnit4.class)
31 public final class ModelTest {
32 
33   @Test
cycleTest()34   public void cycleTest() {
35     Source a =
36         CompilerTests.javaSource(
37             "test.A",
38             "package test;",
39             "",
40             "import javax.inject.Inject;",
41             "",
42             "final class A {",
43             "  @Inject A(B b) {}",
44             "}");
45     Source b =
46         CompilerTests.javaSource(
47             "test.B",
48             "package test;",
49             "",
50             "import javax.inject.Inject;",
51             "import javax.inject.Provider;",
52             "",
53             "final class B {",
54             "  @Inject B(Provider<A> a) {}",
55             "}");
56     Source component =
57         CompilerTests.javaSource(
58             "test.TestComponent",
59             "package test;",
60             "",
61             "import dagger.Component;",
62             "",
63             "@Component",
64             "interface TestComponent {",
65             "  A a();",
66             "}");
67 
68     CompilerTests.daggerCompiler(a, b, component)
69         .withBindingGraphPlugins(
70             () -> new BindingGraphPlugin() {
71               @Override
72               public void visitGraph(BindingGraph graph, DiagnosticReporter reporter) {
73                 assertThat(graph).bindingWithKey("test.A").dependsOnBindingWithKey("test.B");
74                 assertThat(graph).bindingWithKey("test.B").dependsOnBindingWithKey("test.A");
75               }
76             })
77         .compile(subject -> subject.hasErrorCount(0));
78   }
79 }
80