KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > spring > SpringIntegrationTest


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.spring;
18
19 import junit.framework.TestCase;
20 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
21 import org.springframework.beans.factory.support.RootBeanDefinition;
22 import org.springframework.beans.factory.config.ConstructorArgumentValues;
23 import org.springframework.beans.factory.BeanFactory;
24 import com.google.inject.PerformanceComparison.TeeImpl;
25 import com.google.inject.Injector;
26 import com.google.inject.Guice;
27 import com.google.inject.AbstractModule;
28 import com.google.inject.CreationException;
29 import com.google.inject.Key;
30 import com.google.inject.name.Names;
31 import static com.google.inject.spring.SpringIntegration.*;
32
33 /**
34  * @author crazybob@google.com (Bob Lee)
35  */

36 public class SpringIntegrationTest extends TestCase {
37
38   public void testBindFromSpring() throws CreationException {
39     final DefaultListableBeanFactory beanFactory
40         = new DefaultListableBeanFactory();
41
42     RootBeanDefinition singleton
43         = new RootBeanDefinition(Singleton.class);
44     beanFactory.registerBeanDefinition("singleton", singleton);
45
46     RootBeanDefinition prototype
47         = new RootBeanDefinition(Prototype.class, false);
48     beanFactory.registerBeanDefinition("prototype", prototype);
49
50     Injector injector = Guice.createInjector(new AbstractModule() {
51       protected void configure() {
52         bind(BeanFactory.class).toInstance(beanFactory);
53         bind(Singleton.class)
54             .toProvider(fromSpring(Singleton.class, "singleton"));
55         bind(Prototype.class)
56             .toProvider(fromSpring(Prototype.class, "prototype"));
57       }
58     });
59
60     assertNotNull(injector.getInstance(Singleton.class));
61     assertSame(injector.getInstance(Singleton.class),
62         injector.getInstance(Singleton.class));
63
64     assertNotNull(injector.getInstance(Prototype.class));
65     assertNotSame(injector.getInstance(Prototype.class),
66         injector.getInstance(Prototype.class));
67   }
68
69   public void testBindAll() throws CreationException {
70     final DefaultListableBeanFactory beanFactory
71         = new DefaultListableBeanFactory();
72
73     RootBeanDefinition singleton
74         = new RootBeanDefinition(Singleton.class);
75     beanFactory.registerBeanDefinition("singleton", singleton);
76
77     RootBeanDefinition prototype
78         = new RootBeanDefinition(Prototype.class, false);
79     beanFactory.registerBeanDefinition("prototype", prototype);
80
81     Injector injector = Guice.createInjector(new AbstractModule() {
82       protected void configure() {
83         SpringIntegration.bindAll(binder(), beanFactory);
84       }
85     });
86
87     Key<Singleton> singletonKey
88         = Key.get(Singleton.class, Names.named("singleton"));
89     Key<Prototype> prototypeKey
90         = Key.get(Prototype.class, Names.named("prototype"));
91
92     assertNotNull(injector.getInstance(singletonKey));
93     assertSame(injector.getInstance(singletonKey),
94         injector.getInstance(singletonKey));
95
96     assertNotNull(injector.getInstance(prototypeKey));
97     assertNotSame(injector.getInstance(prototypeKey),
98         injector.getInstance(prototypeKey));
99   }
100
101   static class Singleton {}
102   static class Prototype {}
103 }
104
Popular Tags