crazyboblee | b8cf1e5 | 2007-02-02 21:48:16 +0000 | [diff] [blame] | 1 | /** |
| 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 | */ |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
| 19 | import junit.framework.TestCase; |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 20 | import java.lang.annotation.Retention; |
| 21 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * @author crazybob@google.com (Bob Lee) |
| 25 | */ |
| 26 | public class ReflectionTest extends TestCase { |
| 27 | |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 28 | @Retention(RUNTIME) |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 29 | @Binder @interface I {} |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 30 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 31 | public void testNormalBinding() throws CreationException { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 32 | ContainerBuilder builder = new ContainerBuilder(); |
| 33 | Foo foo = new Foo(); |
| 34 | builder.bind(Foo.class).to(foo); |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 35 | Container container = builder.create(); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 36 | Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class)); |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 37 | assertSame(foo, fooBinding.getLocator().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 38 | assertNotNull(fooBinding.getSource()); |
| 39 | assertEquals(Key.get(Foo.class), fooBinding.getKey()); |
| 40 | assertFalse(fooBinding.isConstant()); |
| 41 | } |
| 42 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 43 | public void testConstantBinding() throws CreationException { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 44 | ContainerBuilder builder = new ContainerBuilder(); |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 45 | builder.bindConstant(I.class).to(5); |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 46 | Container container = builder.create(); |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 47 | Binding<?> i = container.getBinding(Key.get(int.class, I.class)); |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 48 | assertEquals(5, i.getLocator().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 49 | assertNotNull(i.getSource()); |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 50 | assertEquals(Key.get(int.class, I.class), i.getKey()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 51 | assertTrue(i.isConstant()); |
| 52 | } |
| 53 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 54 | public void testLinkedBinding() throws CreationException { |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 55 | 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)); |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 59 | Container container = builder.create(); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 60 | Binding<Foo> fooBinding = container.getBinding(Key.get(Foo.class)); |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 61 | assertSame(bar, fooBinding.getLocator().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 62 | 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 | } |