blob: 181541149ccee297e75c31251212e089d51d37c5 [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;
20
21/**
22 * @author crazybob@google.com (Bob Lee)
23 */
24public class ReflectionTest extends TestCase {
25
26 public void testNormalBinding() throws ContainerCreationException {
27 ContainerBuilder builder = new ContainerBuilder();
28 Foo foo = new Foo();
29 builder.bind(Foo.class).to(foo);
30 Container container = builder.create(false);
31 Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class));
32 assertSame(foo, fooBinding.getFactory().get());
33 assertNotNull(fooBinding.getSource());
34 assertEquals(Key.get(Foo.class), fooBinding.getKey());
35 assertFalse(fooBinding.isConstant());
36 }
37
38 public void testConstantBinding() throws ContainerCreationException {
39 ContainerBuilder builder = new ContainerBuilder();
40 builder.bind("i").to(5);
41 Container container = builder.create(false);
42 Binding<?> i = container.getBinding(Key.get(int.class, "i"));
43 assertEquals(5, i.getFactory().get());
44 assertNotNull(i.getSource());
45 assertEquals(Key.get(int.class, "i"), i.getKey());
46 assertTrue(i.isConstant());
47 }
48
49 public void testLinkedBinding() throws ContainerCreationException {
50 ContainerBuilder builder = new ContainerBuilder();
51 Bar bar = new Bar();
52 builder.bind(Bar.class).to(bar);
53 builder.link(Key.get(Foo.class)).to(Key.get(Bar.class));
54 Container container = builder.create(false);
55 Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class));
56 assertSame(bar, fooBinding.getFactory().get());
57 assertNotNull(fooBinding.getSource());
58 assertEquals(Key.get(Foo.class), fooBinding.getKey());
59 assertFalse(fooBinding.isConstant());
60 }
61
62 static class Foo {}
63
64 static class Bar extends Foo {}
65}