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

http://testability-explorer.googlecode.com/ · Java · 154 lines · 120 code · 18 blank · 16 comment · 0 complexity · 1e3dad6516c9d07940a3f9b0f09308b4 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 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.GlobalCost;
  22. import com.google.test.metric.MethodCost;
  23. import com.google.test.metric.MethodInvocationCost;
  24. import static com.google.test.metric.Reason.IMPLICIT_STATIC_INIT;
  25. import static com.google.test.metric.Reason.NON_OVERRIDABLE_METHOD_CALL;
  26. import com.google.test.metric.SourceLocation;
  27. import com.google.test.metric.ViolationCost;
  28. import junit.framework.TestCase;
  29. import org.apache.xml.serialize.OutputFormat;
  30. import org.apache.xml.serialize.XMLSerializer;
  31. import org.xml.sax.SAXException;
  32. import java.io.StringWriter;
  33. import static java.util.Arrays.asList;
  34. public class XMLReportTest extends TestCase {
  35. private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  36. private final StringWriter out = new StringWriter();
  37. private final XMLSerializer handler = new XMLSerializer();
  38. private final CostModel costModel = new CostModel();
  39. @Override
  40. protected void setUp() throws Exception {
  41. handler.setOutputCharStream(out);
  42. handler.startDocument();
  43. OutputFormat format = new OutputFormat();
  44. format.setIndenting(true);
  45. handler.setOutputFormat(format);
  46. }
  47. private void assertXMLEquals(String expected) throws SAXException {
  48. handler.endDocument();
  49. assertEquals(XML_HEADER + expected, out.toString().trim());
  50. }
  51. private void write(String text) throws SAXException {
  52. char[] chars = new char[text.length()];
  53. text.getChars(0, text.length(), chars, 0);
  54. handler.characters(chars, 0, chars.length);
  55. }
  56. public void testPrintCost() throws Exception {
  57. XMLReportGenerator report = new XMLReportGenerator(handler, costModel, 0, 0, 0);
  58. MethodCost methodCost = new MethodCost("", "methodName", 1, false, false, false);
  59. methodCost.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  60. methodCost.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  61. ViolationCost violation = new MethodInvocationCost(new SourceLocation("source.file", 123), methodCost,
  62. IMPLICIT_STATIC_INIT, Cost.cyclomatic(2).add(Cost.global(3)));
  63. report.writeCost(violation);
  64. assertXMLEquals("<cost cyclomatic=\"2\" file=\"source.file\" global=\"3\" line=\"123\" "
  65. + "lod=\"0\" method=\"methodName\" overall=\"32\" "
  66. + "reason=\"implicit cost from static initialization\"/>");
  67. }
  68. // This was throwing NPE before
  69. public void testPrintCostNullReason() throws Exception {
  70. XMLReportGenerator report = new XMLReportGenerator(handler, costModel, 0, 0, 0);
  71. MethodCost methodCost = new MethodCost("", "methodName", 1, false, false, false);
  72. methodCost.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  73. methodCost.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  74. ViolationCost violation = new MethodInvocationCost(new SourceLocation("source.file", 123), methodCost,
  75. NON_OVERRIDABLE_METHOD_CALL, Cost.cyclomatic(2).add(Cost.global(3)));
  76. report.writeCost(violation);
  77. assertXMLEquals("<cost cyclomatic=\"2\" file=\"source.file\" global=\"3\" line=\"123\" "
  78. + "lod=\"0\" method=\"methodName\" overall=\"32\" "
  79. + "reason=\"" + NON_OVERRIDABLE_METHOD_CALL + "\"/>");
  80. }
  81. public void testPrintMethodCost() throws Exception {
  82. XMLReportGenerator report = new XMLReportGenerator(handler, costModel, 0, 0, 0) {
  83. @Override
  84. public void writeCost(ViolationCost violation) throws SAXException {
  85. write("L" + violation.getLocation().getLineNumber() + ",");
  86. }
  87. };
  88. MethodCost methodCost = new MethodCost("", "methodName", 123, false, false, false);
  89. methodCost.addCostSource(new GlobalCost(new SourceLocation(null, 123), null, Cost.global(1)));
  90. methodCost.addCostSource(new CyclomaticCost(new SourceLocation(null, 234), Cost.cyclomatic(1)));
  91. methodCost.addCostSource(new CyclomaticCost(new SourceLocation(null, 345), Cost.cyclomatic(1)));
  92. methodCost.addCostSource(new GlobalCost(new SourceLocation(null, 456), null, Cost.global(1)));
  93. methodCost.link();
  94. report.writeCost(methodCost);
  95. assertXMLEquals("<method cyclomatic=\"2\" global=\"2\" line=\"123\" "
  96. + "lod=\"0\" name=\"methodName\" overall=\"22\">L123,L234,L345,L456,</method>");
  97. }
  98. public void testPrintClassCost() throws Exception {
  99. XMLReportGenerator report = new XMLReportGenerator(handler, costModel, 0, 0, 0) {
  100. @Override
  101. public void writeCost(MethodCost methodCost) throws SAXException {
  102. write(methodCost.getMethodName() + "()");
  103. }
  104. };
  105. MethodCost m1 = new MethodCost("", "M1", -1, false, false, false);
  106. m1.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  107. m1.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  108. MethodCost m2 = new MethodCost("", "M2", -1, false, false, false);
  109. m2.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  110. m1.link();
  111. m2.link();
  112. ClassCost classCost = new ClassCost("className", asList(m1, m2));
  113. report.writeCost(classCost);
  114. assertXMLEquals("<class class=\"className\" cost=\"1\">M1()M2()</class>");
  115. }
  116. public void testWholeDocument() throws Exception {
  117. XMLReportGenerator report = new XMLReportGenerator(handler, costModel, 1, 2, 3) {
  118. @Override
  119. public void writeCost(ClassCost cost) throws SAXException {
  120. write(cost.getClassName() + ";");
  121. }
  122. };
  123. report.printHeader();
  124. MethodCost m1 = new MethodCost("", "M1", -1, false, false, false);
  125. m1.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  126. m1.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  127. m1.link();
  128. ClassCost c1 = new ClassCost("C1", asList(m1));
  129. ClassCost c2 = new ClassCost("C2", asList(m1));
  130. report.addClassCost(c1);
  131. report.addClassCost(c2);
  132. report.printFooter();
  133. assertXMLEquals("<testability excellent=\"0\" good=\"0\" " +
  134. "needsWork=\"2\" overall=\"2\">C1;C2;</testability>");
  135. }
  136. }