PageRenderTime 20ms CodeModel.GetById 7ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/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 */
 16package com.google.test.metric.report;
 17
 18import static java.lang.System.getProperty;
 19
 20import java.io.PrintStream;
 21import java.util.HashSet;
 22import java.util.List;
 23import java.util.Set;
 24import java.util.SortedSet;
 25import java.util.TreeSet;
 26
 27import com.google.test.metric.ClassCost;
 28import com.google.test.metric.CostModel;
 29import com.google.test.metric.MethodCost;
 30import com.google.test.metric.MethodInvocationCost;
 31import com.google.test.metric.ViolationCost;
 32
 33/**
 34 * @deprecated remove this report as it's not very useful
 35 */
 36public class DrillDownReportGenerator implements ReportGenerator {
 37  public static final String NEW_LINE = getProperty("line.separator");
 38
 39  private static final String DIVIDER = "-----------------------------------------\n";
 40  private final PrintStream out;
 41  private final List<String> entryList;
 42  private final SortedSet<ClassCost> toPrint;
 43  private final int maxDepth;
 44  private final int minCost;
 45
 46  private final CostModel costModel;
 47
 48  public DrillDownReportGenerator(PrintStream out, CostModel costModel,
 49      List<String> entryList, int maxDepth, int minCost) {
 50    this.out = out;
 51    this.entryList = entryList;
 52    this.costModel = costModel;
 53    this.maxDepth = maxDepth;
 54    this.minCost = minCost;
 55    toPrint = new TreeSet<ClassCost>(new ClassCost.CostComparator(costModel));
 56  }
 57
 58  public void printHeader() {
 59    out.println(DIVIDER + "Packages/Classes To Enter: ");
 60    for (String entry : entryList) {
 61      out.println("  " + entry + "*");
 62    }
 63    out.println("Max Method Print Depth: " + maxDepth);
 64    out.println("Min Class Cost: " + minCost);
 65    out.println(DIVIDER);
 66  }
 67
 68  public void printFooter() {
 69    for (ClassCost classCost : toPrint) {
 70      print(classCost);
 71    }
 72  }
 73
 74  public void addClassCost(ClassCost classCost) {
 75    toPrint.add(classCost);
 76  }
 77
 78  public void print(ClassCost classCost) {
 79    if (shouldPrint(classCost, minCost)) {
 80      long tcc = classCost.getTotalComplexityCost();
 81      long tgc = classCost.getTotalGlobalCost();
 82      out.println(NEW_LINE + "Testability cost for " + classCost.getClassName()
 83          + " [ cost = " + costModel.computeClass(classCost) + " ]" + " [ "
 84          + tcc + " TCC, " + tgc + " TGC ]");
 85      for (MethodCost cost : classCost.getMethods()) {
 86        print("  ", cost, maxDepth);
 87      }
 88    }
 89  }
 90
 91  public void print(String prefix, MethodCost cost, int maxDepth) {
 92    Set<String> alreadySeen = new HashSet<String>();
 93    if (shouldPrint(cost, maxDepth, alreadySeen)) {
 94      out.print(prefix);
 95      out.println(cost);
 96      for (ViolationCost child : cost.getViolationCosts()) {
 97        print("  " + prefix, child, maxDepth - 1, alreadySeen);
 98      }
 99    }
100  }
101
102  private void print(String prefix, ViolationCost line, int maxDepth,
103      Set<String> alreadSeen) {
104    if (!(line instanceof MethodInvocationCost)) {
105      return;
106    }
107    MethodCost method = ((MethodInvocationCost) line).getMethodCost();
108    if (shouldPrint(method, maxDepth, alreadSeen)) {
109      out.print(prefix);
110      out.print("line ");
111      out.print(line.getLocation());
112      out.print(": ");
113      out.print(method);
114      out.println(" " + line.getReason());
115      for (ViolationCost child : method.getViolationCosts()) {
116        print("  " + prefix, child, maxDepth - 1, alreadSeen);
117      }
118    }
119  }
120
121  private boolean shouldPrint(ClassCost classCost, int minCost) {
122    return classCost.getHighestMethodComplexityCost() >= minCost
123        || classCost.getHighestMethodGlobalCost() >= minCost;
124  }
125
126  private boolean shouldPrint(MethodCost method, int maxDepth,
127      Set<String> alreadySeen) {
128    if (maxDepth <= 0 || alreadySeen.contains(method.getMethodName())) {
129      return false;
130    }
131    alreadySeen.add(method.getMethodName());
132    long totalComplexityCost = method.getTotalCost()
133        .getCyclomaticComplexityCost();
134    long totalGlobalCost = method.getTotalCost().getGlobalCost();
135    if (totalGlobalCost < minCost && totalComplexityCost < minCost) {
136      return false;
137    }
138    return true;
139  }
140
141}