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.DefaultPicoContainer;
016    import org.picocontainer.ComponentFactory;
017    import org.picocontainer.Characteristics;
018    import org.picocontainer.ComponentAdapter;
019    import org.picocontainer.monitors.NullComponentMonitor;
020    import org.picocontainer.lifecycle.NullLifecycleStrategy;
021    import org.picocontainer.adapters.InstanceAdapter;
022    
023    
024    /**
025     * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
026     */
027    public class CachingTestCase extends AbstractComponentFactoryTestCase {
028    
029        protected ComponentFactory createComponentFactory() {
030            return new Caching().wrap(new ConstructorInjection());
031        }
032    
033        public void testAddComponentUsesCachingBehavior() {
034            DefaultPicoContainer pico =
035                new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
036            pico.addComponent("foo", String.class);
037            ComponentAdapter foo = pico.getComponentAdapter("foo");
038            assertEquals(Cached.class, foo.getClass());
039            assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
040        }
041    
042        public void testAddComponentUsesCachingBehaviorWithRedundantCacheProperty() {
043            DefaultPicoContainer pico =
044                new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
045            pico.change(Characteristics.CACHE).addComponent("foo", String.class);
046            ComponentAdapter foo = pico.getComponentAdapter("foo");
047            assertEquals(Cached.class, foo.getClass());
048            assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
049        }
050    
051        public void testAddComponentNoesNotUseCachingBehaviorWhenNoCachePropertyIsSpecified() {
052            DefaultPicoContainer pico =
053                new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
054            pico.change(Characteristics.NO_CACHE).addComponent("foo", String.class);
055            ComponentAdapter foo = pico.getComponentAdapter("foo");
056            assertEquals(ConstructorInjector.class, foo.getClass());
057        }
058    
059        public void testAddAdapterUsesCachingBehavior() {
060            DefaultPicoContainer pico =
061                new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
062            pico.addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
063            ComponentAdapter foo = pico.getComponentAdapter("foo");
064            assertEquals(Cached.class, foo.getClass());
065            assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
066        }
067    
068        public void testAddAdapterUsesCachingBehaviorWithRedundantCacheProperty() {
069            DefaultPicoContainer pico =
070                new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
071            pico.change(Characteristics.CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
072            ComponentAdapter foo = pico.getComponentAdapter("foo");
073            assertEquals(Cached.class, foo.getClass());
074            assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
075        }
076    
077        public void testAddAdapterNoesNotUseCachingBehaviorWhenNoCachePropertyIsSpecified() {
078            DefaultPicoContainer pico =
079                new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
080            pico.change(Characteristics.NO_CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
081            ComponentAdapter foo = pico.getComponentAdapter("foo");
082            assertEquals(InstanceAdapter.class, foo.getClass());
083        }    
084    
085    
086    }