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 | |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 19 | import java.lang.annotation.Retention; |
| 20 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
kevinb9n | c0c12ea | 2007-02-20 04:46:01 +0000 | [diff] [blame] | 21 | import junit.framework.TestCase; |
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) |
kevinb9n | c0c12ea | 2007-02-20 04:46:01 +0000 | [diff] [blame] | 29 | @BindingAnnotation @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 { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 32 | 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 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 41 | Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class)); |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 42 | assertSame(foo, fooBinding.getProvider().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 43 | assertNotNull(fooBinding.getSource()); |
| 44 | assertEquals(Key.get(Foo.class), fooBinding.getKey()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 45 | } |
| 46 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame] | 47 | public void testConstantBinding() throws CreationException { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 48 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 49 | protected void configure() { |
| 50 | bindConstant().annotatedWith(I.class).to(5); |
| 51 | } |
| 52 | }); |
| 53 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 54 | Binding<?> i = injector.getBinding(Key.get(int.class, I.class)); |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 55 | assertEquals(5, i.getProvider().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 56 | assertNotNull(i.getSource()); |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 57 | assertEquals(Key.get(int.class, I.class), i.getKey()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame] | 60 | public void testLinkedBinding() throws CreationException { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 61 | 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 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 71 | Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class)); |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 72 | assertSame(bar, fooBinding.getProvider().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 73 | assertNotNull(fooBinding.getSource()); |
| 74 | assertEquals(Key.get(Foo.class), fooBinding.getKey()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static class Foo {} |
| 78 | |
| 79 | static class Bar extends Foo {} |
| 80 | } |