KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > doc > caching > BasicCachingExampleTestCase


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 JavaDoc;
12 import java.util.ArrayList JavaDoc;
13
14 /**
15  * Created by IntelliJ IDEA.
16  * User: paul
17  * Date: Aug 27, 2005
18  * Time: 9:46:23 AM
19  * To change this template use File | Settings | File Templates.
20  */

21 public class BasicCachingExampleTestCase extends TestCase {
22
23     public void testCachingContainerCaches() {
24
25         // START SNIPPET: caching
26
CachingPicoContainer pico = new CachingPicoContainer();
27         pico.registerComponentImplementation(List JavaDoc.class, ArrayList JavaDoc.class);
28         // other resitrations
29

30         Object JavaDoc one = pico.getComponentInstanceOfType(List JavaDoc.class);
31         Object JavaDoc two = pico.getComponentInstanceOfType(List JavaDoc.class);
32
33         assertSame("instances should be the same", one, two);
34         // END SNIPPET: caching
35

36     }
37
38     public void testCachingContainerWithCAFStillCaches() {
39
40         // START SNIPPET: caching2
41
CachingPicoContainer pico = new CachingPicoContainer(new ConstructorInjectionComponentAdapterFactory());
42         pico.registerComponentImplementation(List JavaDoc.class, ArrayList JavaDoc.class);
43         // other resitrations
44

45         Object JavaDoc one = pico.getComponentInstanceOfType(List JavaDoc.class);
46         Object JavaDoc two = pico.getComponentInstanceOfType(List JavaDoc.class);
47
48         assertSame("instances should be the same", one, two);
49         // END SNIPPET: caching2
50

51     }
52
53     public void testDefaulCaching() {
54         // START SNIPPET: default
55
DefaultPicoContainer pico = new DefaultPicoContainer();
56         pico.registerComponentImplementation(List JavaDoc.class, ArrayList JavaDoc.class);
57         // other resitrations
58

59         Object JavaDoc one = pico.getComponentInstanceOfType(List JavaDoc.class);
60         Object JavaDoc two = pico.getComponentInstanceOfType(List JavaDoc.class);
61
62         assertSame("instances should be the same by default", one, two);
63         // END SNIPPET: default
64

65     }
66
67     public void testDefaulCachingtheHardWay() {
68         // START SNIPPET: default2
69
DefaultPicoContainer pico = new DefaultPicoContainer(
70                         new CachingComponentAdapterFactory(new ConstructorInjectionComponentAdapterFactory()));
71         pico.registerComponentImplementation(List JavaDoc.class, ArrayList JavaDoc.class);
72         // other resitrations
73

74         Object JavaDoc one = pico.getComponentInstanceOfType(List JavaDoc.class);
75         Object JavaDoc two = pico.getComponentInstanceOfType(List JavaDoc.class);
76
77         assertSame("instances should be the same", one, two);
78         // END SNIPPET: default2
79

80     }
81
82     public void testDefaultWithCAFNonCaching() {
83
84         // START SNIPPET: default-noncaching
85
DefaultPicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory());
86         pico.registerComponentImplementation(List JavaDoc.class, ArrayList JavaDoc.class);
87         // other resitrations
88

89         Object JavaDoc one = pico.getComponentInstanceOfType(List JavaDoc.class);
90         Object JavaDoc two = pico.getComponentInstanceOfType(List JavaDoc.class);
91
92         assertNotSame("instances should NOT be the same", one, two);
93         // END SNIPPET: default-noncaching
94

95     }
96
97     public void testImplementationHidingCaching() {
98
99         // START SNIPPET: implhiding
100
ImplementationHidingCachingPicoContainer pico = new ImplementationHidingCachingPicoContainer();
101         pico.registerComponentImplementation(List JavaDoc.class, ArrayList JavaDoc.class);
102         // other resitrations
103

104         Object JavaDoc one = pico.getComponentInstanceOfType(List JavaDoc.class);
105         Object JavaDoc two = pico.getComponentInstanceOfType(List JavaDoc.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 JavaDoc) == 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 JavaDoc.class, ArrayList JavaDoc.class);
119         // other resitrations
120

121         Object JavaDoc one = pico.getComponentInstanceOfType(List JavaDoc.class);
122         cca.flush();
123         Object JavaDoc two = pico.getComponentInstanceOfType(List JavaDoc.class);
124
125         assertNotSame("instances should NOT be the same", one, two);
126         // END SNIPPET: caching
127

128     }
129
130
131 }
132
Popular Tags