1 26 27 package net.sourceforge.groboutils.codecoverage.v2.ant; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.util.Properties ; 32 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 36 37 38 45 public class GroboInstrumentTaskUTest extends AntTestA 46 { 47 50 private static final Class THIS_CLASS = GroboInstrumentTaskUTest.class; 51 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 52 53 public GroboInstrumentTaskUTest( String name ) 54 { 55 super( name ); 56 } 57 58 59 62 65 private static final Properties EXPECTED_1 = new Properties (); 66 static { 67 EXPECTED_1.setProperty( "factory", 68 "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory" ); 69 EXPECTED_1.setProperty( "channel-count", "0" ); 70 } 71 public void testInstrument1() throws Exception 72 { 73 executeTarget( "instrument-1" ); 74 75 EXPECTED_1.setProperty( "logger.dir", 76 (new File ( getCoverageDir(), 77 "logs" + File.separator + "logs" )).getAbsolutePath() ); 78 79 assertEquals( 80 "Did not create the grobocoverage.properties file correctly", 81 EXPECTED_1, 82 loadGroboProperties() ); 83 } 84 85 private static final Properties EXPECTED_2 = new Properties (); 86 static { 87 EXPECTED_2.setProperty( "factory", 88 "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory" ); 89 EXPECTED_2.setProperty( "channel-count", "0" ); 90 } 91 public void testInstrument2() throws Exception 92 { 93 executeTarget( "instrument-2" ); 94 95 EXPECTED_2.setProperty( "logger.dir", 96 (new File ( getCoverageDir(), 97 "logs" + File.separator + "logs" )).getAbsolutePath() ); 98 99 assertEquals( 100 "Did not create the grobocoverage.properties file correctly", 101 EXPECTED_2, 102 loadGroboProperties() ); 103 104 assertModuleData( 106 "Did not create the module data correctly:", 107 "LineCount", 108 "a.A" ); 109 } 110 111 112 public void testInstrument3() throws Exception 113 { 114 executeTarget( "instrument-3" ); 115 116 assertModuleData( 118 "Did not create the module data correctly:", 119 "LineCount", 120 "a.A" ); 121 assertModuleData( 122 "Did not create the module data correctly:", 123 "LineCount", 124 "a.A$X" ); 125 assertModuleData( 126 "Did not create the module data correctly:", 127 "LineCount", 128 "a.B" ); 129 assertModuleData( 130 "Did not create the module data correctly:", 131 "LineCount", 132 "a.B$X" ); 133 } 134 135 136 public void testInstrument3a() throws Exception 137 { 138 executeTarget( "instrument-3a" ); 139 140 assertModuleData( 142 "Did not create the module data correctly:", 143 "LineCount", 144 "a.A" ); 145 assertModuleData( 146 "Did not create the module data correctly:", 147 "LineCount", 148 "a.A$X" ); 149 assertModuleData( 150 "Did not remove the module data correctly:", 151 "LineCount", 152 "a.B", 0 ); 153 } 154 155 156 public void testInstrument4() throws Exception 157 { 158 executeTarget( "instrument-4" ); 159 160 162 169 assertModuleData( 170 "Did not create the module data correctly:", 171 "LineCount", 172 "a.A$X" ); 173 assertModuleData( 174 "Did not create the module data correctly:", 175 "LineCount", 176 "a.B", 2 ); 177 assertModuleData( 178 "Did not create the module data correctly:", 179 "LineCount", 180 "a.B$X" ); 181 } 182 183 184 public void testInstrument5() throws Exception 185 { 186 executeTarget( "instrument-5" ); 187 188 assertModuleData( 190 "Did not create the module data correctly:", 191 "LineCount", 192 "a.A" ); 193 assertModuleData( 194 "Did not keep the module data correctly:", 195 "LineCount", 196 "a.B" ); 197 assertModuleData( 198 "Did not create the module data correctly:", 199 "LineCount", 200 "a.A$X" ); 201 } 202 203 204 private static final Properties EXPECTED_6 = new Properties (); 205 static { 206 EXPECTED_6.setProperty( "factory", 207 "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory" ); 208 EXPECTED_6.setProperty( "channel-count", "0" ); 209 EXPECTED_6.setProperty( "logger.1", "A" ); 210 } 211 public void testInstrument6() throws Exception 212 { 213 executeTarget( "instrument-6" ); 214 215 EXPECTED_6.setProperty( "logger.dir", 216 (new File ( getCoverageDir(), 217 "logs" + File.separator + "logs" )).getAbsolutePath() ); 218 EXPECTED_6.setProperty( "logger.2", 219 getCoverageDir().getAbsolutePath() ); 220 221 assertEquals( 222 "Did not create the grobocoverage.properties file correctly", 223 EXPECTED_6, 224 loadGroboProperties() ); 225 } 226 227 228 public void testInstrument7() throws Exception 229 { 230 executeTarget( "instrument-7" ); 232 } 233 234 235 private static final Properties EXPECTED_8 = new Properties (); 236 static { 237 EXPECTED_8.setProperty( "factory", 238 "net.sourceforge.groboutils.codecoverage.v2.logger.MinDirChannelLoggerFactory" ); 239 EXPECTED_8.setProperty( "channel-count", "0" ); 240 } 241 public void testInstrument8() throws Exception 242 { 243 executeTarget( "instrument-8" ); 244 245 EXPECTED_8.setProperty( "logger.dir", 246 (new File ( getCoverageDir(), 247 "logs" + File.separator + "logs" )).getAbsolutePath() ); 248 249 assertEquals( 250 "Did not create the grobocoverage.properties file correctly", 251 EXPECTED_8, 252 loadGroboProperties() ); 253 } 254 255 256 private static final Properties EXPECTED_9 = new Properties (); 257 static { 258 EXPECTED_9.setProperty( "factory", 259 "net.sourceforge.groboutils.codecoverage.v2.logger.X" ); 260 EXPECTED_9.setProperty( "channel-count", "0" ); 261 } 262 public void testInstrument9() throws Exception 263 { 264 executeTarget( "instrument-9" ); 265 266 EXPECTED_9.setProperty( "logger.dir", 267 (new File ( getCoverageDir(), 268 "logs" + File.separator + "logs" )).getAbsolutePath() ); 269 270 assertEquals( 271 "Did not create the grobocoverage.properties file correctly", 272 EXPECTED_9, 273 loadGroboProperties() ); 274 } 275 276 277 private static final Properties EXPECTED_10 = new Properties (); 278 static { 279 EXPECTED_10.setProperty( "factory", 280 "a.X" ); 281 EXPECTED_10.setProperty( "channel-count", "0" ); 282 } 283 public void testInstrument10() throws Exception 284 { 285 executeTarget( "instrument-9" ); 286 287 EXPECTED_10.setProperty( "logger.dir", 288 (new File ( getCoverageDir(), 289 "logs" + File.separator + "logs" )).getAbsolutePath() ); 290 291 assertEquals( 292 "Did not create the grobocoverage.properties file correctly", 293 EXPECTED_10, 294 loadGroboProperties() ); 295 } 296 297 298 299 302 public void testFInstrument1() throws Exception 303 { 304 expectBuildExceptionContaining( "f-instrument-1", 305 "did not fail on no logdir specified", 306 "Attribute 'logdir' was never set."); 307 } 308 309 310 public void testFInstrument2() throws Exception 311 { 312 expectBuildExceptionContaining( "f-instrument-2", 313 "did not fail on no destdir specified", 314 "Attribute 'destdir' was never set."); 315 } 316 317 318 public void testFInstrument3() throws Exception 319 { 320 expectBuildExceptionContaining( "f-instrument-3", 321 "did not fail on no loggerprop value specified", 322 "No value or location given for loggerprop key \"3\"."); 323 } 324 325 326 public void testFInstrument4() throws Exception 327 { 328 expectBuildExceptionContaining( "f-instrument-4", 329 "did not fail on no loggerprop key specified", 330 "No key given for loggerprop."); 331 } 332 333 334 public void testFInstrument5() throws Exception 335 { 336 expectBuildExceptionContaining( "f-instrument-5", 337 "did not fail on no loggerprop key specified", 338 "No key given for loggerprop."); 339 } 340 341 342 public void testFInstrument6() throws Exception 343 { 344 expectBuildExceptionContaining( "f-instrument-6", 345 "did not fail on no loggerprop key specified", 346 "No key given for loggerprop."); 347 } 348 349 350 353 354 protected void assertEquals( String text, Properties expected, 355 Properties actual ) 356 { 357 PropertyCheckUtil.assertEquals( text, expected, actual ); 358 } 359 360 361 protected Properties loadGroboProperties() 362 throws IOException 363 { 364 return loadProperties( 365 (new File ( getCoverageDir(), 366 "classes" + File.separator + "grobocoverage.properties")). 367 getAbsolutePath() ); 368 } 369 370 371 protected Properties loadProperties( String file ) 372 throws IOException 373 { 374 return PropertyCheckUtil.loadProperties( file ); 375 } 376 377 378 protected File getCoverageDir() 379 { 380 return new File ( getProjectDir(), "instrument" + 381 File.separator + "coverage" ); 382 } 383 384 385 protected void assertModuleData( String text, String moduleName, 386 String className ) 387 { 388 StringBuffer errors = new StringBuffer ( text ); 389 errors.append( "(for class " ).append( className ). 390 append( " in module " ).append( moduleName ). 391 append( ")" ); 392 boolean hasErrors = false; 393 String prefix = className + "-"; 394 File parent = new File ( getCoverageDir(), "logs" + 395 File.separator + "data" + File.separator + moduleName ); 396 String contents[] = parent.list(); 397 boolean classdata = false; 398 boolean classmarks = false; 399 for (int i = 0; i < contents.length; ++i) 400 { 401 if (contents[i].startsWith( prefix )) 402 { 403 if (contents[i].endsWith( ".classdata.txt" )) 404 { 405 System.out.println("Found class data file "+contents[i]); 406 if (classdata) 407 { 408 errors.append( "; too many classdata files" ); 409 hasErrors = true; 410 } 411 else 412 { 413 classdata = true; 414 } 415 } 416 if (contents[i].endsWith( ".classmarks.txt" )) 417 { 418 System.out.println("Found class marks file "+contents[i]); 419 if (classmarks) 420 { 421 errors.append( "; too many classmark files" ); 422 hasErrors = true; 423 } 424 else 425 { 426 classmarks = true; 427 } 428 } 429 } 430 } 431 432 if (!classdata) 433 { 434 errors.append( "; did not create classdata file" ); 435 hasErrors = true; 436 } 437 if (!classmarks) 438 { 439 errors.append( "; did not create classmarks file" ); 440 hasErrors = true; 441 } 442 assertFalse( errors.toString(), hasErrors ); 443 } 444 445 446 450 protected void assertModuleData( String text, String moduleName, 451 String className, int count ) 452 { 453 String baseErrorText = text + "(for class " + className + 454 " in module " + moduleName + ") "; 455 String prefix = className + "-"; 456 File parent = new File ( getCoverageDir(), "logs" + 457 File.separator + "data" + File.separator + moduleName ); 458 String contents[] = parent.list(); 459 int classdata = 0; 460 int classmarks = 0; 461 for (int i = 0; i < contents.length; ++i) 462 { 463 if (contents[i].startsWith( prefix )) 464 { 465 if (contents[i].endsWith( ".classdata.txt" )) 466 { 467 ++classdata; 468 } 469 if (contents[i].endsWith( ".classmarks.txt" )) 470 { 471 ++classmarks; 472 } 473 } 474 } 475 476 assertEquals( 477 baseErrorText + "incorrect number of classdata files.", 478 count, 479 classdata ); 480 assertEquals( 481 baseErrorText + "incorrect number of classmarks files.", 482 count, 483 classmarks ); 484 } 485 486 487 490 491 public static Test suite() 492 { 493 TestSuite suite = new TestSuite( THIS_CLASS ); 494 495 return suite; 496 } 497 498 public static void main( String [] args ) 499 { 500 String [] name = { THIS_CLASS.getName() }; 501 502 505 junit.textui.TestRunner.main( name ); 506 } 507 508 509 513 protected void setUp() throws Exception 514 { 515 super.setUp(); 516 517 configureProject( "instrument.xml" ); 519 } 520 521 522 526 protected void tearDown() throws Exception 527 { 528 executeTarget( "test-teardown" ); 530 531 super.tearDown(); 532 } 533 } 534 535 | Popular Tags |