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 java.lang.annotation.Retention;
18: import static java.lang.annotation.RetentionPolicy.RUNTIME;
19: import junit.framework.TestCase;
20:
21: /**
22: * @author crazybob@google.com (Bob Lee)
23: */
24: public class ReflectionTest extends TestCase {
25:
26: @Retention(RUNTIME)
27: @BindingAnnotation
28: @interface I {
29: }
30:
31: public void testNormalBinding() throws CreationException {
32: BinderImpl builder = new BinderImpl();
33: Foo foo = new Foo();
34: builder.bind(Foo.class).toInstance(foo);
35: Injector injector = builder.createInjector();
36: Binding<Foo> fooBinding = injector.getBinding(Key
37: .get(Foo.class));
38: assertSame(foo, fooBinding.getProvider().get());
39: assertNotNull(fooBinding.getSource());
40: assertEquals(Key.get(Foo.class), fooBinding.getKey());
41: }
42:
43: public void testConstantBinding() throws CreationException {
44: BinderImpl builder = new BinderImpl();
45: builder.bindConstant().annotatedWith(I.class).to(5);
46: Injector injector = builder.createInjector();
47: Binding<?> i = injector.getBinding(Key.get(int.class, I.class));
48: assertEquals(5, i.getProvider().get());
49: assertNotNull(i.getSource());
50: assertEquals(Key.get(int.class, I.class), i.getKey());
51: }
52:
53: public void testLinkedBinding() throws CreationException {
54: BinderImpl builder = new BinderImpl();
55: Bar bar = new Bar();
56: builder.bind(Bar.class).toInstance(bar);
57: builder.bind(Key.get(Foo.class)).to(Key.get(Bar.class));
58: Injector injector = builder.createInjector();
59: Binding<Foo> fooBinding = injector.getBinding(Key
60: .get(Foo.class));
61: assertSame(bar, fooBinding.getProvider().get());
62: assertNotNull(fooBinding.getSource());
63: assertEquals(Key.get(Foo.class), fooBinding.getKey());
64: }
65:
66: static class Foo {
67: }
68:
69: static class Bar extends Foo {
70: }
71: }
|