001: /**
002: * Copyright (C) 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package com.google.inject;
016:
017: import java.lang.annotation.Retention;
018: import static java.lang.annotation.RetentionPolicy.RUNTIME;
019: import junit.framework.TestCase;
020:
021: /**
022: * @author crazybob@google.com (Bob Lee)
023: */
024: public class InjectorTest extends TestCase {
025:
026: @Retention(RUNTIME)
027: @BindingAnnotation
028: @interface Other {
029: }
030:
031: @Retention(RUNTIME)
032: @BindingAnnotation
033: @interface S {
034: }
035:
036: @Retention(RUNTIME)
037: @BindingAnnotation
038: @interface I {
039: }
040:
041: public void testProviderMethods() throws CreationException {
042: SampleSingleton singleton = new SampleSingleton();
043: SampleSingleton other = new SampleSingleton();
044:
045: BinderImpl builder = new BinderImpl();
046: builder.bind(SampleSingleton.class).toInstance(singleton);
047: builder.bind(SampleSingleton.class).annotatedWith(Other.class)
048: .toInstance(other);
049: Injector injector = builder.createInjector();
050:
051: assertSame(singleton, injector.getInstance(Key
052: .get(SampleSingleton.class)));
053: assertSame(singleton, injector
054: .getInstance(SampleSingleton.class));
055:
056: assertSame(other, injector.getInstance(Key.get(
057: SampleSingleton.class, Other.class)));
058: }
059:
060: static class SampleSingleton {
061: }
062:
063: public void testInjection() throws CreationException {
064: Injector injector = createFooInjector();
065: Foo foo = injector.getInstance(Foo.class);
066:
067: assertEquals("test", foo.s);
068: assertEquals("test", foo.bar.getTee().getS());
069: assertSame(foo.bar, foo.copy);
070: assertEquals(5, foo.i);
071: assertEquals(5, foo.bar.getI());
072:
073: // Test circular dependency.
074: assertSame(foo.bar, foo.bar.getTee().getBar());
075: }
076:
077: private Injector createFooInjector() throws CreationException {
078: return Guice.createInjector(new AbstractModule() {
079: protected void configure() {
080: bind(Bar.class).to(BarImpl.class);
081: bind(Tee.class).to(TeeImpl.class);
082: bindConstant().annotatedWith(S.class).to("test");
083: bindConstant().annotatedWith(I.class).to(5);
084: }
085: });
086: }
087:
088: public void testGetInstance() throws CreationException {
089: Injector injector = createFooInjector();
090:
091: Bar bar = injector.getInstance(Key.get(Bar.class));
092: assertEquals("test", bar.getTee().getS());
093: assertEquals(5, bar.getI());
094: }
095:
096: public void testIntAndIntegerAreInterchangeable()
097: throws CreationException {
098: BinderImpl builder = new BinderImpl();
099: builder.bindConstant().annotatedWith(I.class).to(5);
100: Injector injector = builder.createInjector();
101: IntegerWrapper iw = injector.getInstance(IntegerWrapper.class);
102: assertEquals(5, (int) iw.i);
103: }
104:
105: static class IntegerWrapper {
106: @Inject
107: @I
108: Integer i;
109: }
110:
111: static class Foo {
112:
113: @Inject
114: Bar bar;
115: @Inject
116: Bar copy;
117:
118: @Inject
119: @S
120: String s;
121:
122: int i;
123:
124: @Inject
125: void setI(@I
126: int i) {
127: this .i = i;
128: }
129: }
130:
131: interface Bar {
132:
133: Tee getTee();
134:
135: int getI();
136: }
137:
138: @Singleton
139: static class BarImpl implements Bar {
140:
141: @Inject
142: @I
143: int i;
144:
145: Tee tee;
146:
147: @Inject
148: void initialize(Tee tee) {
149: this .tee = tee;
150: }
151:
152: public Tee getTee() {
153: return tee;
154: }
155:
156: public int getI() {
157: return i;
158: }
159: }
160:
161: interface Tee {
162:
163: String getS();
164:
165: Bar getBar();
166: }
167:
168: static class TeeImpl implements Tee {
169:
170: final String s;
171: @Inject
172: Bar bar;
173:
174: @Inject
175: TeeImpl(@S
176: String s) {
177: this .s = s;
178: }
179:
180: public String getS() {
181: return s;
182: }
183:
184: public Bar getBar() {
185: return bar;
186: }
187: }
188:
189: public void testInjectStatics() throws CreationException {
190: BinderImpl builder = new BinderImpl();
191: builder.bindConstant().annotatedWith(S.class).to("test");
192: builder.bindConstant().annotatedWith(I.class).to(5);
193: builder.requestStaticInjection(Static.class);
194: builder.createInjector();
195:
196: assertEquals("test", Static.s);
197: assertEquals(5, Static.i);
198: }
199:
200: static class Static {
201:
202: @Inject
203: @I
204: static int i;
205:
206: static String s;
207:
208: @Inject
209: static void setS(@S
210: String s) {
211: Static.s = s;
212: }
213: }
214:
215: public void testPrivateInjection() throws CreationException {
216: Injector injector = Guice.createInjector(new AbstractModule() {
217: protected void configure() {
218: bind(String.class).toInstance("foo");
219: bind(int.class).toInstance(5);
220: }
221: });
222:
223: Private p = injector.getInstance(Private.class);
224: assertEquals("foo", p.fromConstructor);
225: assertEquals(5, p.fromMethod);
226: }
227:
228: static class Private {
229: String fromConstructor;
230: int fromMethod;
231:
232: @Inject
233: private Private(String fromConstructor) {
234: this .fromConstructor = fromConstructor;
235: }
236:
237: @Inject
238: private void setInt(int i) {
239: this .fromMethod = i;
240: }
241: }
242:
243: public void testProtectedInjection() throws CreationException {
244: Injector injector = Guice.createInjector(new AbstractModule() {
245: protected void configure() {
246: bind(String.class).toInstance("foo");
247: bind(int.class).toInstance(5);
248: }
249: });
250:
251: Protected p = injector.getInstance(Protected.class);
252: assertEquals("foo", p.fromConstructor);
253: assertEquals(5, p.fromMethod);
254: }
255:
256: static class Protected {
257: String fromConstructor;
258: int fromMethod;
259:
260: @Inject
261: protected Protected(String fromConstructor) {
262: this .fromConstructor = fromConstructor;
263: }
264:
265: @Inject
266: protected void setInt(int i) {
267: this .fromMethod = i;
268: }
269: }
270:
271: public void testInstanceInjectionHappensAfterFactoriesAreSetUp() {
272: Guice.createInjector(new AbstractModule() {
273: protected void configure() {
274: bind(Object.class).toInstance(new Object() {
275: @Inject
276: Runnable r;
277: });
278:
279: bind(Runnable.class).to(MyRunnable.class);
280: }
281: });
282: }
283:
284: static class MyRunnable implements Runnable {
285: public void run() {
286: }
287: }
288: }
|