blob: a86ad6c83f0db94d62b99737095d4f4ab276ecd7 [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
19import junit.framework.TestCase;
crazyboblee1c4d3e32007-02-15 04:24:13 +000020import java.lang.annotation.Retention;
21import static java.lang.annotation.RetentionPolicy.RUNTIME;
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)
crazyboblee278ee4d2007-02-15 19:23:13 +000029 @Binder @interface I {}
crazyboblee1c4d3e32007-02-15 04:24:13 +000030
crazyboblee5746d5d2007-02-18 21:52:24 +000031 public void testNormalBinding() throws CreationException {
crazybobleea6e73982007-02-02 00:21:07 +000032 ContainerBuilder builder = new ContainerBuilder();
33 Foo foo = new Foo();
34 builder.bind(Foo.class).to(foo);
crazyboblee278ee4d2007-02-15 19:23:13 +000035 Container container = builder.create();
crazybobleea6e73982007-02-02 00:21:07 +000036 Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class));
crazyboblee5746d5d2007-02-18 21:52:24 +000037 assertSame(foo, fooBinding.getLocator().get());
crazybobleea6e73982007-02-02 00:21:07 +000038 assertNotNull(fooBinding.getSource());
39 assertEquals(Key.get(Foo.class), fooBinding.getKey());
40 assertFalse(fooBinding.isConstant());
41 }
42
crazyboblee5746d5d2007-02-18 21:52:24 +000043 public void testConstantBinding() throws CreationException {
crazybobleea6e73982007-02-02 00:21:07 +000044 ContainerBuilder builder = new ContainerBuilder();
crazyboblee1c4d3e32007-02-15 04:24:13 +000045 builder.bindConstant(I.class).to(5);
crazyboblee278ee4d2007-02-15 19:23:13 +000046 Container container = builder.create();
crazyboblee1c4d3e32007-02-15 04:24:13 +000047 Binding<?> i = container.getBinding(Key.get(int.class, I.class));
crazyboblee5746d5d2007-02-18 21:52:24 +000048 assertEquals(5, i.getLocator().get());
crazybobleea6e73982007-02-02 00:21:07 +000049 assertNotNull(i.getSource());
crazyboblee1c4d3e32007-02-15 04:24:13 +000050 assertEquals(Key.get(int.class, I.class), i.getKey());
crazybobleea6e73982007-02-02 00:21:07 +000051 assertTrue(i.isConstant());
52 }
53
crazyboblee5746d5d2007-02-18 21:52:24 +000054 public void testLinkedBinding() throws CreationException {
crazybobleea6e73982007-02-02 00:21:07 +000055 ContainerBuilder builder = new ContainerBuilder();
56 Bar bar = new Bar();
57 builder.bind(Bar.class).to(bar);
58 builder.link(Key.get(Foo.class)).to(Key.get(Bar.class));
crazyboblee278ee4d2007-02-15 19:23:13 +000059 Container container = builder.create();
crazybobleea6e73982007-02-02 00:21:07 +000060 Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class));
crazyboblee5746d5d2007-02-18 21:52:24 +000061 assertSame(bar, fooBinding.getLocator().get());
crazybobleea6e73982007-02-02 00:21:07 +000062 assertNotNull(fooBinding.getSource());
63 assertEquals(Key.get(Foo.class), fooBinding.getKey());
64 assertFalse(fooBinding.isConstant());
65 }
66
67 static class Foo {}
68
69 static class Bar extends Foo {}
70}