• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 import junit.framework.TestCase;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.Target;
30 
31 /**
32  * @author crazybob@google.com (Bob Lee)
33  */
34 public class BoundInstanceInjectionTest extends TestCase {
35 
testInstancesAreInjected()36   public void testInstancesAreInjected() throws CreationException {
37     final O o = new O();
38 
39     Injector injector = Guice.createInjector(new AbstractModule() {
40       protected void configure() {
41         bind(O.class).toInstance(o);
42         bind(int.class).toInstance(5);
43       }
44     });
45 
46     assertEquals(5, o.fromMethod);
47   }
48 
49   static class O {
50     int fromMethod;
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 = Guice.createInjector(new AbstractModule() {
59       protected void configure() {
60         bind(O.class).toProvider(new Provider<O>() {
61           @Inject int i;
62           public O get() {
63             O o = new O();
64             o.setInt(i);
65             return o;
66           }
67         });
68         bind(int.class).toInstance(5);
69       }
70     });
71 
72     assertEquals(5, injector.getInstance(O.class).fromMethod);
73   }
74 
testMalformedInstance()75   public void testMalformedInstance() {
76     try {
77       Guice.createInjector(new AbstractModule() {
78         protected void configure() {
79           bind(Object.class).toInstance(new MalformedInjectable());
80         }
81       });
82       fail();
83     } catch (CreationException expected) {
84       Asserts.assertContains(expected.getMessage(), MalformedInjectable.class.getName(),
85           ".doublyAnnotated() has more than one ", "annotation annotated with @BindingAnnotation: ",
86           Named.class.getName() + " and " + Another.class.getName());
87     }
88   }
89 
testMalformedProvider()90   public void testMalformedProvider() {
91     try {
92       Guice.createInjector(new AbstractModule() {
93         protected void configure() {
94           bind(String.class).toProvider(new MalformedProvider());
95         }
96       });
97       fail();
98     } catch (CreationException expected) {
99       Asserts.assertContains(expected.getMessage(), MalformedProvider.class.getName(),
100           ".doublyAnnotated() has more than one ", "annotation annotated with @BindingAnnotation: ",
101           Named.class.getName() + " and " + Another.class.getName());
102     }
103   }
104 
105   static class MalformedInjectable {
doublyAnnotated(@amed"a") @nother String unused)106     @Inject void doublyAnnotated(@Named("a") @Another String unused) {}
107   }
108 
109   static class MalformedProvider implements Provider<String> {
doublyAnnotated(@amed"a") @nother String s)110     @Inject void doublyAnnotated(@Named("a") @Another String s) {}
111 
get()112     public String get() {
113       return "a";
114     }
115   }
116 
117   @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
118   public @interface Another {}
119 }
120