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.ElementType.FIELD; 20 import static java.lang.annotation.ElementType.METHOD; 21 import static java.lang.annotation.ElementType.PARAMETER; 22 import static java.lang.annotation.RetentionPolicy.RUNTIME; 23 24 import com.google.inject.name.Named; 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.Target; 27 import junit.framework.TestCase; 28 29 /** @author crazybob@google.com (Bob Lee) */ 30 public class BoundInstanceInjectionTest extends TestCase { 31 testInstancesAreInjected()32 public void testInstancesAreInjected() throws CreationException { 33 final O o = new O(); 34 35 Injector injector = 36 Guice.createInjector( 37 new AbstractModule() { 38 @Override 39 protected void configure() { 40 bind(O.class).toInstance(o); 41 bind(int.class).toInstance(5); 42 } 43 }); 44 45 assertEquals(5, o.fromMethod); 46 } 47 48 static class O { 49 int fromMethod; 50 51 @Inject setInt(int i)52 void setInt(int i) { 53 this.fromMethod = i; 54 } 55 } 56 testProvidersAreInjected()57 public void testProvidersAreInjected() throws CreationException { 58 Injector injector = 59 Guice.createInjector( 60 new AbstractModule() { 61 @Override 62 protected void configure() { 63 bind(O.class) 64 .toProvider( 65 new Provider<O>() { 66 @Inject int i; 67 68 @Override 69 public O get() { 70 O o = new O(); 71 o.setInt(i); 72 return o; 73 } 74 }); 75 bind(int.class).toInstance(5); 76 } 77 }); 78 79 assertEquals(5, injector.getInstance(O.class).fromMethod); 80 } 81 testMalformedInstance()82 public void testMalformedInstance() { 83 try { 84 Guice.createInjector( 85 new AbstractModule() { 86 @Override 87 protected void configure() { 88 bind(Object.class).toInstance(new MalformedInjectable()); 89 } 90 }); 91 fail(); 92 } catch (CreationException expected) { 93 Asserts.assertContains( 94 expected.getMessage(), 95 MalformedInjectable.class.getName(), 96 ".doublyAnnotated() has more than one ", 97 "annotation annotated with @BindingAnnotation: ", 98 Named.class.getName() + " and " + Another.class.getName()); 99 } 100 } 101 testMalformedProvider()102 public void testMalformedProvider() { 103 try { 104 Guice.createInjector( 105 new AbstractModule() { 106 @Override 107 protected void configure() { 108 bind(String.class).toProvider(new MalformedProvider()); 109 } 110 }); 111 fail(); 112 } catch (CreationException expected) { 113 Asserts.assertContains( 114 expected.getMessage(), 115 MalformedProvider.class.getName(), 116 ".doublyAnnotated() has more than one ", 117 "annotation annotated with @BindingAnnotation: ", 118 Named.class.getName() + " and " + Another.class.getName()); 119 } 120 } 121 122 static class MalformedInjectable { 123 @Inject doublyAnnotated(@amed"a") @nother String unused)124 void doublyAnnotated(@Named("a") @Another String unused) {} 125 } 126 127 static class MalformedProvider implements Provider<String> { 128 @Inject doublyAnnotated(@amed"a") @nother String s)129 void doublyAnnotated(@Named("a") @Another String s) {} 130 131 @Override get()132 public String get() { 133 return "a"; 134 } 135 } 136 137 @BindingAnnotation 138 @Target({FIELD, PARAMETER, METHOD}) 139 @Retention(RUNTIME) 140 public @interface Another {} 141 } 142