001: /**
002: * Copyright (C) 2007 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.bm.ejb3guice.inject;
016:
017: import junit.framework.TestCase;
018: import java.lang.annotation.Retention;
019: import java.lang.annotation.Target;
020: import java.lang.annotation.ElementType;
021: import static java.lang.annotation.RetentionPolicy.RUNTIME;
022:
023: /**
024: * @author crazybob@google.com (Bob Lee)
025: */
026: public class ProviderMethodsTest extends TestCase {
027:
028: public void testProviderMethods() {
029: Injector injector = Ejb3Guice.createInjector(new Module() {
030: public void configure(Binder binder) {
031: binder.install(ProviderMethods
032: .from(ProviderMethodsTest.this ));
033: }
034: });
035:
036: Bob bob = injector.getInstance(Bob.class);
037: assertEquals("A Bob", bob.getName());
038:
039: Bob clone = injector.getInstance(Bob.class);
040: assertEquals("A Bob", clone.getName());
041:
042: assertNotSame(bob, clone);
043: assertSame(bob.getDaughter(), clone.getDaughter());
044:
045: Key soleBobKey = Key.get(Bob.class, Sole.class);
046: assertSame(injector.getInstance(soleBobKey), injector
047: .getInstance(soleBobKey));
048: }
049:
050: interface Bob {
051: String getName();
052:
053: Dagny getDaughter();
054: }
055:
056: interface Dagny {
057: int getAge();
058: }
059:
060: @Provides
061: Bob provideBob(final Dagny dagny) {
062: return new Bob() {
063: public String getName() {
064: return "A Bob";
065: }
066:
067: public Dagny getDaughter() {
068: return dagny;
069: }
070: };
071: }
072:
073: @Provides
074: @Singleton
075: @Sole
076: Bob provideSoleBob(final Dagny dagny) {
077: return new Bob() {
078: public String getName() {
079: return "Only Bob";
080: }
081:
082: public Dagny getDaughter() {
083: return dagny;
084: }
085: };
086: }
087:
088: @Provides
089: @Singleton
090: Dagny provideDagny() {
091: return new Dagny() {
092: public int getAge() {
093: return 1;
094: }
095: };
096: }
097:
098: @Retention(RUNTIME)
099: @Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD})
100: @BindingAnnotation
101: @interface Sole {
102: }
103:
104: // We'll have to make getProvider() support circular dependencies before this
105: // will work.
106: //
107: // public void testCircularDependency() {
108: // Injector injector = Guice.createInjector(new Module() {
109: // public void configure(Binder binder) {
110: // binder.install(ProviderMethods.from(ProviderMethodsTest.this));
111: // }
112: // });
113: //
114: // Foo foo = injector.getInstance(Foo.class);
115: // assertEquals(5, foo.getI());
116: // assertEquals(10, foo.getBar().getI());
117: // assertEquals(5, foo.getBar().getFoo().getI());
118: // }
119: //
120: // interface Foo {
121: // Bar getBar();
122: // int getI();
123: // }
124: //
125: // interface Bar {
126: // Foo getFoo();
127: // int getI();
128: // }
129: //
130: // @Provides Foo newFoo(final Bar bar) {
131: // return new Foo() {
132: //
133: // public Bar getBar() {
134: // return bar;
135: // }
136: //
137: // public int getI() {
138: // return 5;
139: // }
140: // };
141: // }
142: //
143: // @Provides Bar newBar(final Foo foo) {
144: // return new Bar() {
145: //
146: // public Foo getFoo() {
147: // return foo;
148: // }
149: //
150: // public int getI() {
151: // return 10;
152: // }
153: // };
154: // }
155: }
|