blob: 0d0fa1a8418e0d9e3bd2984caa0d97903a1a691f [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;
crazybobleea6e73982007-02-02 00:21:07 +000022
23/**
24 * @author crazybob@google.com (Bob Lee)
25 */
26public class ReflectionTest extends TestCase {
27
crazyboblee1c4d3e32007-02-15 04:24:13 +000028 @Retention(RUNTIME)
kevinb9nc0c12ea2007-02-20 04:46:01 +000029 @BindingAnnotation @interface I {}
crazyboblee1c4d3e32007-02-15 04:24:13 +000030
crazyboblee5746d5d2007-02-18 21:52:24 +000031 public void testNormalBinding() throws CreationException {
limpbizkit3d58d6b2008-03-08 16:11:47 +000032 InjectorBuilder builder = new InjectorBuilder();
33 final Foo foo = new Foo();
34
35 Injector injector = Guice.createInjector(new AbstractModule() {
36 protected void configure() {
37 bind(Foo.class).toInstance(foo);
38 }
39 });
40
kevinb9na2915a92007-02-28 06:20:30 +000041 Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
crazybobleebd9544e2007-02-25 20:32:11 +000042 assertSame(foo, fooBinding.getProvider().get());
crazybobleea6e73982007-02-02 00:21:07 +000043 assertNotNull(fooBinding.getSource());
44 assertEquals(Key.get(Foo.class), fooBinding.getKey());
crazybobleea6e73982007-02-02 00:21:07 +000045 }
46
crazyboblee5746d5d2007-02-18 21:52:24 +000047 public void testConstantBinding() throws CreationException {
limpbizkit3d58d6b2008-03-08 16:11:47 +000048 Injector injector = Guice.createInjector(new AbstractModule() {
49 protected void configure() {
50 bindConstant().annotatedWith(I.class).to(5);
51 }
52 });
53
kevinb9na2915a92007-02-28 06:20:30 +000054 Binding<?> i = injector.getBinding(Key.get(int.class, I.class));
crazybobleebd9544e2007-02-25 20:32:11 +000055 assertEquals(5, i.getProvider().get());
crazybobleea6e73982007-02-02 00:21:07 +000056 assertNotNull(i.getSource());
crazyboblee1c4d3e32007-02-15 04:24:13 +000057 assertEquals(Key.get(int.class, I.class), i.getKey());
crazybobleea6e73982007-02-02 00:21:07 +000058 }
59
crazyboblee5746d5d2007-02-18 21:52:24 +000060 public void testLinkedBinding() throws CreationException {
limpbizkit3d58d6b2008-03-08 16:11:47 +000061 InjectorBuilder builder = new InjectorBuilder();
62 final Bar bar = new Bar();
63
64 Injector injector = Guice.createInjector(new AbstractModule() {
65 protected void configure() {
66 bind(Bar.class).toInstance(bar);
67 bind(Key.get(Foo.class)).to(Key.get(Bar.class));
68 }
69 });
70
kevinb9na2915a92007-02-28 06:20:30 +000071 Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
crazybobleebd9544e2007-02-25 20:32:11 +000072 assertSame(bar, fooBinding.getProvider().get());
crazybobleea6e73982007-02-02 00:21:07 +000073 assertNotNull(fooBinding.getSource());
74 assertEquals(Key.get(Foo.class), fooBinding.getKey());
crazybobleea6e73982007-02-02 00:21:07 +000075 }
76
77 static class Foo {}
78
79 static class Bar extends Foo {}
80}