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 CachingTestCase extends AbstractComponentFactoryTest {
032
033 protected ComponentFactory createComponentFactory() {
034 return new Caching().wrap(new ConstructorInjection());
035 }
036
037 @Test public void testAddComponentUsesCachingBehavior() {
038 DefaultPicoContainer pico =
039 new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
040 pico.addComponent("foo", String.class);
041 ComponentAdapter foo = pico.getComponentAdapter("foo");
042 assertEquals(Cached.class, foo.getClass());
043 assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
044 }
045
046 @Test public void testAddComponentUsesCachingBehaviorWithRedundantCacheProperty() {
047 DefaultPicoContainer pico =
048 new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
049 pico.change(Characteristics.CACHE).addComponent("foo", String.class);
050 ComponentAdapter foo = pico.getComponentAdapter("foo");
051 assertEquals(Cached.class, foo.getClass());
052 assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
053 }
054
055 @Test public void testAddComponentNoesNotUseCachingBehaviorWhenNoCachePropertyIsSpecified() {
056 DefaultPicoContainer pico =
057 new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
058 pico.change(Characteristics.NO_CACHE).addComponent("foo", String.class);
059 ComponentAdapter foo = pico.getComponentAdapter("foo");
060 assertEquals(ConstructorInjector.class, foo.getClass());
061 }
062
063 @Test public void testAddAdapterUsesCachingBehavior() {
064 DefaultPicoContainer pico =
065 new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
066 pico.addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
067 ComponentAdapter foo = pico.getComponentAdapter("foo");
068 assertEquals(Cached.class, foo.getClass());
069 assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
070 }
071
072 @Test public void testAddAdapterUsesCachingBehaviorWithRedundantCacheProperty() {
073 DefaultPicoContainer pico =
074 new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
075 pico.change(Characteristics.CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
076 ComponentAdapter foo = pico.getComponentAdapter("foo");
077 assertEquals(Cached.class, foo.getClass());
078 assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
079 }
080
081 @Test public void testAddAdapterNoesNotUseCachingBehaviorWhenNoCachePropertyIsSpecified() {
082 DefaultPicoContainer pico =
083 new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
084 pico.change(Characteristics.NO_CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
085 ComponentAdapter foo = pico.getComponentAdapter("foo");
086 assertEquals(InstanceAdapter.class, foo.getClass());
087 }
088
089
090 }