01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.google.inject;
16:
17: import junit.framework.TestCase;
18:
19: /**
20: * @author crazybob@google.com (Bob Lee)
21: */
22: public class ImplicitBindingTest extends TestCase {
23:
24: public void testCircularDependency() throws CreationException {
25: Injector injector = Guice.createEmptyInjector();
26: Foo foo = injector.getInstance(Foo.class);
27: assertSame(foo, foo.bar.foo);
28: }
29:
30: static class Foo {
31: @Inject
32: Bar bar;
33: }
34:
35: static class Bar {
36: final Foo foo;
37:
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:
60: public void testDefaultProvider() {
61: Injector injector = Guice.createEmptyInjector();
62: Provided provided = injector.getInstance(Provided.class);
63: provided.go();
64: }
65:
66: @ProvidedBy(ProvidedProvider.class)
67: interface Provided {
68: void go();
69: }
70:
71: static class ProvidedProvider implements Provider<Provided> {
72: public Provided get() {
73: return new Provided() {
74: public void go() {
75: }
76: };
77: }
78: }
79:
80: }
|