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 public void
46 testChainSuccessfullyInvoked()
47 {
48 theLogger.debug("Running assertions ... ");
49
50 // Test that every element had chance to execute
51 assertTrue("First element was never invoked.", theFirst.isCalled());
52 assertTrue("Second element was never invoked.", theSecond.isCalled());
53 assertTrue("Third element was never invoked.", theThird.isCalled());
54 assertTrue("Fourth element was not to canHandle(...)=false", !theFourth.
55 isCalled());
56 assertTrue("Fifth element was never invoked.", theFifth.isCalled());
57
58 theLogger.debug("Completed test");
59 }
60
61 //------------------------------------------------------------------------
62 /**
63 * Test that chain elements were invoked in the correct order.
64 */
65 public void
66 testChainInvokedInCorrectOrder()
67 {
68 theLogger.debug("Running assertions ... ");
69
70 // Test that the chain has the right order
71 assertEquals("First element was not correctly invoked first.",
72 1, theFirst.getIndex());
73 assertEquals("Second element was not correctly invoked second.", 2,
74 theSecond.getIndex());
75 assertEquals("Third element was not correctly invoked third.", 3,
76 theThird.getIndex());
77 assertEquals("Fifth element was not correctly invoked fourth.", 4,
78 theFifth.getIndex());
79
80 theLogger.debug("Completed test");
81 }
82
83 //------------------------------------------------------------------------
84 // protected
85 //------------------------------------------------------------------------
86 @Override
87 protected void
88 setUp()
89 throws Exception
90 {
91 super.setUp();
92
93 theLogger.debug(
94 "Creating ChainOfResponsibility test fixture ... ");
95
96 // Resets the static counter
97 TestChainBasicElement.resetCounter();
98
99 fixture();
100
101 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 protected void
112 fixture()
113 {
114 theLogger.debug("Step #1 := Create handlers");
115 theSecond = new TestChainBasicElement();
116 theFirst = new TestChainBasicElement(theSecond);
117 theThird = new TestChainBasicElement();
118 theFourth = new TestCanNOTHandleChainBasicElement();
119 theFifth = new TestChainBasicElement();
120
121 theLogger.debug("Step #2 := Associate all chain handlers");
122 theSecond.setSuccessor(theThird);
123 theThird.setSuccessor(theFourth);
124 theFourth.setSuccessor(theFifth);
125
126 theLogger.debug("Step #3 := Modify default IChainStrategy");
127 theFirst.setChainStrategy(AllHandleStrategy.getInstance());
128 theSecond.setChainStrategy(AllHandleStrategy.getInstance());
129 theThird.setChainStrategy(AllHandleStrategy.getInstance());
130
131 theLogger.debug("Step #4 := Trigger the chain execution");
132 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 public
147 TestChainBasicElement()
148 {
149 super();
150 }
151
152 //-------------------------------------------------------------------
153 public
154 TestChainBasicElement(TestChainBasicElement aSuccessor)
155 {
156 super(aSuccessor);
157 }
158
159 //-------------------------------------------------------------------
160 /**
161 * Resets the Counter
162 */
163 public static void
164 resetCounter()
165 {
166 TestChainBasicElement.theCounter = 0;
167 }
168
169 //-------------------------------------------------------------------
170 @Override
171 public void
172 handle(NullRequest aRequest)
173 {
174 super.handle(aRequest);
175
176 // test code here ...
177 theIndex = ++theCounter;
178 }
179
180 //-------------------------------------------------------------------
181 public final boolean
182 isCalled()
183 {
184 return theIndex != -1;
185 }
186
187 //-------------------------------------------------------------------
188 /**
189 * @return the theIndex
190 */
191 public final int
192 getIndex()
193 {
194 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 @Override
217 public boolean
218 canHandle(NullRequest aRequest)
219 throws IllegalArgumentException
220 {
221 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 }