1 package csdl.jblanket.report; 2 3 import csdl.jblanket.JBlanket; 4 import csdl.jblanket.JBlanketException; 5 import csdl.jblanket.methodset.MethodSet; 6 import csdl.jblanket.methodset.MethodSetManager; 7 import csdl.jblanket.util.MethodCategories; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.util.Date ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.apache.tools.ant.DirectoryScanner; 19 20 104 public class JBlanketReport extends JBlanket { 105 106 107 private static final String PREFIX = "COVER-"; 108 109 private static final String AGG_FILENAME = "MethodSets.xml"; 110 111 112 private static String currentClassName; 113 114 private Date timeStamp; 115 116 private String reportFormat; 117 118 private File toDir; 119 120 private String results; 121 122 123 private MethodSet testedSet; 124 125 private MethodSet untestedSet; 126 127 128 private boolean excludeIndividualMethods = false; 129 130 private MethodSet excludedIndividualSet; 131 132 147 public JBlanketReport(boolean verbose, boolean excludeOneLineMethods, 148 boolean excludeConstructors, boolean excludeIndividualMethods, 149 String reportFormat, File toDir) 150 151 throws JBlanketException { 152 super(); 153 154 this.verbose = verbose; 155 this.reportFormat = reportFormat; 156 if (toDir == null) { 157 this.toDir = new File (jblanketDir); 158 } else { 159 this.toDir = toDir; 160 } 161 162 MethodSetManager manager = MethodSetManager.getInstance(); 165 super.totalSet = manager.getMethodSet(super.categories.getFileName("totalFile")); 166 this.timeStamp = loadMethods(super.totalSet, 167 new File (super.categories.getFileName("totalFile"))); 168 169 super.excludeOneLineMethods = excludeOneLineMethods; 171 super.oneLineSet = new MethodSet(); 172 if (excludeOneLineMethods) { 173 loadMethods(super.oneLineSet, new File (super.categories.getFileName("oneLineFile"))); 174 } 175 176 super.excludeConstructors = excludeConstructors; 178 super.constructorSet = new MethodSet(); 179 if (excludeConstructors) { 180 loadMethods(super.constructorSet, new File (super.categories.getFileName("constructorFile"))); 181 } 182 183 this.excludeIndividualMethods = excludeIndividualMethods; 185 this.excludedIndividualSet = new MethodSet(); 186 if (excludeIndividualMethods) { 187 File excludeIndividualFile = 188 new File (super.categories.getFileName("excludedIndividualFile")); 189 if (excludeIndividualFile.exists()) { 191 loadMethods(this.excludedIndividualSet, excludeIndividualFile); 192 } 193 else { 194 this.excludedIndividualSet = new MethodSet(); 195 } 196 } 197 198 super.untestableSet = manager.getMethodSet(super.categories.getFileName("untestableFile")); 199 loadMethods(super.untestableSet, new File (super.categories.getFileName("untestableFile"))); 200 201 this.testedSet = manager.getMethodSet(super.categories.getFileName("testedFile")); 203 this.untestedSet = manager.getMethodSet(super.categories.getFileName("untestedFile")); 204 } 205 206 212 public String createReport() throws JBlanketException { 213 214 createTestedFile(); 215 createUntestedFile(); 216 217 Map reportCategories = getReportCategories(); 219 AggregateTransformer transformer = new AggregateTransformer(reportCategories, this.reportFormat, 221 super.jblanketDir, this.toDir); 222 File aggregateFile = new File (super.jblanketDir, PREFIX + AGG_FILENAME); 223 transformer.createAggregateFile(aggregateFile); 224 225 transformer.transformXmlToHtml(aggregateFile.getAbsolutePath()); 227 return this.results; 228 } 229 230 236 protected void createTestedFile() throws JBlanketException { 237 238 DirectoryScanner scanner = new DirectoryScanner(); 240 scanner.setIncludes(new String []{PREFIX + "*"}); 241 scanner.setExcludes(null); 242 scanner.setBasedir(new File (jblanketDir)); 243 scanner.setCaseSensitive(true); 244 scanner.scan(); 245 String [] files = scanner.getIncludedFiles(); 246 247 if (files.length == 0) { 249 String message = "No intermediate '" + PREFIX + "*.xml' files found in " + super.jblanketDir 250 + ". Invoked class files may not have been modified by JBlanket."; 251 throw new JBlanketException(message); 252 } 253 254 MethodSetManager manager = MethodSetManager.getInstance(); 256 for (int i = 0; i < files.length; i++) { 257 MethodSet nextSet = manager.getMethodSet(files[i]); 258 259 if ((PREFIX + AGG_FILENAME).equals(files[i])) { 261 continue; 262 } 263 loadMethods(nextSet, new File (super.jblanketDir, files[i])); 264 this.testedSet.union(nextSet); 265 } 266 267 this.testedSet.intersection(super.totalSet); 269 270 try { 272 storeMethods(this.testedSet, new File (categories.getFileName("total.testedFile"))); 273 } 274 catch (IOException e) { 275 throw new JBlanketException("Unable to store total tested methods to " 276 + categories.getFileName("total.testedFile"), e); 277 } 278 279 this.testedSet.difference(super.oneLineSet); 281 this.testedSet.difference(super.constructorSet); 282 this.testedSet.difference(this.excludedIndividualSet); 283 try { 284 storeMethods(this.testedSet, new File (categories.getFileName("testedFile"))); 285 } 286 catch (IOException e) { 287 throw new JBlanketException("Unable to store tested methods to " 288 + categories.getFileName("testedFile"), e); 289 } 290 } 291 292 300 protected void createUntestedFile() throws JBlanketException { 301 int untestableSize = this.untestableSet.size(); 303 int totalSize = super.totalSet.size() + untestableSize; 304 int oneLineSize = super.oneLineSet.size(); 305 int constructorSize = super.constructorSet.size(); 306 int excludedIndividualSize = this.excludedIndividualSet.size(); 307 int subtotalSize = totalSize - oneLineSize - constructorSize - 308 excludedIndividualSize - untestableSize; 309 int testedSize = testedSet.size(); 310 311 this.untestedSet = super.totalSet.difference(this.testedSet); 313 try { 314 storeMethods(this.untestedSet, new File (categories.getFileName("total.untestedFile"))); 315 } 316 catch (IOException e) { 317 throw new JBlanketException("Unable to store total untested methods to " 318 + categories.getFileName("total.untestedFile"), e); 319 } 320 321 this.untestedSet.difference(super.oneLineSet); 323 this.untestedSet.difference(super.constructorSet); 324 this.untestedSet.difference(this.excludedIndividualSet); 325 try { 326 storeMethods(this.untestedSet, new File (super.categories.getFileName("untestedFile"))); 327 } 328 catch (IOException e) { 329 throw new JBlanketException("Unable to store untested methods to " 330 + categories.getFileName("untestedFile"), e); 331 } 332 333 int untestedSize = this.untestedSet.size(); 335 int percent = 100; 336 int oneLinePercent = Math.round(((float) oneLineSize / totalSize) * percent); 337 int constructorPercent = Math.round(((float) constructorSize / totalSize) * percent); 338 int excludedIndividualPercent = Math.round(((float) excludedIndividualSize / totalSize) 339 * percent); 340 int testedPercent = Math.round(((float) testedSize / subtotalSize) * percent); 341 int untestedPercent = percent - testedPercent; 342 343 StringBuffer msg = new StringBuffer (); 345 msg.append("********************************************************\n"); 346 msg.append("Method-level Coverage:\n"); 347 msg.append("All methods : {total=" + totalSize + "}\n"); 348 msg.append("Untestable methods : {total=" + untestableSize + "}\n"); 349 if (super.excludeOneLineMethods) { 350 msg.append("Excluded One-line methods : {total=" + oneLineSize + "}\n"); 351 } 352 if (super.excludeConstructors) { 353 msg.append("Excluded Constructors : {total=" + constructorSize + "}\n"); 354 } 355 if (this.excludeIndividualMethods) { 356 msg.append("Excluded individual methods : {total=" + excludedIndividualSize + "}\n"); 357 } 358 msg.append("--------------------------------------------------------\n"); 359 if (super.excludeOneLineMethods || super.excludeConstructors || this.excludeIndividualMethods) { 360 msg.append("Remaining methods : {total=" + subtotalSize + "}\n"); 361 } 362 msg.append("Tested methods : {total=" + testedSize + 363 ", percent=" + testedPercent + "%}\n"); 364 msg.append("Untested methods : {total=" + untestedSize + ", percent=" 365 + untestedPercent + "%}\n"); 366 msg.append("********************************************************"); 367 368 this.results = msg.toString(); 369 } 370 371 380 private Date loadMethods(MethodSet methodSet, File file) throws JBlanketException { 381 382 Date timeStamp; 383 try { 384 FileInputStream in = new FileInputStream (file); 386 timeStamp = methodSet.load(in); 388 in.close(); 390 } 391 catch (Exception e) { 392 throw new JBlanketException("Unable to read file " + file.getAbsolutePath(), e); 393 } 394 395 return timeStamp; 396 } 397 398 406 protected void storeMethods(MethodSet methodSet, File file) throws IOException { 407 408 FileOutputStream fostream = new FileOutputStream (file); 410 methodSet.store(fostream, null, this.timeStamp); 412 fostream.close(); 414 } 415 416 422 protected Map getReportCategories() { 423 424 Map reportCategories = new HashMap (); 425 426 if (super.excludeConstructors) { 427 reportCategories.put("constructor", super.categories.getFileName("constructorFile")); 428 } 429 430 if (super.excludeOneLineMethods) { 431 reportCategories.put("oneline", super.categories.getFileName("oneLineFile")); 432 } 433 434 if (this.excludeIndividualMethods) { 435 reportCategories.put("excludedIndividual", 436 super.categories.getFileName("excludedIndividualFile")); 437 } 438 439 reportCategories.put("tested", super.categories.getFileName("testedFile")); 440 reportCategories.put("untested", super.categories.getFileName("untestedFile")); 441 reportCategories.put("untestable", super.categories.getFileName("untestableFile")); 442 return reportCategories; 443 } 444 445 451 public static void main(String args[]) throws JBlanketException { 452 main(java.util.Arrays.asList(args)); 453 } 454 455 481 public static void main(List args) throws JBlanketException { 482 483 boolean verbose = false; 485 String reportFormat = "frames"; 487 File toDir = null; 488 489 boolean excludeOneLineMethods = false; 491 boolean excludeConstructors = false; 493 boolean excludeIndividualMethods = false; 495 496 MethodCategories categories = MethodCategories.getInstance(); 497 498 int i; 500 for (i = 0; i < args.size(); ++i) { 502 String argument = (String ) args.get(i); 503 if (argument.equals("-verbose")) { 504 verbose = ((Boolean ) args.get(++i)).booleanValue(); 505 } 506 else if (argument.equals("-reportFormat")) { 507 reportFormat = (String ) args.get(++i); 508 } 509 else if (argument.equals("-toDir")) { 510 toDir = new File ((String ) args.get(++i)); 511 } 512 else if (argument.equals("-excludeOneLineMethods")) { 513 excludeOneLineMethods = ((Boolean ) args.get(++i)).booleanValue(); 514 } 515 else if (argument.equals("-excludeConstructors")) { 516 excludeConstructors = ((Boolean ) args.get(++i)).booleanValue(); 517 } 518 else if (argument.equals("-excludeIndividualMethods")) { 519 excludeIndividualMethods = ((Boolean ) args.get(++i)).booleanValue(); 520 } 521 else if (argument.equals("-oneLineFile")) { 522 categories.addCategory("oneLineFile", (String ) args.get(++i)); 523 } 524 else if (argument.equals("-constructorFile")) { 525 categories.addCategory("constructorFile", (String ) args.get(++i)); 526 } 527 else if (argument.equals("-excludedIndividualFile")) { 528 categories.addCategory("excludedIndividualFile", (String ) args.get(++i)); 529 } 530 else if (argument.equals("-totalFile")) { 531 categories.addCategory("totalFile", (String ) args.get(++i)); 532 } 533 else if (argument.equals("-testedFile")) { 534 categories.addCategory("testedFile", (String ) args.get(++i)); 535 } 536 else if (argument.equals("-untestedFile")) { 537 categories.addCategory("untestedFile", (String ) args.get(++i)); 538 } 539 else if (argument.equals("-untestableFile")) { 540 categories.addCategory("untestableFile", (String ) args.get(++i)); 541 } 542 else { 543 System.out.println("Incorrect usage: " + argument); 544 System.exit(1); 545 } 546 } 547 548 JBlanketReport report = new JBlanketReport(verbose, excludeOneLineMethods, excludeConstructors, 549 excludeIndividualMethods, reportFormat, toDir); 550 551 System.out.println(report.createReport()); 552 if (reportFormat.equals("frames")) { 553 System.out.println("JBlanket results in " + report.toDir.getAbsolutePath() 554 + File.separator + "index.html"); 555 } 556 else { 557 System.out.println("JBlanket results in " + report.toDir.getAbsolutePath() 558 + File.separator + "jblanket-noframes.html"); 559 } 560 } 561 } 562 | Popular Tags |