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