blob: b60112073ed4041a2167753a52139c89ccd1ce6b [file] [log] [blame]
crazybobleeb8cf1e52007-02-02 21:48:16 +00001/**
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 */
crazybobleea6e73982007-02-02 00:21:07 +000016
17package com.google.inject;
18
crazyboblee1c4d3e32007-02-15 04:24:13 +000019import java.lang.annotation.Retention;
20import static java.lang.annotation.RetentionPolicy.RUNTIME;
kevinb9nc0c12ea2007-02-20 04:46:01 +000021import junit.framework.TestCase;
limpbizkit5ae41eb2009-06-06 17:51:27 +000022import com.google.inject.internal.InjectorBuilder;
crazybobleea6e73982007-02-02 00:21:07 +000023
24/**
25 * @author crazybob@google.com (Bob Lee)
26 */
27public class ReflectionTest extends TestCase {
28
crazyboblee1c4d3e32007-02-15 04:24:13 +000029 @Retention(RUNTIME)
kevinb9nc0c12ea2007-02-20 04:46:01 +000030 @BindingAnnotation @interface I {}
crazyboblee1c4d3e32007-02-15 04:24:13 +000031
crazyboblee5746d5d2007-02-18 21:52:24 +000032 public void testNormalBinding() throws CreationException {
limpbizkit3d58d6b2008-03-08 16:11:47 +000033 InjectorBuilder builder = new InjectorBuilder();
34 final Foo foo = new Foo();
35
36 Injector injector = Guice.createInjector(new AbstractModule() {
37 protected void configure() {
38 bind(Foo.class).toInstance(foo);
39 }
40 });
41
kevinb9na2915a92007-02-28 06:20:30 +000042 Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
crazybobleebd9544e2007-02-25 20:32:11 +000043 assertSame(foo, fooBinding.getProvider().get());
crazybobleea6e73982007-02-02 00:21:07 +000044 assertNotNull(fooBinding.getSource());
45 assertEquals(Key.get(Foo.class), fooBinding.getKey());
crazybobleea6e73982007-02-02 00:21:07 +000046 }
47
crazyboblee5746d5d2007-02-18 21:52:24 +000048 public void testConstantBinding() throws CreationException {
limpbizkit3d58d6b2008-03-08 16:11:47 +000049 Injector injector = Guice.createInjector(new AbstractModule() {
50 protected void configure() {
51 bindConstant().annotatedWith(I.class).to(5);
52 }
53 });
54
kevinb9na2915a92007-02-28 06:20:30 +000055 Binding<?> i = injector.getBinding(Key.get(int.class, I.class));
crazybobleebd9544e2007-02-25 20:32:11 +000056 assertEquals(5, i.getProvider().get());
crazybobleea6e73982007-02-02 00:21:07 +000057 assertNotNull(i.getSource());
crazyboblee1c4d3e32007-02-15 04:24:13 +000058 assertEquals(Key.get(int.class, I.class), i.getKey());
crazybobleea6e73982007-02-02 00:21:07 +000059 }
60
crazyboblee5746d5d2007-02-18 21:52:24 +000061 public void testLinkedBinding() throws CreationException {
limpbizkit3d58d6b2008-03-08 16:11:47 +000062 InjectorBuilder builder = new InjectorBuilder();
63 final Bar bar = new Bar();
64
65 Injector injector = Guice.createInjector(new AbstractModule() {
66 protected void configure() {
67 bind(Bar.class).toInstance(bar);
68 bind(Key.get(Foo.class)).to(Key.get(Bar.class));
69 }
70 });
71
kevinb9na2915a92007-02-28 06:20:30 +000072 Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
crazybobleebd9544e2007-02-25 20:32:11 +000073 assertSame(bar, fooBinding.getProvider().get());
crazybobleea6e73982007-02-02 00:21:07 +000074 assertNotNull(fooBinding.getSource());
75 assertEquals(Key.get(Foo.class), fooBinding.getKey());
crazybobleea6e73982007-02-02 00:21:07 +000076 }
77
78 static class Foo {}
79
80 static class Bar extends Foo {}
81}