1 package org.picocontainer.doc.caching; 2 3 import junit.framework.TestCase; 4 import org.picocontainer.alternatives.CachingPicoContainer; 5 import org.picocontainer.alternatives.ImplementationHidingCachingPicoContainer; 6 import org.picocontainer.defaults.DefaultPicoContainer; 7 import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory; 8 import org.picocontainer.defaults.CachingComponentAdapter; 9 import org.picocontainer.defaults.CachingComponentAdapterFactory; 10 11 import java.util.List ; 12 import java.util.ArrayList ; 13 14 21 public class BasicCachingExampleTestCase extends TestCase { 22 23 public void testCachingContainerCaches() { 24 25 CachingPicoContainer pico = new CachingPicoContainer(); 27 pico.registerComponentImplementation(List .class, ArrayList .class); 28 30 Object one = pico.getComponentInstanceOfType(List .class); 31 Object two = pico.getComponentInstanceOfType(List .class); 32 33 assertSame("instances should be the same", one, two); 34 36 } 37 38 public void testCachingContainerWithCAFStillCaches() { 39 40 CachingPicoContainer pico = new CachingPicoContainer(new ConstructorInjectionComponentAdapterFactory()); 42 pico.registerComponentImplementation(List .class, ArrayList .class); 43 45 Object one = pico.getComponentInstanceOfType(List .class); 46 Object two = pico.getComponentInstanceOfType(List .class); 47 48 assertSame("instances should be the same", one, two); 49 51 } 52 53 public void testDefaulCaching() { 54 DefaultPicoContainer pico = new DefaultPicoContainer(); 56 pico.registerComponentImplementation(List .class, ArrayList .class); 57 59 Object one = pico.getComponentInstanceOfType(List .class); 60 Object two = pico.getComponentInstanceOfType(List .class); 61 62 assertSame("instances should be the same by default", one, two); 63 65 } 66 67 public void testDefaulCachingtheHardWay() { 68 DefaultPicoContainer pico = new DefaultPicoContainer( 70 new CachingComponentAdapterFactory(new ConstructorInjectionComponentAdapterFactory())); 71 pico.registerComponentImplementation(List .class, ArrayList .class); 72 74 Object one = pico.getComponentInstanceOfType(List .class); 75 Object two = pico.getComponentInstanceOfType(List .class); 76 77 assertSame("instances should be the same", one, two); 78 80 } 81 82 public void testDefaultWithCAFNonCaching() { 83 84 DefaultPicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); 86 pico.registerComponentImplementation(List .class, ArrayList .class); 87 89 Object one = pico.getComponentInstanceOfType(List .class); 90 Object two = pico.getComponentInstanceOfType(List .class); 91 92 assertNotSame("instances should NOT be the same", one, two); 93 95 } 96 97 public void testImplementationHidingCaching() { 98 99 ImplementationHidingCachingPicoContainer pico = new ImplementationHidingCachingPicoContainer(); 101 pico.registerComponentImplementation(List .class, ArrayList .class); 102 104 Object one = pico.getComponentInstanceOfType(List .class); 105 Object two = pico.getComponentInstanceOfType(List .class); 106 107 assertSame("instances should be the same", one, two); 108 109 assertTrue("should not be castable back to implementation", 110 (one instanceof ArrayList ) == false); 111 113 } 114 115 public void testFlushingOfCache() { 116 CachingPicoContainer pico = new CachingPicoContainer(); 118 CachingComponentAdapter cca = (CachingComponentAdapter) pico.registerComponentImplementation(List .class, ArrayList .class); 119 121 Object one = pico.getComponentInstanceOfType(List .class); 122 cca.flush(); 123 Object two = pico.getComponentInstanceOfType(List .class); 124 125 assertNotSame("instances should NOT be the same", one, two); 126 128 } 129 130 131 } 132 | Popular Tags |