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