Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
63   397   23   3.15
0   231   0.37   3.33
20     1.15  
6    
 
  TestCommand       Line # 44 45 0% 11 4 92.5% 0.9245283
  TestCommand.State       Line # 232 6 0% 6 0 100% 1.0
  TestCommand.MyCommand       Line # 289 6 0% 3 0 100% 1.0
  TestCommand.MyReceiver       Line # 327 6 0% 3 0 100% 1.0
  TestCommand.MyParameter       Line # 365 0 - 0 0 - -1.0
  TestCommand.MyResult       Line # 375 0 - 0 0 - -1.0
 
  (3)
 
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  1 toggle public void
56    testLifecycleWorks()
57    {
58  1 theLogger.debug("Running assertions ... ");
59   
60  1 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  1 assertEquals("Invalid number of states.", myExpectedStates.length,
72    theStates.size());
73   
74  1 assertEquals("States mismatch ", Arrays.deepToString(myExpectedStates),
75    Arrays.deepToString(theStates.toArray(new State[0])));
76   
77  1 theLogger.debug("Completed test");
78    }
79   
80    //------------------------------------------------------------------------
81    /**
82    * Test results handling.
83    */
 
84  1 toggle public void
85    testResultHandling()
86    {
87  1 theLogger.debug("Running assertions ... ");
88   
89  1 assertNotNull("Receiver final result must not be null",
90    theFinalResult);
91   
92  1 theLogger.debug("Completed test");
93    }
94   
95    //------------------------------------------------------------------------
 
96  1 toggle public void
97    testCommandInvalidStateDetected()
98    {
99  1 theLogger.debug("Running assertions ... ");
100   
101    // create a new Command
102  1 ICommand<Object, Object> myCommand = new Command<Object, Object>();
103   
104  1 try
105    {
106  1 myCommand.execute();
107  0 fail("Command implementation did not detect missing IReceiver");
108    }
109    catch (IllegalStateException anException)
110    {
111    // ok
112    }
113   
114  1 try
115    {
116  1 myCommand.getResult();
117  0 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  1 IReceiver<Object, Object> myReceiver = new IReceiver<Object, Object>()
126    {
127    //----------------------------------------------------------------
 
128  1 toggle public void execute()
129    {
130    // do nothing
131    }
132   
133    //----------------------------------------------------------------
 
134  1 toggle public Object
135    getResult()
136    {
137  1 return null;
138    }
139   
140    //----------------------------------------------------------------
 
141  0 toggle public void setParameter(Object aParameter)
142    {
143    // do nothing
144    }
145    };
146   
147  1 myCommand.setReceiver(myReceiver);
148  1 myCommand.execute();
149   
150  1 try
151    {
152  1 myCommand.getResult();
153  0 fail("Command implementation did not detect null IResult.");
154    }
155    catch (IllegalStateException anException)
156    {
157    // ok
158    }
159   
160  1 theLogger.debug("Completed test");
161    }
162   
163    //------------------------------------------------------------------------
164    // protected
165    //------------------------------------------------------------------------
 
166  3 toggle @Override
167    protected void
168    setUp()
169    throws Exception
170    {
171  3 super.setUp();
172   
173  3 theLogger.debug("Creating Command pattern test fixture ... ");
174   
175    // clean all existing states
176  3 theStates.clear();
177   
178    // fixture for usage of the Command
179  3 fixture();
180   
181  3 theLogger.debug("Completed Command pattern test fixture.");
182    }
183   
184    //------------------------------------------------------------------------
185    // private
186    //------------------------------------------------------------------------
 
187  3 toggle public void
188    fixture()
189    {
190  3 theLogger.debug("Step #1 := Create the Invoker");
191  3 IInvoker<MyParameter, MyResult> myInvoker =
192    new Invoker<MyParameter, MyResult>();
193   
194  3 theLogger.debug("Step #2 := Create the Command");
195  3 ICommand<MyParameter, MyResult> myCommand = new MyCommand();
196   
197  3 theLogger.debug("Step #3 := Create the Receiver");
198  3 IReceiver<MyParameter, MyResult> myReceiver = new MyReceiver();
199   
200  3 theLogger.debug("Step #4 := Wire components");
201   
202    // assign command to invoker
203  3 myInvoker.setCommand(myCommand);
204   
205    // assign receiver to command
206  3 myCommand.setReceiver(myReceiver);
207   
208    // create theParameter
209  3 MyParameter myParameter = new MyParameter();
210   
211    // assign theParameter to invoker
212  3 myInvoker.setParameter(myParameter);
213   
214  3 theLogger.debug("Step #5 := Execute the invoker");
215  3 myInvoker.invoke();
216   
217  3 theLogger.debug("Step #6 := Collect results");
218  3 MyResult myResult = myInvoker.getResult();
219   
220    // Make result available for testing
221  3 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  2 toggle @Override
237    public String toString()
238    {
239  2 return "Command Parameter Assigned";
240    }
241    },
242    RECEIVER_PARAMETER_ASSIGNED
243    {
 
244  2 toggle @Override
245    public String toString()
246    {
247  2 return "Receiver Parameter Assigned";
248    }
249    },
250    RECEIVER_ASSIGNED
251    {
 
252  2 toggle @Override
253    public String toString()
254    {
255  2 return "Receiver Assigned";
256    }
257    },
258    COMMAND_EXECUTED
259    {
 
260  2 toggle @Override
261    public String toString()
262    {
263  2 return "Command Executed";
264    }
265    },
266    RECEIVER_EXECUTED
267    {
 
268  2 toggle @Override
269    public String toString()
270    {
271  2 return "Receiver Executed";
272    }
273    },
274    RESULT_RETRIEVED
275    {
 
276  2 toggle @Override
277    public String toString()
278    {
279  2 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  3 toggle @Override
294    public void
295    setParameter(MyParameter aParameter)
296    {
297  3 TestCommand.theStates.add(State.COMMAND_PARAMETER_ASSIGNED);
298   
299  3 super.setParameter(aParameter);
300    }
301   
302    //--------------------------------------------------------------------
 
303  3 toggle @Override
304    public void
305    setReceiver(IReceiver<MyParameter, MyResult> aReceiver)
306    {
307  3 TestCommand.theStates.add(State.RECEIVER_ASSIGNED);
308   
309  3 super.setReceiver(aReceiver);
310    }
311   
312    //--------------------------------------------------------------------
 
313  3 toggle @Override
314    public void
315    execute()
316    {
317  3 TestCommand.theStates.add(State.COMMAND_EXECUTED);
318  3 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  3 toggle public void
332    execute()
333    {
334  3 setResult(new MyResult());
335   
336  3 TestCommand.theStates.add(State.RECEIVER_EXECUTED);
337    }
338   
339    //--------------------------------------------------------------------
 
340  3 toggle @Override
341    public void
342    setParameter(MyParameter aParameter)
343    {
344  3 super.setParameter(aParameter);
345   
346  3 TestCommand.theStates.add(State.RECEIVER_PARAMETER_ASSIGNED);
347    }
348   
349    //--------------------------------------------------------------------
 
350  3 toggle @Override
351    public MyResult
352    getResult()
353    {
354  3 TestCommand.theStates.add(State.RESULT_RETRIEVED);
355   
356  3 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    }