KickJava   Java API By Example, From Geeks To Geeks.

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


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 ImplicitBindingTest extends TestCase {
25
26   public void testCircularDependency() throws CreationException {
27     Injector injector = Guice.createEmptyInjector();
28     Foo foo = injector.getInstance(Foo.class);
29     assertSame(foo, foo.bar.foo);
30   }
31
32   static class Foo {
33     @Inject Bar bar;
34   }
35
36   static class Bar {
37     final Foo foo;
38     @Inject
39     public Bar(Foo foo) {
40       this.foo = foo;
41     }
42   }
43
44   public void testDefaultImplementation() {
45     Injector injector = Guice.createEmptyInjector();
46     I i = injector.getInstance(I.class);
47     i.go();
48   }
49
50   @ImplementedBy(IImpl.class)
51   interface I {
52     void go();
53   }
54
55   static class IImpl implements I {
56     public void go() {}
57   }
58
59   public void testDefaultProvider() {
60     Injector injector = Guice.createEmptyInjector();
61     Provided provided = injector.getInstance(Provided.class);
62     provided.go();
63   }
64
65   @ProvidedBy(ProvidedProvider.class)
66   interface Provided {
67     void go();
68   }
69
70   static class ProvidedProvider implements Provider<Provided> {
71     public Provided get() {
72       return new Provided() {
73         public void go() {}
74       };
75     }
76   }
77
78 }
79
Popular Tags