1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestCommand.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
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 * Test Suite for {@link Command} and {@link Invoker} implementations
33 *
34 * @see ICommand
35 * @see IInvoker
36 * @see Command
37 * @see Invoker
38 *
39 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
40 * @version $Revision: 1.0 $ $Date: Jun 26, 2007 1:15:58 AM $
41 */
42 // CHECKSTYLE:OFF
43 public
44 class TestCommand
45 extends TestCase
46 // CHECKSTYLE:ON
47 {
48 //------------------------------------------------------------------------
49 // public
50 //------------------------------------------------------------------------
51 /**
52 * Test that Command Pattern lifecycle methods are executed in the
53 * right order.
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 * Test results handling.
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 // create a new Command
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 // ok
112 }
113
114 try
115 {
116 myCommand.getResult();
117 fail("Command implementation did not detect missing IReceiver");
118 }
119 catch (IllegalStateException anException)
120 {
121 // ok
122 }
123
124 // now create a new Receiver
125 IReceiver<Object, Object> myReceiver = new IReceiver<Object, Object>()
126 {
127 //----------------------------------------------------------------
128 public void execute()
129 {
130 // do nothing
131 }
132
133 //----------------------------------------------------------------
134 public Object
135 getResult()
136 {
137 return null;
138 }
139
140 //----------------------------------------------------------------
141 public void setParameter(Object aParameter)
142 {
143 // do nothing
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 // ok
158 }
159
160 theLogger.debug("Completed test");
161 }
162
163 //------------------------------------------------------------------------
164 // protected
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 // clean all existing states
176 theStates.clear();
177
178 // fixture for usage of the Command
179 fixture();
180
181 theLogger.debug("Completed Command pattern test fixture.");
182 }
183
184 //------------------------------------------------------------------------
185 // private
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 // assign command to invoker
203 myInvoker.setCommand(myCommand);
204
205 // assign receiver to command
206 myCommand.setReceiver(myReceiver);
207
208 // create theParameter
209 MyParameter myParameter = new MyParameter();
210
211 // assign theParameter to invoker
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 // Make result available for testing
221 theFinalResult = myResult;
222
223 }
224
225 //------------------------------------------------------------------------
226 // inner classes
227 //------------------------------------------------------------------------
228 /**
229 * Enum type with all possible Command pattern states.
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 * Example test implementation of <code>ICommand</code>.
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 * Example test implementation of <code>IReceiver</code>.
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 * Example test <code>IParameter</code>.
363 */
364 private static
365 class MyParameter
366 {
367 // empty
368 }
369
370 //------------------------------------------------------------------------
371 /**
372 * Example test <code>IResult</code>.
373 */
374 private static
375 class MyResult
376 {
377 // empty
378 }
379
380 //------------------------------------------------------------------------
381 // members
382 //------------------------------------------------------------------------
383 /**
384 * Collection of states.
385 */
386 private static List<State> theStates = new ArrayList<State>();
387
388 /**
389 * Final result
390 */
391 private MyResult theFinalResult = null;
392
393 /**
394 * Provides logging services for this class.
395 */
396 private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
397 }