Home | History | Annotate | Download | only in inject
      1 /**
      2  * Copyright (C) 2008 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 com.google.inject.Asserts.assertContains;
     20 import static com.google.inject.util.Types.listOf;
     21 
     22 import com.google.inject.util.Types;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import java.util.List;
     27 
     28 /**
     29  * Demonstrates type reification.
     30  *
     31  * @author jessewilson (at) google.com (Jesse Wilson)
     32  */
     33 public class TypeLiteralInjectionTest extends TestCase {
     34 
     35   public void testBindingToRawTypeLiteralIsNotAllowed() {
     36     try {
     37       Guice.createInjector(new AbstractModule() {
     38         protected void configure() {
     39           bind(TypeLiteral.class).toInstance(TypeLiteral.get(String.class));
     40         }
     41       });
     42       fail();
     43     } catch (CreationException expected) {
     44       assertContains(expected.getMessage(),
     45           "Binding to core guice framework type is not allowed: TypeLiteral");
     46     }
     47   }
     48 
     49   public void testBindingToParameterizedTypeLiteralIsNotAllowed() {
     50     try {
     51       Guice.createInjector(new AbstractModule() {
     52         protected void configure() {
     53           bind(new TypeLiteral<TypeLiteral<String>>() {})
     54               .toInstance(TypeLiteral.get(String.class));
     55         }
     56       });
     57       fail();
     58     } catch (CreationException expected) {
     59       assertContains(expected.getMessage(),
     60           "Binding to core guice framework type is not allowed: TypeLiteral");
     61     }
     62   }
     63 
     64   public void testInjectTypeLiteralWithRawTypes() {
     65     C c = Guice.createInjector().getInstance(C.class);
     66     assertEquals(TypeLiteral.get(String.class), c.string);
     67     assertEquals(TypeLiteral.get(A.class), c.a);
     68 
     69     try {
     70       Guice.createInjector().getInstance(B.class);
     71       fail();
     72     } catch (ConfigurationException expected) {
     73       assertContains(expected.getMessage(), TypeLiteral.class.getName() + "<java.util.List<T>> "
     74           + "cannot be used as a key; It is not fully specified.");
     75     }
     76   }
     77 
     78   public void testInjectTypeLiteralWithClassTypes() {
     79     B<Integer> b = Guice.createInjector().getInstance(new Key<B<Integer>>() {});
     80     assertEquals(TypeLiteral.get(String.class), b.string);
     81     assertEquals(TypeLiteral.get(Integer.class), b.t);
     82     assertEquals(TypeLiteral.get(listOf(Integer.class)), b.listOfT);
     83     assertEquals(TypeLiteral.get(listOf(Types.subtypeOf(Integer.class))), b.listOfWildcardT);
     84   }
     85 
     86   public void testInjectRawTypeLiteral() {
     87     try {
     88       Guice.createInjector().getInstance(TypeLiteral.class);
     89       fail();
     90     } catch (ConfigurationException expected) {
     91       assertContains(expected.getMessage(),
     92           "Cannot inject a TypeLiteral that has no type parameter");
     93     }
     94   }
     95 
     96   static class A<T> {
     97     @Inject TypeLiteral<String> string;
     98     @Inject TypeLiteral<List<T>> listOfT;
     99     @Inject TypeLiteral<List<? extends T>> listOfWildcardT;
    100   }
    101 
    102   static class B<T> extends A<T> {
    103     @Inject TypeLiteral<T> t;
    104   }
    105 
    106   static class C<T> {
    107     @Inject TypeLiteral<String> string;
    108     @Inject TypeLiteral<A> a;
    109     T t;
    110   }
    111 }
    112