001: /**
002: * Copyright (C) 2006 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.google.inject.spring;
016:
017: import junit.framework.TestCase;
018: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
019: import org.springframework.beans.factory.support.RootBeanDefinition;
020: import org.springframework.beans.factory.config.ConstructorArgumentValues;
021: import org.springframework.beans.factory.BeanFactory;
022: import com.google.inject.PerformanceComparison.TeeImpl;
023: import com.google.inject.Injector;
024: import com.google.inject.Guice;
025: import com.google.inject.AbstractModule;
026: import com.google.inject.CreationException;
027: import com.google.inject.Key;
028: import com.google.inject.name.Names;
029: import static com.google.inject.spring.SpringIntegration.*;
030:
031: /**
032: * @author crazybob@google.com (Bob Lee)
033: */
034: public class SpringIntegrationTest extends TestCase {
035:
036: public void testBindFromSpring() throws CreationException {
037: final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
038:
039: RootBeanDefinition singleton = new RootBeanDefinition(
040: Singleton.class);
041: beanFactory.registerBeanDefinition("singleton", singleton);
042:
043: RootBeanDefinition prototype = new RootBeanDefinition(
044: Prototype.class, false);
045: beanFactory.registerBeanDefinition("prototype", prototype);
046:
047: Injector injector = Guice.createInjector(new AbstractModule() {
048: protected void configure() {
049: bind(BeanFactory.class).toInstance(beanFactory);
050: bind(Singleton.class).toProvider(
051: fromSpring(Singleton.class, "singleton"));
052: bind(Prototype.class).toProvider(
053: fromSpring(Prototype.class, "prototype"));
054: }
055: });
056:
057: assertNotNull(injector.getInstance(Singleton.class));
058: assertSame(injector.getInstance(Singleton.class), injector
059: .getInstance(Singleton.class));
060:
061: assertNotNull(injector.getInstance(Prototype.class));
062: assertNotSame(injector.getInstance(Prototype.class), injector
063: .getInstance(Prototype.class));
064: }
065:
066: public void testBindAll() throws CreationException {
067: final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
068:
069: RootBeanDefinition singleton = new RootBeanDefinition(
070: Singleton.class);
071: beanFactory.registerBeanDefinition("singleton", singleton);
072:
073: RootBeanDefinition prototype = new RootBeanDefinition(
074: Prototype.class, false);
075: beanFactory.registerBeanDefinition("prototype", prototype);
076:
077: Injector injector = Guice.createInjector(new AbstractModule() {
078: protected void configure() {
079: SpringIntegration.bindAll(binder(), beanFactory);
080: }
081: });
082:
083: Key<Singleton> singletonKey = Key.get(Singleton.class, Names
084: .named("singleton"));
085: Key<Prototype> prototypeKey = Key.get(Prototype.class, Names
086: .named("prototype"));
087:
088: assertNotNull(injector.getInstance(singletonKey));
089: assertSame(injector.getInstance(singletonKey), injector
090: .getInstance(singletonKey));
091:
092: assertNotNull(injector.getInstance(prototypeKey));
093: assertNotSame(injector.getInstance(prototypeKey), injector
094: .getInstance(prototypeKey));
095: }
096:
097: static class Singleton {
098: }
099:
100: static class Prototype {
101: }
102: }
|