Home | History | Annotate | Download | only in inject
      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 
     23 import junit.framework.TestCase;
     24 
     25 import java.lang.annotation.Retention;
     26 
     27 /**
     28  * @author crazybob (at) google.com (Bob Lee)
     29  */
     30 public class ReflectionTest extends TestCase {
     31 
     32   @Retention(RUNTIME)
     33   @BindingAnnotation @interface I {}
     34 
     35   public void testNormalBinding() throws CreationException {
     36     final Foo foo = new Foo();
     37 
     38     Injector injector = Guice.createInjector(new AbstractModule() {
     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 
     51   public void testConstantBinding() throws CreationException {
     52     Injector injector = Guice.createInjector(new AbstractModule() {
     53       protected void configure() {
     54         bindConstant().annotatedWith(I.class).to(5);
     55       }
     56     });
     57 
     58     Binding<?> i = injector.getBinding(Key.get(int.class, I.class));
     59     assertEquals(5, i.getProvider().get());
     60     ElementSource source = (ElementSource) i.getSource();
     61     assertNotNull(source.getDeclaringSource());
     62     assertEquals(Key.get(int.class, I.class), i.getKey());
     63   }
     64 
     65   public void testLinkedBinding() throws CreationException {
     66     final Bar bar = new Bar();
     67 
     68     Injector injector = Guice.createInjector(new AbstractModule() {
     69       protected void configure() {
     70         bind(Bar.class).toInstance(bar);
     71         bind(Key.get(Foo.class)).to(Key.get(Bar.class));
     72       }
     73     });
     74 
     75     Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
     76     assertSame(bar, fooBinding.getProvider().get());
     77     ElementSource source = (ElementSource) fooBinding.getSource();
     78     assertNotNull(source.getDeclaringSource());
     79     assertEquals(Key.get(Foo.class), fooBinding.getKey());
     80   }
     81 
     82   static class Foo {}
     83 
     84   static class Bar extends Foo {}
     85 }
     86