1b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee/**
2b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * Copyright (C) 2006 Google Inc.
3b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee *
4b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * Licensed under the Apache License, Version 2.0 (the "License");
5b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * you may not use this file except in compliance with the License.
6b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * You may obtain a copy of the License at
7b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee *
8b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * http://www.apache.org/licenses/LICENSE-2.0
9b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee *
10b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * Unless required by applicable law or agreed to in writing, software
11b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * distributed under the License is distributed on an "AS IS" BASIS,
12b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * See the License for the specific language governing permissions and
14b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee * limitations under the License.
15b8cf1e5f607a5aaf09c904703a269ea7979781adcrazyboblee */
16a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
17a6e73987231debb0012c99d6c3df54ebffca05dccrazybobleepackage com.google.inject;
18a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
191c4d3e37ba5aa517819f56e8544c4269b8ccadc3crazybobleeimport static java.lang.annotation.RetentionPolicy.RUNTIME;
20888a264bdee08c82cccd9dcc94a8a4ac98912badsberlin
212bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruberimport com.google.inject.spi.ElementSource;
222bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruber
23c0c12ea61414709600577bb547e1e77b4a405691kevinbimport junit.framework.TestCase;
24a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
25b7a02b02d81c830d148355c90bc309bcd66fb592sberlinimport java.lang.annotation.Retention;
26b7a02b02d81c830d148355c90bc309bcd66fb592sberlin
27a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee/**
28a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee * @author crazybob@google.com (Bob Lee)
29a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee */
30a6e73987231debb0012c99d6c3df54ebffca05dccrazybobleepublic class ReflectionTest extends TestCase {
31a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
321c4d3e37ba5aa517819f56e8544c4269b8ccadc3crazyboblee  @Retention(RUNTIME)
33c0c12ea61414709600577bb547e1e77b4a405691kevinb  @BindingAnnotation @interface I {}
341c4d3e37ba5aa517819f56e8544c4269b8ccadc3crazyboblee
355746d5d7f2a97a9720ee33a7c7f9cc8e60e5cc71crazyboblee  public void testNormalBinding() throws CreationException {
363d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    final Foo foo = new Foo();
373d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit
383d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    Injector injector = Guice.createInjector(new AbstractModule() {
393d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit      protected void configure() {
403d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit        bind(Foo.class).toInstance(foo);
413d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit      }
423d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    });
433d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit
44a2915a9855e2d110c708425ec44854587e419790kevinb    Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
45bd9544e08ba5d41995024e7e301faf465457e593crazyboblee    assertSame(foo, fooBinding.getProvider().get());
462bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruber    ElementSource source = (ElementSource) fooBinding.getSource();
472bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruber    assertNotNull(source.getDeclaringSource());
48a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee    assertEquals(Key.get(Foo.class), fooBinding.getKey());
49a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee  }
50a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
515746d5d7f2a97a9720ee33a7c7f9cc8e60e5cc71crazyboblee  public void testConstantBinding() throws CreationException {
523d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    Injector injector = Guice.createInjector(new AbstractModule() {
533d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit      protected void configure() {
543d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit        bindConstant().annotatedWith(I.class).to(5);
553d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit      }
563d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    });
573d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit
58a2915a9855e2d110c708425ec44854587e419790kevinb    Binding<?> i = injector.getBinding(Key.get(int.class, I.class));
59bd9544e08ba5d41995024e7e301faf465457e593crazyboblee    assertEquals(5, i.getProvider().get());
602bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruber    ElementSource source = (ElementSource) i.getSource();
612bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruber    assertNotNull(source.getDeclaringSource());
621c4d3e37ba5aa517819f56e8544c4269b8ccadc3crazyboblee    assertEquals(Key.get(int.class, I.class), i.getKey());
63a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee  }
64a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
655746d5d7f2a97a9720ee33a7c7f9cc8e60e5cc71crazyboblee  public void testLinkedBinding() throws CreationException {
663d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    final Bar bar = new Bar();
673d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit
683d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    Injector injector = Guice.createInjector(new AbstractModule() {
693d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit      protected void configure() {
703d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit        bind(Bar.class).toInstance(bar);
713d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit        bind(Key.get(Foo.class)).to(Key.get(Bar.class));
723d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit      }
733d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit    });
743d58d6b7c1235722366a641759a02bd9c6625c21limpbizkit
75a2915a9855e2d110c708425ec44854587e419790kevinb    Binding<Foo> fooBinding = injector.getBinding(Key.get(Foo.class));
76bd9544e08ba5d41995024e7e301faf465457e593crazyboblee    assertSame(bar, fooBinding.getProvider().get());
772bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruber    ElementSource source = (ElementSource) fooBinding.getSource();
782bb4771a1835b8a145c857b63bb41ae56e5e6767Christian Edward Gruber    assertNotNull(source.getDeclaringSource());
79a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee    assertEquals(Key.get(Foo.class), fooBinding.getKey());
80a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee  }
81a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
82a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee  static class Foo {}
83a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee
84a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee  static class Bar extends Foo {}
85a6e73987231debb0012c99d6c3df54ebffca05dccrazyboblee}
86