01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.google.inject;
16:
17: import junit.framework.TestCase;
18:
19: /**
20: * @author crazybob@google.com (Bob Lee)
21: */
22: public class BoundInstanceInjectionTest extends TestCase {
23:
24: public void testInstancesAreInjected() throws CreationException {
25: final O o = new O();
26:
27: Injector injector = Guice.createInjector(new AbstractModule() {
28: protected void configure() {
29: bind(O.class).toInstance(o);
30: bind(int.class).toInstance(5);
31: }
32: });
33:
34: assertEquals(5, o.fromMethod);
35: }
36:
37: static class O {
38: int fromMethod;
39:
40: @Inject
41: void setInt(int i) {
42: this .fromMethod = i;
43: }
44: }
45:
46: public void testProvidersAreInjected() throws CreationException {
47: Injector injector = Guice.createInjector(new AbstractModule() {
48: protected void configure() {
49: bind(O.class).toProvider(new Provider<O>() {
50: @Inject
51: int i;
52:
53: public O get() {
54: O o = new O();
55: o.setInt(i);
56: return o;
57: }
58: });
59: bind(int.class).toInstance(5);
60: }
61: });
62:
63: assertEquals(5, injector.getInstance(O.class).fromMethod);
64: }
65:
66: }
|