001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by                                                          *
009     *****************************************************************************/
010    package org.picocontainer.behaviors;
011    
012    import org.picocontainer.tck.AbstractComponentFactoryTestCase;
013    import org.picocontainer.injectors.ConstructorInjection;
014    import org.picocontainer.injectors.ConstructorInjector;
015    import org.picocontainer.behaviors.OptInCaching;
016    import org.picocontainer.DefaultPicoContainer;
017    import org.picocontainer.ComponentFactory;
018    import org.picocontainer.Characteristics;
019    import org.picocontainer.ComponentAdapter;
020    import org.picocontainer.monitors.NullComponentMonitor;
021    import org.picocontainer.lifecycle.NullLifecycleStrategy;
022    import org.picocontainer.adapters.InstanceAdapter;
023    
024    /**
025     * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
026     */
027    public class OptInCachingTestCase extends AbstractComponentFactoryTestCase {
028    
029        protected ComponentFactory createComponentFactory() {
030            return new OptInCaching().wrap(new ConstructorInjection());
031        }
032    
033        public void testAddComponentDoesNotUseCachingBehaviorByDefault() {
034            DefaultPicoContainer pico =
035                new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()));
036            pico.addComponent("foo", String.class);
037            ComponentAdapter foo = pico.getComponentAdapter("foo");
038            assertEquals(ConstructorInjector.class, foo.getClass());
039        }
040    
041        public void testAddComponentUsesImplementationHidingBehaviorWithRedundantCacheProperty() {
042            DefaultPicoContainer pico =
043                new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()));
044            pico.change(Characteristics.CACHE).addComponent("foo", String.class);
045            ComponentAdapter foo = pico.getComponentAdapter("foo");
046            assertEquals(Cached.class, foo.getClass());
047            assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
048        }
049    
050        public void testAddComponentNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
051            DefaultPicoContainer pico =
052                new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()));
053            pico.change(Characteristics.NO_CACHE).addComponent("foo", String.class);
054            ComponentAdapter foo = pico.getComponentAdapter("foo");
055            assertEquals(ConstructorInjector.class, foo.getClass());
056        }
057    
058        public void testAddAdapterUsesDoesNotUseCachingBehaviorByDefault() {
059            DefaultPicoContainer pico =
060                new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()));
061            pico.addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
062            ComponentAdapter foo = pico.getComponentAdapter("foo");
063            assertEquals(InstanceAdapter.class, foo.getClass());
064        }
065    
066        public void testAddAdapterUsesCachingBehaviorWithHideImplProperty() {
067            DefaultPicoContainer pico =
068                new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()));
069            pico.change(Characteristics.CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
070            ComponentAdapter foo = pico.getComponentAdapter("foo");
071            assertEquals(Cached.class, foo.getClass());
072            assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
073        }
074    
075        public void testAddAdapterNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
076            DefaultPicoContainer pico =
077                new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()));
078            pico.change(Characteristics.NO_CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
079            ComponentAdapter foo = pico.getComponentAdapter("foo");
080            assertEquals(InstanceAdapter.class, foo.getClass());
081        }
082    
083    
084    
085    }