Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
32   203   10   3.2
0   98   0.31   5
10     1  
2    
 
  TestAbstractParameterlessHandler       Line # 34 26 0% 4 0 100% 1.0
  TestAbstractParameterlessHandler.TestChainBasicElement       Line # 130 6 0% 6 0 100% 1.0
 
  (2)
 
1    //----------------------------------------------------------------------
2    //
3    // PerfectJPattern: "Design patterns are good but components are better!"
4    // TestAbstractHandler.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.chainofresponsibility;
22   
23    import junit.framework.*;
24   
25    import org.slf4j.*;
26   
27    /**
28    * Test Suite for <code>AbstractHandler</code> implementation.
29    *
30    * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
31    * @version $Revision: 1.0 $ $Date: Jun 26, 2007 12:42:39 AM $
32    */
33    public
 
34    class TestAbstractParameterlessHandler
35    extends TestCase
36    {
37    //------------------------------------------------------------------------
38    // public
39    //------------------------------------------------------------------------
40    /**
41    * Test that all handlers are successfully invoked.
42    */
 
43  1 toggle public void
44    testChainSuccessfullyInvoked()
45    {
46  1 theLogger.debug("Running assertions ... ");
47   
48    // Test that every element had chance to execute
49  1 assertTrue("First element was never invoked.", theFirst.isCalled());
50  1 assertTrue("Second element was never invoked.", theSecond.isCalled());
51  1 assertTrue("Third element was never invoked.", theThird.isCalled());
52   
53  1 theLogger.debug("Completed test");
54    }
55   
56    //------------------------------------------------------------------------
57    /**
58    * Test that chain elements were invoked in the correct order.
59    */
 
60  1 toggle public void
61    testChainInvokedInCorrectOrder()
62    {
63  1 theLogger.debug("Running assertions ... ");
64   
65    // Test that the chain has the right order
66  1 assertEquals("First element was not correctly invoked first.",
67    1, theFirst.getIndex());
68  1 assertEquals("Second element was not correctly invoked second.", 2,
69    theSecond.getIndex());
70  1 assertEquals("Third element was not correctly invoked third.", 3,
71    theThird.getIndex());
72   
73  1 theLogger.debug("Completed test");
74    }
75   
76    //------------------------------------------------------------------------
77    // protected
78    //------------------------------------------------------------------------
 
79  2 toggle @Override
80    protected void
81    setUp()
82    throws Exception
83    {
84  2 super.setUp();
85   
86  2 theLogger.debug(
87    "Creating ChainOfResponsibility test fixture ... ");
88   
89    // Resets the static counter
90  2 TestChainBasicElement.resetCounter();
91   
92  2 fixture();
93   
94  2 theLogger.debug(
95    "Completed ChainOfResponsibility test fixture.");
96    }
97   
98    //------------------------------------------------------------------------
99    // private
100    //------------------------------------------------------------------------
101    /**
102    * Provides sample fixture for the Chain of Responsibility Pattern.
103    */
 
104  2 toggle protected void
105    fixture()
106    {
107  2 theLogger.debug("Step #1 := Create handlers");
108  2 theSecond = new TestChainBasicElement();
109  2 theFirst = new TestChainBasicElement(theSecond);
110  2 theThird = new TestChainBasicElement();
111   
112  2 theLogger.debug("Step #2 := Associate all chain handlers");
113  2 theSecond.setSuccessor(theThird);
114   
115  2 theLogger.debug("Step #3 := Modify default IChainStrategy");
116  2 theFirst.setChainStrategy(AllHandleStrategy.getInstance());
117  2 theSecond.setChainStrategy(AllHandleStrategy.getInstance());
118   
119  2 theLogger.debug("Step #4 := Trigger the chain execution");
120  2 theFirst.start();
121    }
122   
123    //------------------------------------------------------------------------
124    // inner classes
125    //------------------------------------------------------------------------
126    /**
127    * Concrete implementation of <code>IHandler</code>.
128    */
129    private static
 
130    class TestChainBasicElement
131    extends AbstractParameterlessHandler
132    {
133    //-------------------------------------------------------------------
 
134  4 toggle public
135    TestChainBasicElement()
136    {
137  4 super();
138    }
139   
140    //-------------------------------------------------------------------
 
141  2 toggle public
142    TestChainBasicElement(TestChainBasicElement aSuccessor)
143    {
144  2 super(aSuccessor);
145    }
146   
147    //-------------------------------------------------------------------
148    /**
149    * Resets the Counter
150    */
 
151  2 toggle public static void
152    resetCounter()
153    {
154  2 TestChainBasicElement.theCounter = 0;
155    }
156   
157    //-------------------------------------------------------------------
 
158  6 toggle public void
159    handle()
160    {
161    // test code here ...
162  6 theIndex = ++theCounter;
163    }
164   
165    //-------------------------------------------------------------------
 
166  3 toggle public final boolean
167    isCalled()
168    {
169  3 return theIndex != -1;
170    }
171   
172    //-------------------------------------------------------------------
173    /**
174    * @return the theIndex
175    */
 
176  3 toggle public final int
177    getIndex()
178    {
179  3 return theIndex;
180    }
181   
182    //-------------------------------------------------------------------
183    // members
184    //-------------------------------------------------------------------
185    private static int theCounter = 0;
186    private int theIndex = -1;
187    }
188   
189    //------------------------------------------------------------------------
190    // members
191    //------------------------------------------------------------------------
192    /**
193    * Chain of Responsibility elements
194    */
195    private TestChainBasicElement theFirst = null;
196    private TestChainBasicElement theSecond = null;
197    private TestChainBasicElement theThird = null;
198   
199    /**
200    * Provides logging services for this class.
201    */
202    private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
203    }