KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > ProviderInjectionTest


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 junit.framework.TestCase;
20
21 /**
22  * @author crazybob@google.com (Bob Lee)
23  */

24 public class ProviderInjectionTest extends TestCase {
25
26   public void testProviderInjection() throws CreationException {
27     BinderImpl builder = new BinderImpl();
28
29     builder.bind(Bar.class);
30     builder.bind(SampleSingleton.class).in(Scopes.SINGLETON);
31
32     Injector injector = builder.createInjector();
33
34     Foo foo = injector.getInstance(Foo.class);
35
36     Bar bar = foo.barProvider.get();
37     assertNotNull(bar);
38     assertNotSame(bar, foo.barProvider.get());
39
40     SampleSingleton singleton = foo.singletonProvider.get();
41     assertNotNull(singleton);
42     assertSame(singleton, foo.singletonProvider.get());
43   }
44
45   static class Foo {
46     @Inject Provider<Bar> barProvider;
47     @Inject Provider<SampleSingleton> singletonProvider;
48   }
49
50   static class Bar {}
51
52   static class SampleSingleton {}
53 }
54
Popular Tags