crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame^] | 1 | // Copyright 2006 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | package com.google.inject; |
| 4 | |
| 5 | import junit.framework.TestCase; |
| 6 | |
| 7 | /** |
| 8 | * @author crazybob@google.com (Bob Lee) |
| 9 | */ |
| 10 | public class ReflectionTest extends TestCase { |
| 11 | |
| 12 | public void testNormalBinding() throws ContainerCreationException { |
| 13 | ContainerBuilder builder = new ContainerBuilder(); |
| 14 | Foo foo = new Foo(); |
| 15 | builder.bind(Foo.class).to(foo); |
| 16 | Container container = builder.create(false); |
| 17 | Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class)); |
| 18 | assertSame(foo, fooBinding.getFactory().get()); |
| 19 | assertNotNull(fooBinding.getSource()); |
| 20 | assertEquals(Key.get(Foo.class), fooBinding.getKey()); |
| 21 | assertFalse(fooBinding.isConstant()); |
| 22 | } |
| 23 | |
| 24 | public void testConstantBinding() throws ContainerCreationException { |
| 25 | ContainerBuilder builder = new ContainerBuilder(); |
| 26 | builder.bind("i").to(5); |
| 27 | Container container = builder.create(false); |
| 28 | Binding<?> i = container.getBinding(Key.get(int.class, "i")); |
| 29 | assertEquals(5, i.getFactory().get()); |
| 30 | assertNotNull(i.getSource()); |
| 31 | assertEquals(Key.get(int.class, "i"), i.getKey()); |
| 32 | assertTrue(i.isConstant()); |
| 33 | } |
| 34 | |
| 35 | public void testLinkedBinding() throws ContainerCreationException { |
| 36 | ContainerBuilder builder = new ContainerBuilder(); |
| 37 | Bar bar = new Bar(); |
| 38 | builder.bind(Bar.class).to(bar); |
| 39 | builder.link(Key.get(Foo.class)).to(Key.get(Bar.class)); |
| 40 | Container container = builder.create(false); |
| 41 | Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class)); |
| 42 | assertSame(bar, fooBinding.getFactory().get()); |
| 43 | assertNotNull(fooBinding.getSource()); |
| 44 | assertEquals(Key.get(Foo.class), fooBinding.getKey()); |
| 45 | assertFalse(fooBinding.isConstant()); |
| 46 | } |
| 47 | |
| 48 | static class Foo {} |
| 49 | |
| 50 | static class Bar extends Foo {} |
| 51 | } |