Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
57   447   25   2.38
0   229   0.44   3.43
24     1.04  
7    
 
  TestVisitor       Line # 41 34 0% 7 0 100% 1.0
  TestVisitor.RedNode       Line # 184 1 0% 1 0 100% 1.0
  TestVisitor.BlackNode       Line # 202 1 0% 1 0 100% 1.0
  TestVisitor.SimpleVisitor       Line # 218 4 0% 3 0 100% 1.0
  TestVisitor.VisitorA       Line # 262 4 0% 3 0 100% 1.0
  TestVisitor.VisitorB       Line # 305 4 0% 3 0 100% 1.0
  TestVisitor.VisitorC       Line # 349 9 0% 7 0 100% 1.0
 
  (4)
 
1    //----------------------------------------------------------------------
2    //
3    // PerfectJPattern: "Design patterns are good but components are better!"
4    // TestVisitorPattern.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.visitor;
22   
23    import java.util.*;
24   
25    import junit.framework.*;
26   
27    import org.perfectjpattern.core.api.behavioral.visitor.*;
28    import org.slf4j.*;
29   
30   
31    /**
32    * Test Suite for {@link AbstractVisitor} implementation.
33    *
34    * @see AbstractVisitor
35    *
36    * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
37    * @version $Revision: 1.0 $ $Date: Jul 1, 2007 12:06:36 PM $
38    */
39    @SuppressWarnings("unchecked")
40    public
 
41    class TestVisitor
42    extends TestCase
43    {
44    //------------------------------------------------------------------------
45    // public
46    //------------------------------------------------------------------------
47    /**
48    * Test simple Visitor usage
49    */
 
50  1 toggle public void
51    testSimpleUsage()
52    {
53  1 IVisitor<BlackNode> mySimpleVisitor = new SimpleVisitor();
54   
55  1 BlackNode myBlackNode = theBlackNode;
56   
57    // visit two times
58  1 mySimpleVisitor.visit(myBlackNode);
59  1 mySimpleVisitor.visit(myBlackNode);
60   
61  1 theLogger.debug("Running assertions ... ");
62   
63  1 assertEquals("Simple visit implementation did not work as expected.",
64    BLACK_NODE_NAME, ((SimpleVisitor) mySimpleVisitor).
65    getElementName());
66   
67  1 assertEquals("The two calls were not executed.", 2,
68    ((SimpleVisitor) mySimpleVisitor).getCallsCounter());
69   
70  1 theLogger.debug("Completed");
71    }
72   
73    /**
74    * Test that VisitorA properly visited the required nodes.
75    */
 
76  1 toggle public void
77    testVisitorA()
78    {
79  1 theLogger.debug("Running assertions ... ");
80   
81  1 assertTrue("VisitorA visitRedNode was not successfully called.",
82    theVisitorA.isCalled());
83   
84  1 assertEquals("VisitorA visited incorrect node.", RED_NODE_NAME,
85    theVisitorA.getElementName());
86   
87  1 theLogger.debug("Completed");
88    }
89   
90    //------------------------------------------------------------------------
91    /**
92    * Test that VisitorB properly visited the required nodes.
93    */
 
94  1 toggle public void
95    testVisitorB()
96    {
97  1 theLogger.debug("Running assertions ... ");
98   
99  1 assertTrue("VisitorB's visitBlackNode was not successfully called.",
100    theVisitorB.isCalled());
101   
102  1 assertEquals("VisitorB visited incorrect node.", BLACK_NODE_NAME,
103    theVisitorB.getElementName());
104   
105  1 theLogger.debug("Completed");
106    }
107   
108    //------------------------------------------------------------------------
109    /**
110    * Test that VisitorC properly visited the required nodes.
111    */
 
112  1 toggle public void
113    testVisitorC()
114    {
115  1 theLogger.debug("Running assertions ... ");
116   
117  1 assertTrue("VisitorC visitBlackNode was not successfully called.",
118    theVisitorC.isCalledBlack());
119   
120  1 assertTrue("VisitorC visitRedNode was not successfully called.",
121    theVisitorC.isCalledRed());
122   
123  1 assertEquals("VisitorC visited incorrect node.", BLACK_NODE_NAME,
124    theVisitorC.getBlackElementName());
125   
126  1 assertEquals("VisitorC visited incorrect node.", RED_NODE_NAME,
127    theVisitorC.getRedElementName());
128   
129  1 theLogger.debug("Completed");
130    }
131   
132    //------------------------------------------------------------------------
133    // protected
134    //------------------------------------------------------------------------
 
135  4 toggle @Override
136    protected void
137    setUp()
138    throws Exception
139    {
140  4 super.setUp();
141   
142  4 theLogger.debug("Creating Visitor pattern test fixture ... ");
143   
144    // fixture for the usage of Visitor Pattern
145  4 fixture();
146   
147  4 theLogger.debug("Completed Visitor pattern test fixture.");
148    }
149   
150    //------------------------------------------------------------------------
151    // private
152    //------------------------------------------------------------------------
153    /**
154    * Provides example usage of the Visitor Pattern.
155    */
 
156  4 toggle private void
157    fixture()
158    {
159  4 theLogger.debug("Step #1 := Create a Collection of Visitors");
160   
161  4 Collection<IVisitor> myVisitors = Arrays.asList(new IVisitor[]
162    {
163    theVisitorA, theVisitorB
164    });
165   
166  4 theLogger.debug("Step #2 := Have every visitor visit every element.");
167  4 for (IVisitor myVisitor : myVisitors)
168    {
169  8 AbstractVisitor.reusableVisit(myVisitor, theBlackNode, theRedNode);
170    }
171   
172  4 theLogger.debug("Step #3 := Have visitor C visit every element.");
173  4 theVisitorC.visit(theBlackNode);
174  4 theVisitorC.visit(theRedNode);
175    }
176   
177    //------------------------------------------------------------------------
178    // inner classes
179    //------------------------------------------------------------------------
180    /**
181    * Test Element that extends the AbstractElement base class.
182    */
183    private static
 
184    class RedNode
185    implements IElement
186    {
187    //--------------------------------------------------------------------
188    /**
189    * @return the myName
190    */
 
191  8 toggle public final String
192    getName()
193    {
194  8 return RED_NODE_NAME;
195    }
196    }
197   
198    //------------------------------------------------------------------------
199    /**
200    * Test Element that extends the AbstractElement base class.
201    */
 
202    private static class BlackNode
203    implements IElement
204    {
205    //--------------------------------------------------------------------
206    /**
207    * @return the myName
208    */
 
209  10 toggle public final String
210    getName()
211    {
212  10 return BLACK_NODE_NAME;
213    }
214    }
215   
216    //-------------------------------------------------------------------------
217    public static
 
218    class SimpleVisitor
219    extends AbstractVisitor<BlackNode>
220    {
221    //---------------------------------------------------------------------
 
222  2 toggle public void
223    visitBlackNode(BlackNode aNode)
224    {
225  2 theCallsCounter++;
226   
227  2 theElementName = aNode.getName();
228    }
229   
230    //---------------------------------------------------------------------
231    /**
232    * @return the Calls Counter
233    */
 
234  1 toggle public final int
235    getCallsCounter()
236    {
237  1 return theCallsCounter;
238    }
239   
240    //---------------------------------------------------------------------
241    /**
242    * @return the elementName
243    */
 
244  1 toggle public final String
245    getElementName()
246    {
247  1 return theElementName;
248    }
249   
250    //---------------------------------------------------------------------
251    // members
252    //---------------------------------------------------------------------
253    private int theCallsCounter = 0;
254    private String theElementName = null;
255    }
256   
257    //-------------------------------------------------------------------------
258    /**
259    * Test Visitor that extends the AbstractVisitor base class.
260    */
261    public static
 
262    class VisitorA<E extends IElement>
263    extends AbstractVisitor<E>
264    {
265    //---------------------------------------------------------------------
 
266  4 toggle public void
267    visitRedNode(RedNode aNode)
268    {
269  4 theCalled = true;
270  4 theElementName = aNode.getName();
271    }
272   
273    //---------------------------------------------------------------------
274    /**
275    * @return the Called
276    */
 
277  1 toggle public final boolean
278    isCalled()
279    {
280  1 return theCalled;
281    }
282   
283    //---------------------------------------------------------------------
284    /**
285    * @return the elementName
286    */
 
287  1 toggle public final String
288    getElementName()
289    {
290  1 return theElementName;
291    }
292   
293    //---------------------------------------------------------------------
294    // members
295    //---------------------------------------------------------------------
296    private boolean theCalled = false;
297    private String theElementName = null;
298    }
299   
300    //------------------------------------------------------------------------
301    /**
302    * Test Visitor that extends the AbstractVisitor base class.
303    */
304    public static
 
305    class VisitorB<E extends IElement>
306    extends AbstractVisitor<E>
307    {
308    //--------------------------------------------------------------------
 
309  4 toggle public void
310    visitBlackNode(BlackNode aNode)
311    {
312  4 theCalled = true;
313  4 theElementName = aNode.getName();
314    }
315   
316    //--------------------------------------------------------------------
317    /**
318    * @return the Called
319    */
 
320  1 toggle public final boolean
321    isCalled()
322    {
323  1 return theCalled;
324    }
325   
326    //--------------------------------------------------------------------
327    /**
328    * @return the elementName
329    */
 
330  1 toggle public final String
331    getElementName()
332    {
333  1 return theElementName;
334    }
335   
336    //--------------------------------------------------------------------
337    // members
338    //--------------------------------------------------------------------
339    private boolean theCalled = false;
340    private String theElementName = null;
341    }
342   
343    //------------------------------------------------------------------------
344    /**
345    * Test Visitor that implements the IVisitor interface, and delegates core
346    * visit method to the AbstractVisitor's static implementation.
347    */
348    public static
 
349    class VisitorC<E extends IElement>
350    implements IVisitor<E>
351    {
352    //--------------------------------------------------------------------
 
353  8 toggle public void
354    visit(E anElement)
355    {
356  8 AbstractVisitor.reusableVisit(this, anElement);
357    }
358   
359    //--------------------------------------------------------------------
 
360  4 toggle public void
361    visitRedNode(RedNode aNode)
362    {
363  4 theCalledRed = true;
364  4 theRedElementName = aNode.getName();
365    }
366   
367    //--------------------------------------------------------------------
 
368  4 toggle public void
369    visitBlackNode(BlackNode aNode)
370    {
371  4 theCalledBlack = true;
372  4 theBlackElementName = aNode.getName();
373    }
374   
375    //--------------------------------------------------------------------
376    /**
377    * @return the blackElementName
378    */
 
379  1 toggle public final String
380    getBlackElementName()
381    {
382  1 return theBlackElementName;
383    }
384   
385    //--------------------------------------------------------------------
386    /**
387    * @return the CalledBlack
388    */
 
389  1 toggle public final boolean
390    isCalledBlack()
391    {
392  1 return theCalledBlack;
393    }
394   
395    //--------------------------------------------------------------------
396    /**
397    * @return the CalledRed
398    */
 
399  1 toggle public final boolean
400    isCalledRed()
401    {
402  1 return theCalledRed;
403    }
404   
405    //--------------------------------------------------------------------
406    /**
407    * @return the redElementName
408    */
 
409  1 toggle public final String
410    getRedElementName()
411    {
412  1 return theRedElementName;
413    }
414   
415    //--------------------------------------------------------------------
416    // members
417    //--------------------------------------------------------------------
418    private boolean theCalledRed = false;
419    private boolean theCalledBlack = false;
420    private String theRedElementName = null;
421    private String theBlackElementName = null;
422    }
423   
424    //------------------------------------------------------------------------
425    // members
426    //------------------------------------------------------------------------
427    private static final String BLACK_NODE_NAME = "Black Node";
428    private static final String RED_NODE_NAME = "Red Node";
429   
430    /**
431    * Element components
432    */
433    private final BlackNode theBlackNode = new BlackNode();
434    private final RedNode theRedNode = new RedNode();
435   
436    /**
437    * Visitor components
438    */
439    private final VisitorA theVisitorA = new VisitorA();
440    private final VisitorB theVisitorB = new VisitorB();
441    private final VisitorC theVisitorC = new VisitorC();
442   
443    /**
444    * Provides logging services for this class.
445    */
446    private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
447    }