| 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.visitor; |
| 22 |
|
|
| 23 |
|
import java.util.*; |
| 24 |
|
|
| 25 |
|
import junit.framework.*; |
| 26 |
|
|
| 27 |
|
import org.perfectjpattern.core.api.behavioral.visitor.*; |
| 28 |
|
import org.slf4j.*; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
@link |
| 33 |
|
|
| 34 |
|
@see |
| 35 |
|
|
| 36 |
|
@author |
| 37 |
|
@version |
| 38 |
|
|
| 39 |
|
@SuppressWarnings("unchecked") |
| 40 |
|
public |
|
|
|
| 100% |
Uncovered Elements: 0 (40) |
Complexity: 7 |
Complexity Density: 0.21 |
|
| 41 |
|
class TestVisitor |
| 42 |
|
extends TestCase |
| 43 |
|
{ |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1
PASS
|
|
| 50 |
1
|
public void... |
| 51 |
|
testSimpleUsage() |
| 52 |
|
{ |
| 53 |
1
|
IVisitor<BlackNode> mySimpleVisitor = new SimpleVisitor(); |
| 54 |
|
|
| 55 |
1
|
BlackNode myBlackNode = theBlackNode; |
| 56 |
|
|
| 57 |
|
|
| 58 |
1
|
mySimpleVisitor.visit(myBlackNode); |
| 59 |
1
|
mySimpleVisitor.visit(myBlackNode); |
| 60 |
|
|
| 61 |
1
|
theLogger.debug("Running assertions ... "); |
| 62 |
|
|
| 63 |
1
|
assertEquals("Simple visit implementation did not work as expected.", |
| 64 |
|
BLACK_NODE_NAME, ((SimpleVisitor) mySimpleVisitor). |
| 65 |
|
getElementName()); |
| 66 |
|
|
| 67 |
1
|
assertEquals("The two calls were not executed.", 2, |
| 68 |
|
((SimpleVisitor) mySimpleVisitor).getCallsCounter()); |
| 69 |
|
|
| 70 |
1
|
theLogger.debug("Completed"); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
| 76 |
1
|
public void ... |
| 77 |
|
testVisitorA() |
| 78 |
|
{ |
| 79 |
1
|
theLogger.debug("Running assertions ... "); |
| 80 |
|
|
| 81 |
1
|
assertTrue("VisitorA visitRedNode was not successfully called.", |
| 82 |
|
theVisitorA.isCalled()); |
| 83 |
|
|
| 84 |
1
|
assertEquals("VisitorA visited incorrect node.", RED_NODE_NAME, |
| 85 |
|
theVisitorA.getElementName()); |
| 86 |
|
|
| 87 |
1
|
theLogger.debug("Completed"); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
| 94 |
1
|
public void ... |
| 95 |
|
testVisitorB() |
| 96 |
|
{ |
| 97 |
1
|
theLogger.debug("Running assertions ... "); |
| 98 |
|
|
| 99 |
1
|
assertTrue("VisitorB's visitBlackNode was not successfully called.", |
| 100 |
|
theVisitorB.isCalled()); |
| 101 |
|
|
| 102 |
1
|
assertEquals("VisitorB visited incorrect node.", BLACK_NODE_NAME, |
| 103 |
|
theVisitorB.getElementName()); |
| 104 |
|
|
| 105 |
1
|
theLogger.debug("Completed"); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
| 112 |
1
|
public void ... |
| 113 |
|
testVisitorC() |
| 114 |
|
{ |
| 115 |
1
|
theLogger.debug("Running assertions ... "); |
| 116 |
|
|
| 117 |
1
|
assertTrue("VisitorC visitBlackNode was not successfully called.", |
| 118 |
|
theVisitorC.isCalledBlack()); |
| 119 |
|
|
| 120 |
1
|
assertTrue("VisitorC visitRedNode was not successfully called.", |
| 121 |
|
theVisitorC.isCalledRed()); |
| 122 |
|
|
| 123 |
1
|
assertEquals("VisitorC visited incorrect node.", BLACK_NODE_NAME, |
| 124 |
|
theVisitorC.getBlackElementName()); |
| 125 |
|
|
| 126 |
1
|
assertEquals("VisitorC visited incorrect node.", RED_NODE_NAME, |
| 127 |
|
theVisitorC.getRedElementName()); |
| 128 |
|
|
| 129 |
1
|
theLogger.debug("Completed"); |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 135 |
4
|
@Override... |
| 136 |
|
protected void |
| 137 |
|
setUp() |
| 138 |
|
throws Exception |
| 139 |
|
{ |
| 140 |
4
|
super.setUp(); |
| 141 |
|
|
| 142 |
4
|
theLogger.debug("Creating Visitor pattern test fixture ... "); |
| 143 |
|
|
| 144 |
|
|
| 145 |
4
|
fixture(); |
| 146 |
|
|
| 147 |
4
|
theLogger.debug("Completed Visitor pattern test fixture."); |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 156 |
4
|
private void ... |
| 157 |
|
fixture() |
| 158 |
|
{ |
| 159 |
4
|
theLogger.debug("Step #1 := Create a Collection of Visitors"); |
| 160 |
|
|
| 161 |
4
|
Collection<IVisitor> myVisitors = Arrays.asList(new IVisitor[] |
| 162 |
|
{ |
| 163 |
|
theVisitorA, theVisitorB |
| 164 |
|
}); |
| 165 |
|
|
| 166 |
4
|
theLogger.debug("Step #2 := Have every visitor visit every element."); |
| 167 |
4
|
for (IVisitor myVisitor : myVisitors) |
| 168 |
|
{ |
| 169 |
8
|
AbstractVisitor.reusableVisit(myVisitor, theBlackNode, theRedNode); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
4
|
theLogger.debug("Step #3 := Have visitor C visit every element."); |
| 173 |
4
|
theVisitorC.visit(theBlackNode); |
| 174 |
4
|
theVisitorC.visit(theRedNode); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
private static |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 184 |
|
class RedNode |
| 185 |
|
implements IElement |
| 186 |
|
{ |
| 187 |
|
|
| 188 |
|
|
| 189 |
|
@return |
| 190 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 191 |
8
|
public final String ... |
| 192 |
|
getName() |
| 193 |
|
{ |
| 194 |
8
|
return RED_NODE_NAME; |
| 195 |
|
} |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 202 |
|
private static class BlackNode |
| 203 |
|
implements IElement |
| 204 |
|
{ |
| 205 |
|
|
| 206 |
|
|
| 207 |
|
@return |
| 208 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 209 |
10
|
public final String ... |
| 210 |
|
getName() |
| 211 |
|
{ |
| 212 |
10
|
return BLACK_NODE_NAME; |
| 213 |
|
} |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
public static |
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 218 |
|
class SimpleVisitor |
| 219 |
|
extends AbstractVisitor<BlackNode> |
| 220 |
|
{ |
| 221 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 222 |
2
|
public void ... |
| 223 |
|
visitBlackNode(BlackNode aNode) |
| 224 |
|
{ |
| 225 |
2
|
theCallsCounter++; |
| 226 |
|
|
| 227 |
2
|
theElementName = aNode.getName(); |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
@return |
| 233 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 234 |
1
|
public final int... |
| 235 |
|
getCallsCounter() |
| 236 |
|
{ |
| 237 |
1
|
return theCallsCounter; |
| 238 |
|
} |
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
@return |
| 243 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 244 |
1
|
public final String ... |
| 245 |
|
getElementName() |
| 246 |
|
{ |
| 247 |
1
|
return theElementName; |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
private int theCallsCounter = 0; |
| 254 |
|
private String theElementName = null; |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
public static |
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 262 |
|
class VisitorA<E extends IElement> |
| 263 |
|
extends AbstractVisitor<E> |
| 264 |
|
{ |
| 265 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 266 |
4
|
public void ... |
| 267 |
|
visitRedNode(RedNode aNode) |
| 268 |
|
{ |
| 269 |
4
|
theCalled = true; |
| 270 |
4
|
theElementName = aNode.getName(); |
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
@return |
| 276 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 277 |
1
|
public final boolean ... |
| 278 |
|
isCalled() |
| 279 |
|
{ |
| 280 |
1
|
return theCalled; |
| 281 |
|
} |
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
@return |
| 286 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 287 |
1
|
public final String ... |
| 288 |
|
getElementName() |
| 289 |
|
{ |
| 290 |
1
|
return theElementName; |
| 291 |
|
} |
| 292 |
|
|
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
| 296 |
|
private boolean theCalled = false; |
| 297 |
|
private String theElementName = null; |
| 298 |
|
} |
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
|
| 304 |
|
public static |
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 305 |
|
class VisitorB<E extends IElement> |
| 306 |
|
extends AbstractVisitor<E> |
| 307 |
|
{ |
| 308 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 309 |
4
|
public void ... |
| 310 |
|
visitBlackNode(BlackNode aNode) |
| 311 |
|
{ |
| 312 |
4
|
theCalled = true; |
| 313 |
4
|
theElementName = aNode.getName(); |
| 314 |
|
} |
| 315 |
|
|
| 316 |
|
|
| 317 |
|
|
| 318 |
|
@return |
| 319 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 320 |
1
|
public final boolean ... |
| 321 |
|
isCalled() |
| 322 |
|
{ |
| 323 |
1
|
return theCalled; |
| 324 |
|
} |
| 325 |
|
|
| 326 |
|
|
| 327 |
|
|
| 328 |
|
@return |
| 329 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 330 |
1
|
public final String ... |
| 331 |
|
getElementName() |
| 332 |
|
{ |
| 333 |
1
|
return theElementName; |
| 334 |
|
} |
| 335 |
|
|
| 336 |
|
|
| 337 |
|
|
| 338 |
|
|
| 339 |
|
private boolean theCalled = false; |
| 340 |
|
private String theElementName = null; |
| 341 |
|
} |
| 342 |
|
|
| 343 |
|
|
| 344 |
|
|
| 345 |
|
|
| 346 |
|
|
| 347 |
|
|
| 348 |
|
public static |
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 7 |
Complexity Density: 0.78 |
|
| 349 |
|
class VisitorC<E extends IElement> |
| 350 |
|
implements IVisitor<E> |
| 351 |
|
{ |
| 352 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 353 |
8
|
public void ... |
| 354 |
|
visit(E anElement) |
| 355 |
|
{ |
| 356 |
8
|
AbstractVisitor.reusableVisit(this, anElement); |
| 357 |
|
} |
| 358 |
|
|
| 359 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 360 |
4
|
public void ... |
| 361 |
|
visitRedNode(RedNode aNode) |
| 362 |
|
{ |
| 363 |
4
|
theCalledRed = true; |
| 364 |
4
|
theRedElementName = aNode.getName(); |
| 365 |
|
} |
| 366 |
|
|
| 367 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 368 |
4
|
public void ... |
| 369 |
|
visitBlackNode(BlackNode aNode) |
| 370 |
|
{ |
| 371 |
4
|
theCalledBlack = true; |
| 372 |
4
|
theBlackElementName = aNode.getName(); |
| 373 |
|
} |
| 374 |
|
|
| 375 |
|
|
| 376 |
|
|
| 377 |
|
@return |
| 378 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 379 |
1
|
public final String ... |
| 380 |
|
getBlackElementName() |
| 381 |
|
{ |
| 382 |
1
|
return theBlackElementName; |
| 383 |
|
} |
| 384 |
|
|
| 385 |
|
|
| 386 |
|
|
| 387 |
|
@return |
| 388 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 389 |
1
|
public final boolean ... |
| 390 |
|
isCalledBlack() |
| 391 |
|
{ |
| 392 |
1
|
return theCalledBlack; |
| 393 |
|
} |
| 394 |
|
|
| 395 |
|
|
| 396 |
|
|
| 397 |
|
@return |
| 398 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 399 |
1
|
public final boolean ... |
| 400 |
|
isCalledRed() |
| 401 |
|
{ |
| 402 |
1
|
return theCalledRed; |
| 403 |
|
} |
| 404 |
|
|
| 405 |
|
|
| 406 |
|
|
| 407 |
|
@return |
| 408 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 409 |
1
|
public final String ... |
| 410 |
|
getRedElementName() |
| 411 |
|
{ |
| 412 |
1
|
return theRedElementName; |
| 413 |
|
} |
| 414 |
|
|
| 415 |
|
|
| 416 |
|
|
| 417 |
|
|
| 418 |
|
private boolean theCalledRed = false; |
| 419 |
|
private boolean theCalledBlack = false; |
| 420 |
|
private String theRedElementName = null; |
| 421 |
|
private String theBlackElementName = null; |
| 422 |
|
} |
| 423 |
|
|
| 424 |
|
|
| 425 |
|
|
| 426 |
|
|
| 427 |
|
private static final String BLACK_NODE_NAME = "Black Node"; |
| 428 |
|
private static final String RED_NODE_NAME = "Red Node"; |
| 429 |
|
|
| 430 |
|
|
| 431 |
|
|
| 432 |
|
|
| 433 |
|
private final BlackNode theBlackNode = new BlackNode(); |
| 434 |
|
private final RedNode theRedNode = new RedNode(); |
| 435 |
|
|
| 436 |
|
|
| 437 |
|
|
| 438 |
|
|
| 439 |
|
private final VisitorA theVisitorA = new VisitorA(); |
| 440 |
|
private final VisitorB theVisitorB = new VisitorB(); |
| 441 |
|
private final VisitorC theVisitorC = new VisitorC(); |
| 442 |
|
|
| 443 |
|
|
| 444 |
|
|
| 445 |
|
|
| 446 |
|
private final Logger theLogger = LoggerFactory.getLogger(this.getClass()); |
| 447 |
|
} |