1 package org.neo.swarm.core.container.pico;
2
3 import java.util.Collection;
4
5 import junit.framework.TestCase;
6
7 import org.easymock.MockControl;
8 import org.neo.swarm.ApplicationContext;
9 import org.neo.swarm.PretendAppContext;
10 import org.neo.swarm.core.aop.AspectContainer;
11 import org.neo.swarm.core.aop.silc.SilcAspectContainer;
12 import org.neo.swarm.core.container.CreationType;
13 import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
14
15 /***
16 * @author damiang
17 */
18 public class PicoComponentContainerTest extends TestCase {
19 private PicoComponentContainer container;
20 private SilcComponentAdapterFactory adapterFactoryMock;
21 private MockControl control;
22
23
24 protected void setUp() throws Exception {
25 control = MockControl.createStrictControl(SilcComponentAdapterFactory.class);
26 adapterFactoryMock = (SilcComponentAdapterFactory) control.getMock();
27 container = new PicoComponentContainer(adapterFactoryMock);
28 }
29
30 public void testCreationTypeSingleCreatesCachingComponentAdapter() {
31 String key = "key";
32 Class impl = Object.class;
33 recordMocks();
34 {
35 control.expectAndReturn(adapterFactoryMock.createCachingComponentAdapter(key, impl), new ConstructorInjectionComponentAdapter(key, impl));
36 }
37
38 replayMocks();
39 {
40 container.registerComponent(key, impl, CreationType.SINGLE);
41 }
42
43 verifyMocks();
44 {
45 }
46
47 }
48
49 public void testCreationTypeMultiCreatesConstructorComponentAdapter() {
50 String key = "key";
51 Class impl = Object.class;
52 recordMocks();
53 {
54 control.expectAndReturn(adapterFactoryMock.createConstructorComponentAdapter(key, impl), new ConstructorInjectionComponentAdapter(key, impl));
55 }
56
57 replayMocks();
58 {
59 container.registerComponent(key, impl, CreationType.MULTI);
60 }
61
62 verifyMocks();
63 {
64 }
65
66 }
67
68
69 public void testShouldBeAbleToRetrieveAllComponentsOfAParticularType() {
70 container = new PicoComponentContainer(new SilcComponentAdapterFactoryImpl(new SilcAspectContainer()));
71
72 container.registerComponent("p1", PretendAppContext.class, CreationType.SINGLE);
73 container.registerComponent("p2", PretendAppContext.class, CreationType.SINGLE);
74 container.registerComponent("p3", PretendAppContext.class, CreationType.SINGLE);
75 container.registerComponent("p4", SilcAspectContainer.class, CreationType.SINGLE);
76 container.registerComponent("p5", SilcAspectContainer.class, CreationType.SINGLE);
77
78 Collection c = container.getComponentsOfType(ApplicationContext.class);
79 Collection c1 = container.getComponentsOfType(AspectContainer.class);
80 assertEquals(3,c.size());
81 assertEquals(2, c1.size());
82 }
83
84
85
86 private void recordMocks() {
87 control.reset();
88 }
89
90 private void replayMocks() {
91 control.replay();
92 }
93
94 private void verifyMocks() {
95 control.verify();
96 }
97 }