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 com.google.testing.compile.CompilationSubject.assertThat; 20 import static com.google.testing.compile.Compiler.javac; 21 import static dagger.model.testing.BindingGraphSubject.assertThat; 22 23 import com.google.testing.compile.Compilation; 24 import com.google.testing.compile.JavaFileObjects; 25 import dagger.model.BindingGraph; 26 import javax.tools.JavaFileObject; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 @RunWith(JUnit4.class) 32 public final class ModelTest { 33 34 @Test cycleTest()35 public void cycleTest() { 36 JavaFileObject a = 37 JavaFileObjects.forSourceLines( 38 "test.A", 39 "package test;", 40 "", 41 "import javax.inject.Inject;", 42 "", 43 "final class A {", 44 " @Inject A(B b) {}", 45 "}"); 46 JavaFileObject b = 47 JavaFileObjects.forSourceLines( 48 "test.B", 49 "package test;", 50 "", 51 "import javax.inject.Inject;", 52 "import javax.inject.Provider;", 53 "", 54 "final class B {", 55 " @Inject B(Provider<A> a) {}", 56 "}"); 57 JavaFileObject component = 58 JavaFileObjects.forSourceLines( 59 "test.TestComponent", 60 "package test;", 61 "", 62 "import dagger.Component;", 63 "", 64 "@Component", 65 "interface TestComponent {", 66 " A a();", 67 "}"); 68 69 BindingGraphCapturer capturer = new BindingGraphCapturer(); 70 Compilation compilation = 71 javac().withProcessors(ComponentProcessor.forTesting(capturer)).compile(a, b, component); 72 assertThat(compilation).succeeded(); 73 BindingGraph bindingGraph = capturer.bindingGraphs().get("test.TestComponent"); 74 assertThat(bindingGraph).bindingWithKey("test.A").dependsOnBindingWithKey("test.B"); 75 assertThat(bindingGraph).bindingWithKey("test.B").dependsOnBindingWithKey("test.A"); 76 } 77 } 78