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