KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > CircularDependencyTest


1 /**
2  * Copyright (C) 2006 Google Inc.
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 com.google.inject;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @author crazybob@google.com (Bob Lee)
23  */

24 public class CircularDependencyTest extends TestCase {
25
26   public void testCircularlyDependentConstructors()
27       throws CreationException {
28     BinderImpl builder = new BinderImpl();
29     builder.bind(A.class).to(AImpl.class);
30     builder.bind(B.class).to(BImpl.class);
31
32     Injector injector = builder.createInjector();
33     A a = injector.getInstance(A.class);
34     assertNotNull(a.getB().getA());
35   }
36
37   interface A {
38     B getB();
39   }
40
41   @Singleton
42   static class AImpl implements A {
43     final B b;
44     @Inject public AImpl(B b) {
45       this.b = b;
46     }
47     public B getB() {
48       return b;
49     }
50   }
51
52   interface B {
53     A getA();
54   }
55
56   static class BImpl implements B {
57     final A a;
58     @Inject public BImpl(A a) {
59       this.a = a;
60     }
61     public A getA() {
62       return a;
63     }
64   }
65
66 }
67
Popular Tags