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