PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 93 lines | 66 code | 12 blank | 15 comment | 2 complexity | 38145fdb86894dabc06bd8b50884dcdf 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
17package com.google.test.metric.report;
18
19
20import com.google.test.metric.ClassCost;
21import com.google.test.metric.Cost;
22import com.google.test.metric.CostModel;
23import com.google.test.metric.CyclomaticCost;
24import com.google.test.metric.MethodCost;
25import com.google.test.metric.SourceLocation;
26
27import junit.framework.TestCase;
28
29import java.io.ByteArrayOutputStream;
30import java.io.PrintStream;
31import java.util.ArrayList;
32import java.util.List;
33
34public class TextReportTest extends TestCase {
35
36  ByteArrayOutputStream out = new ByteArrayOutputStream();
37  CostModel costModel = new CostModel(1, 1, 1);
38  TextReportGenerator report = new TextReportGenerator(new PrintStream(out), costModel, 50, 100, 0);
39
40  private void assertOutput(String... expected) {
41    StringBuilder buf = new StringBuilder();
42    for (String expect : expected) {
43      buf.append(expect);
44      buf.append(Constants.NEW_LINE);
45    }
46    assertEquals(buf.toString(), out.toString());
47  }
48
49  private ClassCost classCost(String name, int cost) {
50    List<MethodCost> methods = new ArrayList<MethodCost>();
51    MethodCost methodCost = new MethodCost("", "method_" + cost, 1, false, false, false);
52    for (int i = 0; i < cost; i++) {
53      methodCost.addCostSource(new CyclomaticCost(new SourceLocation(null, i), Cost.cyclomatic(1)));
54    }
55    methodCost.link();
56    methods.add(methodCost);
57    ClassCost classCost = new ClassCost(name, methods);
58    return classCost;
59  }
60
61  public void testPrintSummary() throws Exception {
62    report.addClassCost(classCost("c.g.t.A", 1));
63    report.addClassCost(classCost("c.g.t.B", 70));
64    report.addClassCost(classCost("c.g.t.C", 70));
65    report.addClassCost(classCost("c.g.t.D", 101));
66    report.addClassCost(classCost("c.g.t.E", 101));
67    report.addClassCost(classCost("c.g.t.F", 101));
68    report.printSummary();
69    assertOutput(
70        "      Analyzed classes:     6",
71        " Excellent classes (.):     1  16.7%",
72        "      Good classes (=):     2  33.3%",
73        "Needs work classes (@):     3  50.0%",
74        "             Breakdown: [.........=================@@@@@@@@@@@@@@@@@@@@@@@@@]");
75  }
76
77  public void testPrintDistribution() throws Exception {
78    report.addClassCost(classCost("c.g.t.A", 1));
79    report.addClassCost(classCost("c.g.t.B", 10));
80    report.addClassCost(classCost("c.g.t.C", 15));
81    report.addClassCost(classCost("c.g.t.D", 30));
82    report.addClassCost(classCost("c.g.t.E", 31));
83    report.addClassCost(classCost("c.g.t.F", 32));
84    report.printDistribution(3, 50);
85    assertOutput(
86        "       0                                                  3",
87        "     5 |..................................                 :     2",
88        "    16 |.................                                  :     1",
89        "    27 |...................................................:     3"
90        );
91  }
92
93}