PageRenderTime 64ms CodeModel.GetById 30ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 139 lines | 115 code | 19 blank | 5 comment | 0 complexity | e3d3ecf6c7d253314c67cdbc4805d1e6 MD5 | raw file
  1package com.google.test.metric.report;
  2
  3import com.google.test.metric.ReportGeneratorProvider;
  4
  5import freemarker.template.Configuration;
  6
  7import junit.framework.TestCase;
  8
  9import java.io.StringWriter;
 10import java.io.Writer;
 11import java.util.Arrays;
 12import java.util.Collections;
 13
 14/**
 15 * Tests for HTML generation of the Diff report
 16 *
 17 * @author alexeagle@google.com (Alex Eagle)
 18 */
 19public class DiffReportTest extends TestCase {
 20  private Writer out;
 21  private Configuration cfg;
 22
 23  @Override
 24  protected void setUp() throws Exception {
 25    super.setUp();
 26    out = new StringWriter();
 27    cfg = new Configuration();
 28    cfg.setTemplateLoader(new ClassPathTemplateLoader(ReportGeneratorProvider.PREFIX));
 29  }
 30
 31  @SuppressWarnings("unchecked")
 32  public void testNoDiffs() throws Exception {
 33    Diff diff = new Diff(Collections.EMPTY_LIST);
 34    DiffReport diffReport = new DiffReport(diff, cfg);
 35    diffReport.writeHtml(out);
 36    assertContains("<html>", out.toString());
 37  }
 38
 39  public void testSourceLinks() throws Exception {
 40    Diff diff = new Diff(Arrays.asList(new Diff.ClassDiff("com.Foo", 123, 456)));
 41    DiffReport diffReport = new DiffReport(diff, cfg);
 42    diffReport.setOldSourceUrl("link1/{path}.html");
 43    diffReport.setNewSourceUrl("link2/{path}.html");
 44    diffReport.writeHtml(out);
 45    assertContains("link1/com.Foo.html", out.toString());
 46    assertContains("link2/com.Foo.html", out.toString());
 47
 48  }
 49
 50  public void testClassCostIncreased() throws Exception {
 51    Diff diff = new Diff(Arrays.asList(new Diff.ClassDiff("Foo", 123, 456)));
 52    DiffReport diffReport = new DiffReport(diff, cfg);
 53    diffReport.writeHtml(out);
 54    assertContains("Foo", out.toString());
 55    assertContains("123", out.toString());
 56    assertContains("456", out.toString());
 57    assertContains("class=\"worse", out.toString());
 58
 59  }
 60
 61  public void testClassCostDecreased() throws Exception {
 62    Diff diff = new Diff(Arrays.asList(new Diff.ClassDiff("Foo", 456, 123)));
 63    DiffReport diffReport = new DiffReport(diff, cfg);
 64    diffReport.writeHtml(out);
 65    assertContains("Foo", out.toString());
 66    assertContains("123", out.toString());
 67    assertContains("456", out.toString());
 68    assertContains("class=\"better", out.toString());
 69  }
 70
 71  public void testClassRemoved() throws Exception {
 72    Diff diff = new Diff(Arrays.asList(new Diff.ClassDiff("Foo", 456, null)));
 73    DiffReport diffReport = new DiffReport(diff, cfg);
 74    diffReport.writeHtml(out);
 75    assertContains("Foo", out.toString());
 76    assertContains("456", out.toString());
 77    assertContains("class=\"better", out.toString());
 78  }
 79
 80  public void testClassAdded() throws Exception {
 81    Diff diff = new Diff(Arrays.asList(new Diff.ClassDiff("Foo", null, 456)));
 82    DiffReport diffReport = new DiffReport(diff, cfg);
 83    diffReport.writeHtml(out);
 84    assertContains("Foo", out.toString());
 85    assertContains("456", out.toString());
 86    assertContains("class=\"worse", out.toString());
 87  }
 88
 89  public void testMethodRemoved() throws Exception {
 90    Diff.MethodDiff methodDiff = new Diff.MethodDiff("doThing", 123, null);
 91    Diff.ClassDiff classDiff = new Diff.ClassDiff("Foo", 1, 1, Arrays.asList(methodDiff));
 92    Diff diff = new Diff(Arrays.asList(classDiff));
 93    DiffReport diffReport = new DiffReport(diff, cfg);
 94    diffReport.writeHtml(out);
 95    assertContains("doThing", out.toString());
 96    assertContains("123", out.toString());
 97    assertContains("class=\"better", out.toString());
 98  }
 99
100  public void testMethodAdded() throws Exception {
101    Diff.MethodDiff methodDiff = new Diff.MethodDiff("doThing", null, 123);
102    Diff.ClassDiff classDiff = new Diff.ClassDiff("Foo", 1, 1, Arrays.asList(methodDiff));
103    Diff diff = new Diff(Arrays.asList(classDiff));
104    DiffReport diffReport = new DiffReport(diff, cfg);
105    diffReport.writeHtml(out);
106    assertContains("doThing", out.toString());
107    assertContains("123", out.toString());
108    assertContains("class=\"worse", out.toString());
109  }
110
111  public void testMethodCostIncreased() throws Exception {
112    Diff.MethodDiff methodDiff = new Diff.MethodDiff("doThing", 123, 456);
113    Diff.ClassDiff classDiff = new Diff.ClassDiff("Foo", 1, 1, Arrays.asList(methodDiff));
114    Diff diff = new Diff(Arrays.asList(classDiff));
115    DiffReport diffReport = new DiffReport(diff, cfg);
116    diffReport.writeHtml(out);
117    assertContains("doThing", out.toString());
118    assertContains("123", out.toString());
119    assertContains("456", out.toString());
120    assertContains("class=\"worse", out.toString());
121  }
122
123  public void testMethodCostDecreased() throws Exception {
124    Diff.MethodDiff methodDiff = new Diff.MethodDiff("doThing", 456, 123);
125    Diff.ClassDiff classDiff = new Diff.ClassDiff("Foo", 1, 1, Arrays.asList(methodDiff));
126    Diff diff = new Diff(Arrays.asList(classDiff));
127    DiffReport diffReport = new DiffReport(diff, cfg);
128    diffReport.writeHtml(out);
129    assertContains("doThing", out.toString());
130    assertContains("123", out.toString());
131    assertContains("456", out.toString());
132    assertContains("class=\"better", out.toString());
133  }
134
135  private void assertContains(String expected, String actual) {
136    assertTrue(String.format("[%s] expected to contain [%s]", actual, expected),
137        actual.contains(expected));
138  }
139}