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 public void
44 testChainSuccessfullyInvoked()
45 {
46 theLogger.debug("Running assertions ... ");
47
48 // Test that every element had chance to execute
49 assertTrue("First element was never invoked.", theFirst.isCalled());
50 assertTrue("Second element was never invoked.", theSecond.isCalled());
51 assertTrue("Third element was never invoked.", theThird.isCalled());
52
53 theLogger.debug("Completed test");
54 }
55
56 //------------------------------------------------------------------------
57 /**
58 * Test that chain elements were invoked in the correct order.
59 */
60 public void
61 testChainInvokedInCorrectOrder()
62 {
63 theLogger.debug("Running assertions ... ");
64
65 // Test that the chain has the right order
66 assertEquals("First element was not correctly invoked first.",
67 1, theFirst.getIndex());
68 assertEquals("Second element was not correctly invoked second.", 2,
69 theSecond.getIndex());
70 assertEquals("Third element was not correctly invoked third.", 3,
71 theThird.getIndex());
72
73 theLogger.debug("Completed test");
74 }
75
76 //------------------------------------------------------------------------
77 // protected
78 //------------------------------------------------------------------------
79 @Override
80 protected void
81 setUp()
82 throws Exception
83 {
84 super.setUp();
85
86 theLogger.debug(
87 "Creating ChainOfResponsibility test fixture ... ");
88
89 // Resets the static counter
90 TestChainBasicElement.resetCounter();
91
92 fixture();
93
94 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 protected void
105 fixture()
106 {
107 theLogger.debug("Step #1 := Create handlers");
108 theSecond = new TestChainBasicElement();
109 theFirst = new TestChainBasicElement(theSecond);
110 theThird = new TestChainBasicElement();
111
112 theLogger.debug("Step #2 := Associate all chain handlers");
113 theSecond.setSuccessor(theThird);
114
115 theLogger.debug("Step #3 := Modify default IChainStrategy");
116 theFirst.setChainStrategy(AllHandleStrategy.getInstance());
117 theSecond.setChainStrategy(AllHandleStrategy.getInstance());
118
119 theLogger.debug("Step #4 := Trigger the chain execution");
120 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 public
135 TestChainBasicElement()
136 {
137 super();
138 }
139
140 //-------------------------------------------------------------------
141 public
142 TestChainBasicElement(TestChainBasicElement aSuccessor)
143 {
144 super(aSuccessor);
145 }
146
147 //-------------------------------------------------------------------
148 /**
149 * Resets the Counter
150 */
151 public static void
152 resetCounter()
153 {
154 TestChainBasicElement.theCounter = 0;
155 }
156
157 //-------------------------------------------------------------------
158 public void
159 handle()
160 {
161 // test code here ...
162 theIndex = ++theCounter;
163 }
164
165 //-------------------------------------------------------------------
166 public final boolean
167 isCalled()
168 {
169 return theIndex != -1;
170 }
171
172 //-------------------------------------------------------------------
173 /**
174 * @return the theIndex
175 */
176 public final int
177 getIndex()
178 {
179 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 }