1 26 27 package net.sourceforge.groboutils.codecoverage.v2.report; 28 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 36 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil; 37 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData; 38 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule; 39 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogRecord; 40 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord; 41 import net.sourceforge.groboutils.codecoverage.v2.datastore.MarkRecord; 42 import net.sourceforge.groboutils.codecoverage.v2.logger.DefaultChannelLogRecord; 43 44 45 52 public class ClassMarkSetUTest extends TestCase 53 { 54 57 private static final Class THIS_CLASS = ClassMarkSetUTest.class; 58 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 59 60 public ClassMarkSetUTest( String name ) 61 { 62 super( name ); 63 } 64 65 66 69 70 public void testConstructor1() 71 { 72 try 73 { 74 new ClassMarkSet( null, null, null, null ); 75 fail( "Did not throw IllegalArgumentException." ); 76 } 77 catch (IllegalArgumentException ex) 78 { 79 } 81 } 82 83 84 public void testConstructor2() 85 { 86 try 87 { 88 new ClassMarkSet( "a", new String [] { "1", "2" }, 89 new MarkRecord[] { 90 createMarkRecord( 0, "1", "2", 3 ), 91 }, null ); 92 fail( "Did not throw IllegalArgumentException." ); 93 } 94 catch (IllegalArgumentException ex) 95 { 96 } 98 } 99 100 101 public void testConstructor3() 102 { 103 try 104 { 105 new ClassMarkSet( "a", new String [] { "1", "2" }, 106 null, 107 new IChannelLogRecord[] { 108 createChannelLogRecord( "a.M", 0, 0 ), 109 } ); 110 fail( "Did not throw IllegalArgumentException." ); 111 } 112 catch (IllegalArgumentException ex) 113 { 114 } 116 } 117 118 119 public void testConstructor4() 120 { 121 try 122 { 123 new ClassMarkSet( "a", new String [] { "1", "2" }, 124 new MarkRecord[ 1 ], 125 new IChannelLogRecord[ 1 ] ); 126 fail( "Did not throw IllegalArgumentException." ); 127 } 128 catch (IllegalArgumentException ex) 129 { 130 } 132 } 133 134 135 public void testConstructor5() 136 { 137 try 138 { 139 new ClassMarkSet( "a.Name", new String [] { "1", "2" }, 140 processMarks( "a.Name", new MarkRecord[] { 141 createMarkRecord( 0, "1", "2", 3 ), 142 } ), 143 new IChannelLogRecord[ 1 ] ); 144 fail( "Did not throw IllegalArgumentException." ); 145 } 146 catch (IllegalArgumentException ex) 147 { 148 } 150 } 151 152 153 public void testConstructor5a() 154 { 155 try 156 { 157 new ClassMarkSet( "a", new String [] { "1", "2" }, 158 new MarkRecord[] { 159 createMarkRecord( 0, "1", "2", 3 ), 160 }, 161 new IChannelLogRecord[ 1 ] ); 162 fail( "Did not throw IllegalStateException." ); 163 } 164 catch (IllegalStateException ex) 165 { 166 assertTrue( 168 "Did not fail due to process.", 169 ex.getMessage().indexOf( "process" ) >= 0 ); 170 } 171 } 172 173 174 public void testConstructor6() 175 { 176 try 177 { 178 new ClassMarkSet( "a", new String [] { "1", "2" }, 179 new MarkRecord[ 1 ], 180 new IChannelLogRecord[] { 181 createChannelLogRecord( "a.M", 0, 0 ), 182 } ); 183 fail( "Did not throw IllegalArgumentException." ); 184 } 185 catch (IllegalArgumentException ex) 186 { 187 } 189 } 191 192 193 public void testConstructor7() 194 { 195 try 196 { 197 new ClassMarkSet( "a.M", new String [] { "1", "2" }, 198 processMarks( "a.M", new MarkRecord[] { 199 createMarkRecord( 0, "1", "2", 10 ), 200 } ), 201 new IChannelLogRecord[] { 202 createChannelLogRecord( "a.M", 0, 0 ), 203 } ); 204 fail( "Did not throw IllegalArgumentException." ); 205 } 206 catch (IllegalArgumentException iae) 207 { 208 213 } 214 } 215 216 217 public void testConstructor8() 218 { 219 new ClassMarkSet( "a.M", new String [] { "2", "1" }, 220 processMarks( "a.M", new MarkRecord[] { 221 createMarkRecord( 0, "1", "2", 0 ), 222 } ), 223 new IChannelLogRecord[] { 224 createChannelLogRecord( "a.M", 0, 0 ), 225 } ); 226 } 227 228 229 public void testConstructor9() 230 { 231 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 232 createMarkRecord( 0, "1", "m1()V", 3 ), 233 createMarkRecord( 0, "1", "m2()V", 3 ), 234 createMarkRecord( 0, "1", "m2()V", 1 ), 235 } ); 236 try 237 { 238 new ClassMarkSet( "a.M", new String [] { "m1()V", "m2()V" }, 239 mr, new IChannelLogRecord[] { 240 createChannelLogRecord( "a.M", 0, 1 ), 241 } ); 242 fail( "Did not throw IllegalArgumentException." ); 243 } 244 catch (IllegalArgumentException iae) 245 { 246 assertTrue( 247 "Did not mention mark 1 ["+iae.getMessage()+"].", 248 iae.getMessage().indexOf( "mark 1" ) >= 0 ); 249 } 250 } 251 252 253 public void testConstructor10() 254 { 255 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 256 createMarkRecord( 0, "1", "m1()V", 3 ), 257 createMarkRecord( 0, "1", "m2()V", 3 ), 258 createMarkRecord( 0, "1", "m2()V", 1 ), 259 } ); 260 try 261 { 262 new ClassMarkSet( "a.M", new String [] { "m1()V", "m2()V" }, 263 mr, new IChannelLogRecord[] { 264 createChannelLogRecord( "a.M", 0, 1 ), 265 createChannelLogRecord( "a.M", 0, 5 ), 266 } ); 267 fail( "Did not throw IllegalArgumentException." ); 268 } 269 catch (IllegalArgumentException iae) 270 { 271 assertTrue( 272 "Did not mention mark 1 or 5 ["+iae.getMessage()+"].", 273 iae.getMessage().indexOf( "mark 1" ) >= 0 || 274 iae.getMessage().indexOf( "mark 5" ) >= 0 ); 275 } 276 } 277 278 279 public void testConstructor11() 280 { 281 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 282 createMarkRecord( 0, "1", "m1()V", 1 ), 283 createMarkRecord( 0, "1", "m1()V", 2 ), 284 createMarkRecord( 0, "1", "m1()V", 3 ), 285 createMarkRecord( 0, "1", "m2()V", 1 ), 286 createMarkRecord( 0, "1", "m2()V", 2 ), 287 createMarkRecord( 0, "1", "m2()V", 3 ), 288 } ); 289 try 290 { 291 new ClassMarkSet( "a.M", new String [] { "m1()V", "m2()V" }, 292 mr, new IChannelLogRecord[] { 293 createChannelLogRecord( "a.M", 0, 3 ), 294 createChannelLogRecord( "a.M", 0, 4 ), 295 createChannelLogRecord( "a.M", 1, 1 ), 296 createChannelLogRecord( "a.M", 1, 2 ), 297 } ); 298 fail( "Did not throw IllegalArgumentException." ); 299 } 300 catch (IllegalArgumentException iae) 301 { 302 assertTrue( 303 "Did not mention mark 4 ["+iae.getMessage()+"].", 304 iae.getMessage().indexOf( "mark 4" ) >= 0 ); 305 } 306 } 307 308 309 public void testGetMethodSignatures1() 310 { 311 ClassMarkSet cms = new ClassMarkSet( "a.M", 312 new String [] {}, 313 new MarkRecord[0], new IChannelLogRecord[ 0 ] ); 314 String [] methods = cms.getMethodSignatures(); 315 assertNotNull( 316 "Returned null method signature list.", 317 methods ); 318 assertEquals( 319 "Created methods out of nothing.", 320 0, 321 methods.length ); 322 } 323 324 325 public void testGetMethodSignatures1a() 326 { 327 try 328 { 329 ClassMarkSet cms = new ClassMarkSet( 330 "a.M", new String [] { "m1()V", "m2()V" }, 331 new MarkRecord[0], new IChannelLogRecord[] { 332 createChannelLogRecord( "a.M", 0, 1 ), 333 } ); 334 } 335 catch (IllegalArgumentException ex) 336 { 337 345 } 346 } 347 348 349 public void testGetMethodSignatures2() 350 { 351 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 352 createMarkRecord( 0, "1", "m()V", 0 ), 353 } ); 354 ClassMarkSet cms = new ClassMarkSet("a.M", 355 new String [] { "m()V" }, 356 mr, new IChannelLogRecord[] { 357 createChannelLogRecord( "a.M", 0, 0 ), 358 } ); 359 360 String [] methods = cms.getMethodSignatures(); 361 assertEquals( 362 "Did not return the right number of methods.", 363 1, 364 methods.length ); 365 assertEquals( 366 "Did not return the right method.", 367 "m()V", 368 methods[0] ); 369 } 370 371 372 public void testGetMethodSignatures3() 373 { 374 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 375 createMarkRecord( 0, "1", "m1()V", 0 ), 376 createMarkRecord( 0, "1", "m1()V", 1 ), 377 createMarkRecord( 0, "1", "m1()V", 2 ), 378 createMarkRecord( 0, "1", "m2()V", 3 ), 379 createMarkRecord( 0, "1", "m2()V", 2 ), 380 createMarkRecord( 0, "1", "m2()V", 1 ), 381 createMarkRecord( 0, "1", "m2()V", 0 ), 382 } ); 383 ClassMarkSet cms = new ClassMarkSet( 384 "a.M", new String [] { "m1()V", "m2()V" }, 385 mr, new IChannelLogRecord[] { 386 createChannelLogRecord( "a.M", 1, 1 ), 387 } ); 388 389 String [] methods = cms.getMethodSignatures(); 390 assertEquals( 391 "Did not return the right number of methods.", 392 2, 393 methods.length ); 394 assertTrue( 395 "Did not return the right methods.", 396 ( 397 methods[0].equals( "m1()V" ) && methods[1].equals( "m2()V" ) 398 ) || 399 ( 400 methods[0].equals( "m2()V" ) && methods[1].equals( "m1()V" ) 401 ) ); 402 } 403 404 405 public void testGetMethodSignatures4() 406 { 407 ClassMarkSet cms = new ClassMarkSet( "a.M", 408 new String [] { "1", "2" }, 409 new MarkRecord[0], new IChannelLogRecord[ 0 ] ); 410 String [] methods = cms.getMethodSignatures(); 411 assertNotNull( 412 "Returned null method signature list.", 413 methods ); 414 assertEquals( 415 "Did not return right methods.", 416 2, 417 methods.length ); 418 } 419 420 421 public void testGetCoveredMarksForMethod3() 422 { 423 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 424 createMarkRecord( 0, "1", "m1()V", 0 ), 425 createMarkRecord( 0, "1", "m1()V", 1 ), 426 createMarkRecord( 0, "1", "m1()V", 2 ), 427 createMarkRecord( 0, "1", "m1()V", 3 ), 428 createMarkRecord( 0, "1", "m2()V", 0 ), 429 createMarkRecord( 0, "1", "m2()V", 1 ), 430 createMarkRecord( 0, "1", "m2()V", 2 ), 431 createMarkRecord( 0, "1", "m2()V", 3 ), 432 } ); 433 ClassMarkSet cms = new ClassMarkSet( 434 "a.M", new String [] { "m1()V", "m2()V" }, 435 mr, new IChannelLogRecord[] { 436 createChannelLogRecord( "a.M", 0, 3 ), 437 } ); 438 MarkRecord covered[] = cms.getCoveredMarksForMethod( "m1()V" ); 439 assertEquals( 440 "Returned incorrect coverage list.", 441 1, 442 covered.length ); 443 assertEquals( 444 "Returned bad mark method.", 445 "m1()V", 446 covered[0].getMethodSignature() ); 447 assertEquals( 448 "Returned bad mark id.", 449 (short)3, 450 covered[0].getMarkIndex() ); 451 } 452 453 454 public void testGetCoveredMarksForMethod4() 455 { 456 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 457 createMarkRecord( 0, "1", "m1()V", 0 ), 458 createMarkRecord( 0, "1", "m1()V", 1 ), 459 createMarkRecord( 0, "1", "m1()V", 2 ), 460 createMarkRecord( 0, "1", "m1()V", 3 ), 461 createMarkRecord( 0, "1", "m2()V", 0 ), 462 createMarkRecord( 0, "1", "m2()V", 1 ), 463 createMarkRecord( 0, "1", "m2()V", 2 ), 464 createMarkRecord( 0, "1", "m2()V", 3 ), 465 } ); 466 ClassMarkSet cms = new ClassMarkSet( 467 "a.M", new String [] { "m1()V", "m2()V" }, 468 mr, new IChannelLogRecord[] { 469 createChannelLogRecord( "a.M", 0, 3 ), 470 createChannelLogRecord( "a.M", 1, 1 ), 472 createChannelLogRecord( "a.M", 1, 2 ), 473 } ); 474 MarkRecord covered[] = cms.getCoveredMarksForMethod( "m2()V" ); 475 assertEquals( 476 "Returned incorrect coverage list.", 477 2, 478 covered.length ); 479 assertEquals( 480 "Returned bad mark method [0].", 481 "m2()V", 482 covered[0].getMethodSignature() ); 483 assertEquals( 484 "Returned bad mark method [1].", 485 "m2()V", 486 covered[1].getMethodSignature() ); 487 488 assertTrue( 489 "Returned bad mark ids.", 490 ( 491 covered[0].getMarkIndex() == (short)1 && 492 covered[1].getMarkIndex() == (short)2 493 ) || 494 ( 495 covered[0].getMarkIndex() == (short)2 && 496 covered[1].getMarkIndex() == (short)1 497 ) ); 498 } 499 500 501 public void testGetNotCoveredMarksForMethod1() 502 { 503 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 504 createMarkRecord( 0, "1", "m1()V", 0 ), 505 createMarkRecord( 0, "1", "m1()V", 1 ), 506 createMarkRecord( 0, "1", "m1()V", 2 ), 507 createMarkRecord( 0, "1", "m1()V", 3 ), 508 createMarkRecord( 0, "1", "m2()V", 0 ), 509 createMarkRecord( 0, "1", "m2()V", 1 ), 510 createMarkRecord( 0, "1", "m2()V", 2 ), 511 createMarkRecord( 0, "1", "m2()V", 3 ), 512 } ); 513 ClassMarkSet cms = new ClassMarkSet( 514 "a.M", new String [] { "m1()V", "m2()V" }, 515 mr, new IChannelLogRecord[] { 516 createChannelLogRecord( "a.M", 0, 3 ), 517 createChannelLogRecord( "a.M", 1, 0 ), 519 createChannelLogRecord( "a.M", 1, 1 ), 520 createChannelLogRecord( "a.M", 1, 2 ), 521 } ); 522 MarkRecord nc[] = cms.getNotCoveredMarksForMethod( "m2()V" ); 523 assertEquals( 524 "Returned incorrect coverage list.", 525 1, 526 nc.length ); 527 assertEquals( 528 "Returned bad mark method.", 529 "m2()V", 530 nc[0].getMethodSignature() ); 531 assertEquals( 532 "Returned bad mark id.", 533 (short)3, 534 nc[0].getMarkIndex() ); 535 } 536 537 538 public void testGetNotCoveredMarksForMethod2() 539 { 540 MarkRecord mr[] = processMarks( "a.M", new MarkRecord[] { 541 createMarkRecord( 0, "1", "m1()V", 0 ), 542 createMarkRecord( 0, "1", "m1()V", 1 ), 543 createMarkRecord( 0, "1", "m1()V", 2 ), 544 createMarkRecord( 0, "1", "m1()V", 3 ), 545 } ); 546 ClassMarkSet cms = new ClassMarkSet( 547 "a.M", new String [] { "m1()V", "m2()V" }, 548 mr, new IChannelLogRecord[] { 549 createChannelLogRecord( "a.M", 0, 0 ), 550 createChannelLogRecord( "a.M", 0, 1 ), 551 createChannelLogRecord( "a.M", 0, 2 ), 552 createChannelLogRecord( "a.M", 0, 3 ), 553 } ); 555 MarkRecord nc[] = cms.getNotCoveredMarksForMethod( "m1()V" ); 556 assertNotNull( 557 "Returned null not-covered list.", 558 nc ); 559 assertEquals( 560 "Returned incorrect not-coverage list length.", 561 0, 562 nc.length ); 563 } 564 565 566 569 570 protected IAnalysisMetaData createAnalysisMetaData( int weight ) 571 { 572 return CCCreatorUtil.createIAnalysisMetaData( "a", "b", (byte)weight ); 573 } 574 575 576 protected MarkRecord createMarkRecord( int metaDataWeight, String am, 577 String methSig, int markId ) 578 { 579 MarkRecord mr = new MarkRecord( createAnalysisMetaData( metaDataWeight ), 580 am, methSig, (short)markId, 1 ); 581 return mr; 582 } 583 584 585 protected MarkRecord[] processMarks( String className, MarkRecord mr[] ) 586 { 587 List methodSet = new ArrayList (); 589 List measureSet = new ArrayList (); 590 for (int i = 0; i < mr.length; ++i) 591 { 592 if (!methodSet.contains( mr[i].getMethodSignature() )) 593 { 594 methodSet.add( mr[i].getMethodSignature() ); 595 } 596 if (!measureSet.contains( mr[i].getAnalysisModule() )) 597 { 598 measureSet.add( mr[i].getAnalysisModule() ); 599 } 600 } 601 602 IAnalysisModule modules[] = new IAnalysisModule[ measureSet.size() ]; 604 for (int i = 0; i < modules.length; ++i) 605 { 606 modules[i] = CCCreatorUtil.createIAnalysisModule( 607 (String )measureSet.get( i ), "a", "b" ); 608 } 609 String methods[] = (String [])methodSet.toArray( new String [ 610 methodSet.size() ] ); 611 612 ClassRecord cr = new ClassRecord( className, 100L, "a", methods, 613 CCCreatorUtil.createAnalysisModuleSet( modules ) ); 614 for (int i = 0; i < mr.length; ++i) 615 { 616 cr.addMark( mr[i] ); 617 } 618 619 return mr; 620 } 621 622 623 protected IChannelLogRecord createChannelLogRecord( String classSig, 624 int methodIndex, int markIndex ) 625 { 626 return new DefaultChannelLogRecord( classSig, (short)methodIndex, 627 (short)markIndex ); 628 } 629 630 631 634 635 public static Test suite() 636 { 637 TestSuite suite = new TestSuite( THIS_CLASS ); 638 639 return suite; 640 } 641 642 public static void main( String [] args ) 643 { 644 String [] name = { THIS_CLASS.getName() }; 645 646 649 junit.textui.TestRunner.main( name ); 650 } 651 652 653 657 protected void setUp() throws Exception 658 { 659 super.setUp(); 660 661 662 } 664 665 666 670 protected void tearDown() throws Exception 671 { 672 674 675 super.tearDown(); 676 } 677 } 678 679 | Popular Tags |