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 OptInCachingTestCase extends AbstractComponentFactoryTest { 033 034 protected ComponentFactory createComponentFactory() { 035 return new OptInCaching().wrap(new ConstructorInjection()); 036 } 037 038 @Test public void testAddComponentDoesNotUseCachingBehaviorByDefault() { 039 DefaultPicoContainer pico = 040 new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()), new NullLifecycleStrategy(), new EmptyPicoContainer()); 041 pico.addComponent("foo", String.class); 042 ComponentAdapter foo = pico.getComponentAdapter("foo"); 043 assertEquals(ConstructorInjector.class, foo.getClass()); 044 } 045 046 @Test public void testAddComponentUsesOptinBehaviorWithRedundantCacheProperty() { 047 DefaultPicoContainer pico = 048 new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()), new NullLifecycleStrategy(), new EmptyPicoContainer()); 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 testAddComponentNoesNotUseOptinBehaviorWhenNoCachePropertyIsSpecified() { 056 DefaultPicoContainer pico = 057 new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()), new NullLifecycleStrategy(), new EmptyPicoContainer()); 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 testAddAdapterUsesDoesNotUseCachingBehaviorByDefault() { 064 DefaultPicoContainer pico = 065 new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection())); 066 pico.addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor())); 067 ComponentAdapter foo = pico.getComponentAdapter("foo"); 068 assertEquals(InstanceAdapter.class, foo.getClass()); 069 } 070 071 @Test public void testAddAdapterUsesCachingBehaviorWithHideImplProperty() { 072 DefaultPicoContainer pico = 073 new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection())); 074 pico.change(Characteristics.CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor())); 075 ComponentAdapter foo = pico.getComponentAdapter("foo"); 076 assertEquals(Cached.class, foo.getClass()); 077 assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass()); 078 } 079 080 @Test public void testAddAdapterNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() { 081 DefaultPicoContainer pico = 082 new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection())); 083 pico.change(Characteristics.NO_CACHE).addAdapter(new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor())); 084 ComponentAdapter foo = pico.getComponentAdapter("foo"); 085 assertEquals(InstanceAdapter.class, foo.getClass()); 086 } 087 088 089 090}