1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.perfectjpattern.core.behavioral.command;
22
23 import java.util.*;
24
25 import junit.framework.*;
26
27 import org.perfectjpattern.core.api.behavioral.command.*;
28 import org.slf4j.*;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public
44 class TestCommand
45 extends TestCase
46
47 {
48
49
50
51
52
53
54
55 public void
56 testLifecycleWorks()
57 {
58 theLogger.debug("Running assertions ... ");
59
60 State[] myExpectedStates =
61 new State[]
62 {
63 State.RECEIVER_ASSIGNED,
64 State.COMMAND_PARAMETER_ASSIGNED,
65 State.COMMAND_EXECUTED,
66 State.RECEIVER_PARAMETER_ASSIGNED,
67 State.RECEIVER_EXECUTED,
68 State.RESULT_RETRIEVED
69 };
70
71 assertEquals("Invalid number of states.", myExpectedStates.length,
72 theStates.size());
73
74 assertEquals("States mismatch ", Arrays.deepToString(myExpectedStates),
75 Arrays.deepToString(theStates.toArray(new State[0])));
76
77 theLogger.debug("Completed test");
78 }
79
80
81
82
83
84 public void
85 testResultHandling()
86 {
87 theLogger.debug("Running assertions ... ");
88
89 assertNotNull("Receiver final result must not be null",
90 theFinalResult);
91
92 theLogger.debug("Completed test");
93 }
94
95
96 public void
97 testCommandInvalidStateDetected()
98 {
99 theLogger.debug("Running assertions ... ");
100
101
102 ICommand<Object, Object> myCommand = new Command<Object, Object>();
103
104 try
105 {
106 myCommand.execute();
107 fail("Command implementation did not detect missing IReceiver");
108 }
109 catch (IllegalStateException anException)
110 {
111
112 }
113
114 try
115 {
116 myCommand.getResult();
117 fail("Command implementation did not detect missing IReceiver");
118 }
119 catch (IllegalStateException anException)
120 {
121
122 }
123
124
125 IReceiver<Object, Object> myReceiver = new IReceiver<Object, Object>()
126 {
127
128 public void execute()
129 {
130
131 }
132
133
134 public Object
135 getResult()
136 {
137 return null;
138 }
139
140
141 public void setParameter(Object aParameter)
142 {
143
144 }
145 };
146
147 myCommand.setReceiver(myReceiver);
148 myCommand.execute();
149
150 try
151 {
152 myCommand.getResult();
153 fail("Command implementation did not detect null IResult.");
154 }
155 catch (IllegalStateException anException)
156 {
157
158 }
159
160 theLogger.debug("Completed test");
161 }
162
163
164
165
166 @Override
167 protected void
168 setUp()
169 throws Exception
170 {
171 super.setUp();
172
173 theLogger.debug("Creating Command pattern test fixture ... ");
174
175
176 theStates.clear();
177
178
179 fixture();
180
181 theLogger.debug("Completed Command pattern test fixture.");
182 }
183
184
185
186
187 public void
188 fixture()
189 {
190 theLogger.debug("Step #1 := Create the Invoker");
191 IInvoker<MyParameter, MyResult> myInvoker =
192 new Invoker<MyParameter, MyResult>();
193
194 theLogger.debug("Step #2 := Create the Command");
195 ICommand<MyParameter, MyResult> myCommand = new MyCommand();
196
197 theLogger.debug("Step #3 := Create the Receiver");
198 IReceiver<MyParameter, MyResult> myReceiver = new MyReceiver();
199
200 theLogger.debug("Step #4 := Wire components");
201
202
203 myInvoker.setCommand(myCommand);
204
205
206 myCommand.setReceiver(myReceiver);
207
208
209 MyParameter myParameter = new MyParameter();
210
211
212 myInvoker.setParameter(myParameter);
213
214 theLogger.debug("Step #5 := Execute the invoker");
215 myInvoker.invoke();
216
217 theLogger.debug("Step #6 := Collect results");
218 MyResult myResult = myInvoker.getResult();
219
220
221 theFinalResult = myResult;
222
223 }
224
225
226
227
228
229
230
231 private static
232 enum State
233 {
234 COMMAND_PARAMETER_ASSIGNED
235 {
236 @Override
237 public String toString()
238 {
239 return "Command Parameter Assigned";
240 }
241 },
242 RECEIVER_PARAMETER_ASSIGNED
243 {
244 @Override
245 public String toString()
246 {
247 return "Receiver Parameter Assigned";
248 }
249 },
250 RECEIVER_ASSIGNED
251 {
252 @Override
253 public String toString()
254 {
255 return "Receiver Assigned";
256 }
257 },
258 COMMAND_EXECUTED
259 {
260 @Override
261 public String toString()
262 {
263 return "Command Executed";
264 }
265 },
266 RECEIVER_EXECUTED
267 {
268 @Override
269 public String toString()
270 {
271 return "Receiver Executed";
272 }
273 },
274 RESULT_RETRIEVED
275 {
276 @Override
277 public String toString()
278 {
279 return "Result Retrieved";
280 }
281 };
282 }
283
284
285
286
287
288 private static
289 class MyCommand
290 extends Command<MyParameter, MyResult>
291 {
292
293 @Override
294 public void
295 setParameter(MyParameter aParameter)
296 {
297 TestCommand.theStates.add(State.COMMAND_PARAMETER_ASSIGNED);
298
299 super.setParameter(aParameter);
300 }
301
302
303 @Override
304 public void
305 setReceiver(IReceiver<MyParameter, MyResult> aReceiver)
306 {
307 TestCommand.theStates.add(State.RECEIVER_ASSIGNED);
308
309 super.setReceiver(aReceiver);
310 }
311
312
313 @Override
314 public void
315 execute()
316 {
317 TestCommand.theStates.add(State.COMMAND_EXECUTED);
318 super.execute();
319 }
320 }
321
322
323
324
325
326 private static
327 class MyReceiver
328 extends AbstractReceiver<MyParameter, MyResult>
329 {
330
331 public void
332 execute()
333 {
334 setResult(new MyResult());
335
336 TestCommand.theStates.add(State.RECEIVER_EXECUTED);
337 }
338
339
340 @Override
341 public void
342 setParameter(MyParameter aParameter)
343 {
344 super.setParameter(aParameter);
345
346 TestCommand.theStates.add(State.RECEIVER_PARAMETER_ASSIGNED);
347 }
348
349
350 @Override
351 public MyResult
352 getResult()
353 {
354 TestCommand.theStates.add(State.RESULT_RETRIEVED);
355
356 return super.getResult();
357 }
358 }
359
360
361
362
363
364 private static
365 class MyParameter
366 {
367
368 }
369
370
371
372
373
374 private static
375 class MyResult
376 {
377
378 }
379
380
381
382
383
384
385
386 private static List<State> theStates = new ArrayList<State>();
387
388
389
390
391 private MyResult theFinalResult = null;
392
393
394
395
396 private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
397 }