| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
|
| 31 |
|
|
| 32 |
|
@author |
| 33 |
|
@version |
| 34 |
|
|
| 35 |
|
public |
|
|
|
| 100% |
Uncovered Elements: 0 (38) |
Complexity: 4 |
Complexity Density: 0.12 |
|
| 36 |
|
class TestAbstractHandler |
| 37 |
|
extends TestCase |
| 38 |
|
{ |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 45 |
1
|
public void ... |
| 46 |
|
testChainSuccessfullyInvoked() |
| 47 |
|
{ |
| 48 |
1
|
theLogger.debug("Running assertions ... "); |
| 49 |
|
|
| 50 |
|
|
| 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 |
|
|
| 64 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
| 65 |
1
|
public void ... |
| 66 |
|
testChainInvokedInCorrectOrder() |
| 67 |
|
{ |
| 68 |
1
|
theLogger.debug("Running assertions ... "); |
| 69 |
|
|
| 70 |
|
|
| 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 |
|
|
| 85 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 86 |
2
|
@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 |
|
|
| 97 |
2
|
TestChainBasicElement.resetCounter(); |
| 98 |
|
|
| 99 |
2
|
fixture(); |
| 100 |
|
|
| 101 |
2
|
theLogger.debug( |
| 102 |
|
"Completed ChainOfResponsibility test fixture."); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
|
| 111 |
2
|
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 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
private static |
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 6 |
Complexity Density: 0.86 |
|
| 142 |
|
class TestChainBasicElement |
| 143 |
|
extends AbstractHandler<NullRequest> |
| 144 |
|
{ |
| 145 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 146 |
8
|
public... |
| 147 |
|
TestChainBasicElement() |
| 148 |
|
{ |
| 149 |
8
|
super(); |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 153 |
2
|
public... |
| 154 |
|
TestChainBasicElement(TestChainBasicElement aSuccessor) |
| 155 |
|
{ |
| 156 |
2
|
super(aSuccessor); |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 163 |
2
|
public static void ... |
| 164 |
|
resetCounter() |
| 165 |
|
{ |
| 166 |
2
|
TestChainBasicElement.theCounter = 0; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 170 |
8
|
@Override... |
| 171 |
|
public void |
| 172 |
|
handle(NullRequest aRequest) |
| 173 |
|
{ |
| 174 |
8
|
super.handle(aRequest); |
| 175 |
|
|
| 176 |
|
|
| 177 |
8
|
theIndex = ++theCounter; |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 181 |
5
|
public final boolean... |
| 182 |
|
isCalled() |
| 183 |
|
{ |
| 184 |
5
|
return theIndex != -1; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
@return |
| 190 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 191 |
4
|
public final int ... |
| 192 |
|
getIndex() |
| 193 |
|
{ |
| 194 |
4
|
return theIndex; |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
private static int theCounter = 0; |
| 201 |
|
private int theIndex = -1; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
private static |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 209 |
|
class TestCanNOTHandleChainBasicElement |
| 210 |
|
extends TestChainBasicElement |
| 211 |
|
{ |
| 212 |
|
|
| 213 |
|
|
| 214 |
|
@inheritDoc |
| 215 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 216 |
2
|
@Override... |
| 217 |
|
public boolean |
| 218 |
|
canHandle(NullRequest aRequest) |
| 219 |
|
throws IllegalArgumentException |
| 220 |
|
{ |
| 221 |
2
|
return false; |
| 222 |
|
} |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 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 |
|
|
| 239 |
|
|
| 240 |
|
private final Logger theLogger = LoggerFactory.getLogger(this.getClass()); |
| 241 |
|
} |