/testability-explorer/src/main/java/com/google/test/metric/report/DrillDownReportGenerator.java

http://testability-explorer.googlecode.com/ · Java · 141 lines · 107 code · 16 blank · 18 comment · 14 complexity · 5d1216fe02dc0b09550aa7e8bfe15402 MD5 · raw file

  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.report;
  17. import static java.lang.System.getProperty;
  18. import java.io.PrintStream;
  19. import java.util.HashSet;
  20. import java.util.List;
  21. import java.util.Set;
  22. import java.util.SortedSet;
  23. import java.util.TreeSet;
  24. import com.google.test.metric.ClassCost;
  25. import com.google.test.metric.CostModel;
  26. import com.google.test.metric.MethodCost;
  27. import com.google.test.metric.MethodInvocationCost;
  28. import com.google.test.metric.ViolationCost;
  29. /**
  30. * @deprecated remove this report as it's not very useful
  31. */
  32. public class DrillDownReportGenerator implements ReportGenerator {
  33. public static final String NEW_LINE = getProperty("line.separator");
  34. private static final String DIVIDER = "-----------------------------------------\n";
  35. private final PrintStream out;
  36. private final List<String> entryList;
  37. private final SortedSet<ClassCost> toPrint;
  38. private final int maxDepth;
  39. private final int minCost;
  40. private final CostModel costModel;
  41. public DrillDownReportGenerator(PrintStream out, CostModel costModel,
  42. List<String> entryList, int maxDepth, int minCost) {
  43. this.out = out;
  44. this.entryList = entryList;
  45. this.costModel = costModel;
  46. this.maxDepth = maxDepth;
  47. this.minCost = minCost;
  48. toPrint = new TreeSet<ClassCost>(new ClassCost.CostComparator(costModel));
  49. }
  50. public void printHeader() {
  51. out.println(DIVIDER + "Packages/Classes To Enter: ");
  52. for (String entry : entryList) {
  53. out.println(" " + entry + "*");
  54. }
  55. out.println("Max Method Print Depth: " + maxDepth);
  56. out.println("Min Class Cost: " + minCost);
  57. out.println(DIVIDER);
  58. }
  59. public void printFooter() {
  60. for (ClassCost classCost : toPrint) {
  61. print(classCost);
  62. }
  63. }
  64. public void addClassCost(ClassCost classCost) {
  65. toPrint.add(classCost);
  66. }
  67. public void print(ClassCost classCost) {
  68. if (shouldPrint(classCost, minCost)) {
  69. long tcc = classCost.getTotalComplexityCost();
  70. long tgc = classCost.getTotalGlobalCost();
  71. out.println(NEW_LINE + "Testability cost for " + classCost.getClassName()
  72. + " [ cost = " + costModel.computeClass(classCost) + " ]" + " [ "
  73. + tcc + " TCC, " + tgc + " TGC ]");
  74. for (MethodCost cost : classCost.getMethods()) {
  75. print(" ", cost, maxDepth);
  76. }
  77. }
  78. }
  79. public void print(String prefix, MethodCost cost, int maxDepth) {
  80. Set<String> alreadySeen = new HashSet<String>();
  81. if (shouldPrint(cost, maxDepth, alreadySeen)) {
  82. out.print(prefix);
  83. out.println(cost);
  84. for (ViolationCost child : cost.getViolationCosts()) {
  85. print(" " + prefix, child, maxDepth - 1, alreadySeen);
  86. }
  87. }
  88. }
  89. private void print(String prefix, ViolationCost line, int maxDepth,
  90. Set<String> alreadSeen) {
  91. if (!(line instanceof MethodInvocationCost)) {
  92. return;
  93. }
  94. MethodCost method = ((MethodInvocationCost) line).getMethodCost();
  95. if (shouldPrint(method, maxDepth, alreadSeen)) {
  96. out.print(prefix);
  97. out.print("line ");
  98. out.print(line.getLocation());
  99. out.print(": ");
  100. out.print(method);
  101. out.println(" " + line.getReason());
  102. for (ViolationCost child : method.getViolationCosts()) {
  103. print(" " + prefix, child, maxDepth - 1, alreadSeen);
  104. }
  105. }
  106. }
  107. private boolean shouldPrint(ClassCost classCost, int minCost) {
  108. return classCost.getHighestMethodComplexityCost() >= minCost
  109. || classCost.getHighestMethodGlobalCost() >= minCost;
  110. }
  111. private boolean shouldPrint(MethodCost method, int maxDepth,
  112. Set<String> alreadySeen) {
  113. if (maxDepth <= 0 || alreadySeen.contains(method.getMethodName())) {
  114. return false;
  115. }
  116. alreadySeen.add(method.getMethodName());
  117. long totalComplexityCost = method.getTotalCost()
  118. .getCyclomaticComplexityCost();
  119. long totalGlobalCost = method.getTotalCost().getGlobalCost();
  120. if (totalGlobalCost < minCost && totalComplexityCost < minCost) {
  121. return false;
  122. }
  123. return true;
  124. }
  125. }