001 package org.picocontainer.doc.caching;
002
003 import junit.framework.TestCase;
004 import org.picocontainer.alternatives.CachingPicoContainer;
005 import org.picocontainer.alternatives.ImplementationHidingCachingPicoContainer;
006 import org.picocontainer.defaults.DefaultPicoContainer;
007 import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
008 import org.picocontainer.defaults.CachingComponentAdapter;
009 import org.picocontainer.defaults.CachingComponentAdapterFactory;
010
011 import java.util.List;
012 import java.util.ArrayList;
013
014 /**
015 * Created by IntelliJ IDEA.
016 * User: paul
017 * Date: Aug 27, 2005
018 * Time: 9:46:23 AM
019 * To change this template use File | Settings | File Templates.
020 */
021 public class BasicCachingExampleTestCase extends TestCase {
022
023 public void testCachingContainerCaches() {
024
025 // START SNIPPET: caching
026 CachingPicoContainer pico = new CachingPicoContainer();
027 pico.registerComponentImplementation(List.class, ArrayList.class);
028 // other resitrations
029
030 Object one = pico.getComponentInstanceOfType(List.class);
031 Object two = pico.getComponentInstanceOfType(List.class);
032
033 assertSame("instances should be the same", one, two);
034 // END SNIPPET: caching
035
036 }
037
038 public void testCachingContainerWithCAFStillCaches() {
039
040 // START SNIPPET: caching2
041 CachingPicoContainer pico = new CachingPicoContainer(new ConstructorInjectionComponentAdapterFactory());
042 pico.registerComponentImplementation(List.class, ArrayList.class);
043 // other resitrations
044
045 Object one = pico.getComponentInstanceOfType(List.class);
046 Object two = pico.getComponentInstanceOfType(List.class);
047
048 assertSame("instances should be the same", one, two);
049 // END SNIPPET: caching2
050
051 }
052
053 public void testDefaulCaching() {
054 // START SNIPPET: default
055 DefaultPicoContainer pico = new DefaultPicoContainer();
056 pico.registerComponentImplementation(List.class, ArrayList.class);
057 // other resitrations
058
059 Object one = pico.getComponentInstanceOfType(List.class);
060 Object two = pico.getComponentInstanceOfType(List.class);
061
062 assertSame("instances should be the same by default", one, two);
063 // END SNIPPET: default
064
065 }
066
067 public void testDefaulCachingtheHardWay() {
068 // START SNIPPET: default2
069 DefaultPicoContainer pico = new DefaultPicoContainer(
070 new CachingComponentAdapterFactory(new ConstructorInjectionComponentAdapterFactory()));
071 pico.registerComponentImplementation(List.class, ArrayList.class);
072 // other resitrations
073
074 Object one = pico.getComponentInstanceOfType(List.class);
075 Object two = pico.getComponentInstanceOfType(List.class);
076
077 assertSame("instances should be the same", one, two);
078 // END SNIPPET: default2
079
080 }
081
082 public void testDefaultWithCAFNonCaching() {
083
084 // START SNIPPET: default-noncaching
085 DefaultPicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory());
086 pico.registerComponentImplementation(List.class, ArrayList.class);
087 // other resitrations
088
089 Object one = pico.getComponentInstanceOfType(List.class);
090 Object two = pico.getComponentInstanceOfType(List.class);
091
092 assertNotSame("instances should NOT be the same", one, two);
093 // END SNIPPET: default-noncaching
094
095 }
096
097 public void testImplementationHidingCaching() {
098
099 // START SNIPPET: implhiding
100 ImplementationHidingCachingPicoContainer pico = new ImplementationHidingCachingPicoContainer();
101 pico.registerComponentImplementation(List.class, ArrayList.class);
102 // other resitrations
103
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 // END SNIPPET: implhiding
112
113 }
114
115 public void testFlushingOfCache() {
116 // START SNIPPET: caching
117 CachingPicoContainer pico = new CachingPicoContainer();
118 CachingComponentAdapter cca = (CachingComponentAdapter) pico.registerComponentImplementation(List.class, ArrayList.class);
119 // other resitrations
120
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 // END SNIPPET: caching
127
128 }
129
130
131 }