• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 
17 package com.google.inject;
18 
19 import static java.lang.annotation.RetentionPolicy.RUNTIME;
20 
21 import com.google.inject.spi.ElementSource;
22 import java.lang.annotation.Retention;
23 import junit.framework.TestCase;
24 
25 /** @author crazybob@google.com (Bob Lee) */
26 public class ReflectionTest extends TestCase {
27 
28   @Retention(RUNTIME)
29   @BindingAnnotation
30   @interface I {}
31 
testNormalBinding()32   public void testNormalBinding() throws CreationException {
33     final Foo foo = new Foo();
34 
35     Injector injector =
36         Guice.createInjector(
37             new AbstractModule() {
38               @Override
39               protected void configure() {
40                 bind(Foo.class).toInstance(foo);
41               }
42             });
43 
44     Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
45     assertSame(foo, fooBinding.getProvider().get());
46     ElementSource source = (ElementSource) fooBinding.getSource();
47     assertNotNull(source.getDeclaringSource());
48     assertEquals(Key.get(Foo.class), fooBinding.getKey());
49   }
50 
testConstantBinding()51   public void testConstantBinding() throws CreationException {
52     Injector injector =
53         Guice.createInjector(
54             new AbstractModule() {
55               @Override
56               protected void configure() {
57                 bindConstant().annotatedWith(I.class).to(5);
58               }
59             });
60 
61     Binding<?> i = injector.getBinding(Key.get(int.class, I.class));
62     assertEquals(5, i.getProvider().get());
63     ElementSource source = (ElementSource) i.getSource();
64     assertNotNull(source.getDeclaringSource());
65     assertEquals(Key.get(int.class, I.class), i.getKey());
66   }
67 
testLinkedBinding()68   public void testLinkedBinding() throws CreationException {
69     final Bar bar = new Bar();
70 
71     Injector injector =
72         Guice.createInjector(
73             new AbstractModule() {
74               @Override
75               protected void configure() {
76                 bind(Bar.class).toInstance(bar);
77                 bind(Key.get(Foo.class)).to(Key.get(Bar.class));
78               }
79             });
80 
81     Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
82     assertSame(bar, fooBinding.getProvider().get());
83     ElementSource source = (ElementSource) fooBinding.getSource();
84     assertNotNull(source.getDeclaringSource());
85     assertEquals(Key.get(Foo.class), fooBinding.getKey());
86   }
87 
88   static class Foo {}
89 
90   static class Bar extends Foo {}
91 }
92