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; |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 22 | import com.google.inject.internal.InjectorBuilder; |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * @author crazybob@google.com (Bob Lee) |
| 26 | */ |
| 27 | public class ReflectionTest extends TestCase { |
| 28 | |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 29 | @Retention(RUNTIME) |
kevinb9n | c0c12ea | 2007-02-20 04:46:01 +0000 | [diff] [blame] | 30 | @BindingAnnotation @interface I {} |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 31 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame] | 32 | public void testNormalBinding() throws CreationException { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 33 | InjectorBuilder builder = new InjectorBuilder(); |
| 34 | final Foo foo = new Foo(); |
| 35 | |
| 36 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 37 | protected void configure() { |
| 38 | bind(Foo.class).toInstance(foo); |
| 39 | } |
| 40 | }); |
| 41 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 42 | Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class)); |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 43 | assertSame(foo, fooBinding.getProvider().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 44 | assertNotNull(fooBinding.getSource()); |
| 45 | assertEquals(Key.get(Foo.class), fooBinding.getKey()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 46 | } |
| 47 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame] | 48 | public void testConstantBinding() throws CreationException { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 49 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 50 | protected void configure() { |
| 51 | bindConstant().annotatedWith(I.class).to(5); |
| 52 | } |
| 53 | }); |
| 54 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 55 | Binding<?> i = injector.getBinding(Key.get(int.class, I.class)); |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 56 | assertEquals(5, i.getProvider().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 57 | assertNotNull(i.getSource()); |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 58 | assertEquals(Key.get(int.class, I.class), i.getKey()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 59 | } |
| 60 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame] | 61 | public void testLinkedBinding() throws CreationException { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 62 | InjectorBuilder builder = new InjectorBuilder(); |
| 63 | final Bar bar = new Bar(); |
| 64 | |
| 65 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 66 | protected void configure() { |
| 67 | bind(Bar.class).toInstance(bar); |
| 68 | bind(Key.get(Foo.class)).to(Key.get(Bar.class)); |
| 69 | } |
| 70 | }); |
| 71 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 72 | Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class)); |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 73 | assertSame(bar, fooBinding.getProvider().get()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 74 | assertNotNull(fooBinding.getSource()); |
| 75 | assertEquals(Key.get(Foo.class), fooBinding.getKey()); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static class Foo {} |
| 79 | |
| 80 | static class Bar extends Foo {} |
| 81 | } |