PageRenderTime 44ms CodeModel.GetById 1ms app.highlight 37ms RepoModel.GetById 2ms app.codeStats 0ms

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