1 package org.neo.swarm;
2
3 import junit.framework.TestCase;
4 import org.easymock.MockControl;
5 import org.neo.swarm.ApplicationContext;
6 import org.neo.swarm.SwarmApplicationContext;
7 import org.neo.swarm.core.aop.Aspect;
8 import org.neo.swarm.core.aop.AspectContainer;
9 import org.neo.swarm.core.aop.silc.comp.Invocation;
10 import org.neo.swarm.core.aop.silc.comp.StubInvocation;
11 import org.neo.swarm.core.container.ComponentContainer;
12 import org.neo.swarm.core.container.CreationType;
13
14 import java.util.LinkedList;
15 import java.util.Collection;
16
17 /***
18 * @author damiang
19 */
20 public class SwarmAppContextTest extends TestCase {
21
22 private SwarmApplicationContext context;
23 private AspectContainer aspectMock;
24 private MockControl aspectControl;
25 private MockControl containerControl;
26 private ComponentContainer container;
27 public boolean bindingCalled;
28
29 protected void setUp() throws Exception {
30 aspectControl = MockControl.createStrictControl(AspectContainer.class);
31 aspectMock = (AspectContainer) aspectControl.getMock();
32
33 containerControl = MockControl.createStrictControl(ComponentContainer.class);
34 container = (ComponentContainer) containerControl.getMock();
35 context = new SwarmApplicationContext("test", container, aspectMock);
36 }
37
38 public void testShouldAddAspectsToAspectContainer() {
39 Aspect aspect = new ExampleAspect();
40
41 recordMocks();
42 {
43 aspectMock.addApplicationAspect(aspect);
44 }
45
46 replayMocks();
47 {
48 context.addAspect(aspect);
49 }
50
51 verifyMocks();
52 {
53 }
54 }
55
56 public void testShouldRegisterComponentsInComponentContainer() {
57 recordMocks();
58 {
59 container.registerComponent(TestComp1.class, TestComp1Impl.class, CreationType.SINGLE);
60 }
61
62 replayMocks();
63 {
64 context.addCachedComponent(TestComp1.class, TestComp1Impl.class);
65 }
66
67 verifyMocks();
68 {
69 }
70
71 }
72
73 public void testShouldRegisterComponentAsMulti() {
74 recordMocks(); {
75 container.registerComponent(TestComp1.class, TestComp1Impl.class, CreationType.MULTI);
76 }
77
78 replayMocks(); {
79 context.addComponent(TestComp1.class, TestComp1Impl.class);
80 }
81
82 verifyMocks(); {
83
84 }
85 }
86
87 public void testShouldUseSingleCreationTypeWhenCached() {
88 Object[] args = new Object [] {"blah"};
89
90 recordMocks(); {
91 container.registerComponent("blah", TestComp1Impl.class, args, CreationType.SINGLE);
92 }
93
94 replayMocks(); {
95 context.addCachedComponent("blah", TestComp1Impl.class, args);
96 }
97
98 verifyMocks();
99
100 }
101
102 public void testShouldUseMultiCreationTypeWhenNotCached() {
103 Object[] args = new Object [] {"blah"};
104
105 recordMocks(); {
106 container.registerComponent("blah",TestComp1Impl.class, args, CreationType.MULTI);
107 }
108
109 replayMocks(); {
110 context.addComponent("blah", TestComp1Impl.class, args);
111 }
112
113 verifyMocks();
114
115 }
116
117 public void testShouldBeAbleToRetrieveComponents() {
118 recordMocks(); {
119 containerControl.expectAndReturn(container.getComponent("blah"), null);
120 }
121
122 replayMocks(); {
123 context.retrieveComponent("blah");
124 }
125
126 verifyMocks();
127
128 }
129
130 public void testInjectShouldlookupComponentInContainer() throws Exception {
131
132 Object key = "key";
133 Invocation invocation = new StubInvocation("Fred",Fred.class, "returnSomething", null);
134 Fred fred = new FredImpl("fred");
135 recordMocks(); {
136 containerControl.expectAndReturn(container.getComponent(key),fred);
137 }
138
139 replayMocks(); {
140 String retval = (String) context.inject(key, invocation);
141 assertEquals("fred", retval);
142 }
143
144 verifyMocks();
145 }
146
147 public void testInjectShouldLookupComponentsInContainer() throws Exception {
148 Object key = "key";
149 Invocation invocation = new StubInvocation("Fred",Fred.class, "returnSomething", null);
150 Fred fred = new FredImpl("blah");
151 recordMocks(); {
152 containerControl.expectAndReturn(container.getComponent(key),fred);
153 }
154
155 replayMocks(); {
156 String retval = (String) context.inject(key, invocation);
157 assertEquals("blah", retval);
158 }
159
160 verifyMocks();
161 }
162
163 public void testShouldBeAbleToRetrieveAllComponentsOfAParticularType() throws Exception {
164
165 recordMocks(); {
166 containerControl.expectAndReturn(container.getComponentsOfType(ApplicationContext.class), new LinkedList());
167 }
168
169 replayMocks(); {
170 Collection c = context.retrieveComponentsOfType(ApplicationContext.class);
171 assertNotNull(c);
172 }
173
174 verifyMocks();
175
176 }
177
178 public void testThatBindingInterfaceCallsBackOntoComponentWhenAddingPreconstructedComp() throws Exception {
179
180 TestComp2Impl testClass = new TestComp2Impl();
181 bindingCalled = false;
182 recordMocks(); {
183 container.registerComponentInstance("blah", testClass);
184 }
185
186 replayMocks(); {
187 context.addPreConstructedComponent("blah", testClass);
188 assertTrue(bindingCalled);
189 }
190
191 verifyMocks();
192
193 }
194 private void recordMocks() {
195 aspectControl.reset();
196 containerControl.reset();
197 }
198
199 private void replayMocks() {
200 aspectControl.replay();
201 containerControl.replay();
202 }
203
204 private void verifyMocks() {
205 aspectControl.verify();
206 containerControl.verify();
207 }
208
209 interface TestComp1 {
210 void testComp1DoStuff();
211 }
212
213
214 static class TestComp1Impl implements TestComp1 {
215 public void testComp1DoStuff() {
216 }
217 }
218 class TestComp2Impl implements TestComp1, AppContextBinding {
219
220 public void testComp1DoStuff() {
221 }
222
223 public void bind(ApplicationContext context) {
224 bindingCalled = true;
225 }
226 }
227
228 }