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