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
17package com.google.inject.spring;
18
19import static com.google.inject.spring.SpringIntegration.fromSpring;
20
21import com.google.inject.AbstractModule;
22import com.google.inject.CreationException;
23import com.google.inject.Guice;
24import com.google.inject.Injector;
25import com.google.inject.Key;
26import com.google.inject.name.Names;
27
28import junit.framework.TestCase;
29
30import org.springframework.beans.factory.BeanFactory;
31import org.springframework.beans.factory.support.DefaultListableBeanFactory;
32import org.springframework.beans.factory.support.RootBeanDefinition;
33
34/**
35 * @author crazybob@google.com (Bob Lee)
36 */
37public class SpringIntegrationTest extends TestCase {
38
39  public void testBindFromSpring() throws CreationException {
40    final DefaultListableBeanFactory beanFactory
41        = new DefaultListableBeanFactory();
42
43    RootBeanDefinition singleton
44        = new RootBeanDefinition(Singleton.class);
45    beanFactory.registerBeanDefinition("singleton", singleton);
46
47    RootBeanDefinition prototype
48        = new RootBeanDefinition(Prototype.class, false);
49    beanFactory.registerBeanDefinition("prototype", prototype);
50
51    Injector injector = Guice.createInjector(new AbstractModule() {
52      protected void configure() {
53        bind(BeanFactory.class).toInstance(beanFactory);
54        bind(Singleton.class)
55            .toProvider(fromSpring(Singleton.class, "singleton"));
56        bind(Prototype.class)
57            .toProvider(fromSpring(Prototype.class, "prototype"));
58      }
59    });
60
61    assertNotNull(injector.getInstance(Singleton.class));
62    assertSame(injector.getInstance(Singleton.class),
63        injector.getInstance(Singleton.class));
64
65    assertNotNull(injector.getInstance(Prototype.class));
66    assertNotSame(injector.getInstance(Prototype.class),
67        injector.getInstance(Prototype.class));
68  }
69
70  public void testBindAll() throws CreationException {
71    final DefaultListableBeanFactory beanFactory
72        = new DefaultListableBeanFactory();
73
74    RootBeanDefinition singleton
75        = new RootBeanDefinition(Singleton.class);
76    beanFactory.registerBeanDefinition("singleton", singleton);
77
78    RootBeanDefinition prototype
79        = new RootBeanDefinition(Prototype.class, false);
80    beanFactory.registerBeanDefinition("prototype", prototype);
81
82    Injector injector = Guice.createInjector(new AbstractModule() {
83      protected void configure() {
84        SpringIntegration.bindAll(binder(), beanFactory);
85      }
86    });
87
88    Key<Singleton> singletonKey
89        = Key.get(Singleton.class, Names.named("singleton"));
90    Key<Prototype> prototypeKey
91        = Key.get(Prototype.class, Names.named("prototype"));
92
93    assertNotNull(injector.getInstance(singletonKey));
94    assertSame(injector.getInstance(singletonKey),
95        injector.getInstance(singletonKey));
96
97    assertNotNull(injector.getInstance(prototypeKey));
98    assertNotSame(injector.getInstance(prototypeKey),
99        injector.getInstance(prototypeKey));
100  }
101
102  static class Singleton {}
103  static class Prototype {}
104}
105