PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 138 lines | 109 code | 13 blank | 16 comment | 0 complexity | c33d66d9e561f600083341f40165fb9b 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.html;
 17
 18import com.google.common.collect.Lists;
 19import com.google.test.metric.AnalysisModel;
 20import com.google.test.metric.ClassCost;
 21import com.google.test.metric.Cost;
 22import com.google.test.metric.CostModel;
 23import com.google.test.metric.MethodCost;
 24import com.google.test.metric.MethodInvocationCost;
 25import com.google.test.metric.Reason;
 26import com.google.test.metric.ReportGeneratorProvider;
 27import com.google.test.metric.SourceLocation;
 28import com.google.test.metric.report.ClassPathTemplateLoader;
 29import com.google.test.metric.report.FreemarkerReportGenerator;
 30import static com.google.test.metric.report.FreemarkerReportGenerator.HTML_REPORT_TEMPLATE;
 31import com.google.test.metric.report.ReportOptions;
 32import com.google.test.metric.report.SourceLinker;
 33import com.google.test.metric.report.issues.ClassIssues;
 34import com.google.test.metric.report.issues.Issue;
 35import com.google.test.metric.report.issues.IssueSubType;
 36import com.google.test.metric.report.issues.IssueType;
 37import com.google.test.metric.report.issues.IssuesReporter;
 38
 39import freemarker.ext.beans.BeansWrapper;
 40import freemarker.ext.beans.ResourceBundleModel;
 41import freemarker.template.Configuration;
 42import freemarker.template.DefaultObjectWrapper;
 43
 44import junit.framework.TestCase;
 45
 46import java.io.ByteArrayOutputStream;
 47import java.io.PrintStream;
 48import static java.text.MessageFormat.format;
 49import java.util.Arrays;
 50import java.util.LinkedList;
 51import java.util.List;
 52import java.util.ResourceBundle;
 53import static java.util.ResourceBundle.getBundle;
 54
 55public class HtmlReportTest extends TestCase {
 56  private FreemarkerReportGenerator generator;
 57  private ByteArrayOutputStream out;
 58  private ClassCost cost;
 59  private List<ClassIssues> importantIssues = Lists.newLinkedList();
 60
 61  @Override
 62  protected void setUp() throws Exception {
 63    super.setUp();
 64    out = new ByteArrayOutputStream();
 65    CostModel costModel = new CostModel();
 66    SourceLocation location = new SourceLocation("com/google/FooClass.java", 1);
 67    final Issue issue =
 68        new Issue(location, "void doThing()", 0.5f, IssueType.CONSTRUCTION, IssueSubType.SETTER);
 69    final MethodCost methodCost = new MethodCost("", "void setUp()", 1, false, false, false);
 70    methodCost.addCostSource(new MethodInvocationCost(location, methodCost,
 71        Reason.IMPLICIT_SETTER, new Cost(100, 1, new int[0])));
 72
 73    //TODO: looks like we want IssuesReporter to be an interface
 74    IssuesReporter issuesReporter = new IssuesReporter(null, null) {
 75      @Override
 76      public List<ClassIssues> getMostImportantIssues() {
 77        ClassCost aClassCost = new ClassCost("com/google/FooClass", Arrays.asList(methodCost));
 78        importantIssues.add(determineIssues(aClassCost));
 79        return importantIssues;
 80      }
 81
 82      @Override
 83      public void inspectClass(ClassCost classCost) {
 84      }
 85
 86      @Override
 87      public ClassIssues determineIssues(ClassCost classCost) {
 88        return new ClassIssues("com/google/FooClass", 100, new LinkedList<Issue>(Arrays.asList(issue)));
 89      }
 90    };
 91    ReportOptions options = new ReportOptions(1, 10, 1, 10, 20, 5, 100, 100, 1, 10, "", "");
 92    SourceLinker linker = new SourceLinker("http://code.repository/basepath/{path}&line={line}",
 93        "http://code.repository/basepath/{path}");
 94
 95    cost = new ClassCost(getClass().getName(), Arrays.asList(methodCost));
 96    HtmlReportModel report =
 97        new HtmlReportModel(costModel, new AnalysisModel(issuesReporter), options);
 98    BeansWrapper objectWrapper = new DefaultObjectWrapper();
 99    Configuration configuration = new Configuration();
100    configuration.setObjectWrapper(objectWrapper);
101    ResourceBundleModel bundleModel = new ResourceBundleModel(getBundle("messages"), objectWrapper);
102    configuration.setTemplateLoader(new ClassPathTemplateLoader(ReportGeneratorProvider.PREFIX));
103    report.setMessageBundle(bundleModel);
104    report.setSourceLinker(new SourceLinkerModel(linker));
105    generator = new FreemarkerReportGenerator(report, new PrintStream(out),
106        HTML_REPORT_TEMPLATE, configuration);
107  }
108
109  public void testPrintReport() throws Exception {
110    generator.printHeader();
111    generator.addClassCost(cost);
112    generator.printFooter();
113    String text = out.toString();
114
115    assertTrue(text, text.contains("<script type=\"text/javascript\""));
116    assertTrue(text, text.contains("Report generated"));
117    assertTrue(text, text.contains("function toggle(element)"));
118    assertTrue(text, text.contains("function clickHandler(event)"));
119    assertTrue(text, text
120        .contains("<a href=\"http://code.repository/basepath/com/google/FooClass.java&line"));
121  }
122
123  public void testConstructorCosts() throws Exception {
124    ClassCost cost =
125        new ClassCost("classFoo", Arrays.asList(new MethodCost("", "methodFoo", 1, false, false, false)));
126    ClassIssues classIssues = new ClassIssues(cost.getClassName(), 0);
127    classIssues.add(new Issue(new SourceLocation("", 1), null, 1f, null, null));
128    importantIssues.add(classIssues);
129    generator.printHeader();
130    generator.addClassCost(cost);
131    generator.printFooter();
132    String text = out.toString();
133    ResourceBundle bundle = ResourceBundle.getBundle("messages");
134    String expected =
135        format(bundle.getString("report.explain.class.hardToTest"), "<tt>", "classFoo", "</tt>");
136    assertTrue(text, text.contains(expected));
137  }
138}