PageRenderTime 20ms CodeModel.GetById 3ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/ReportGeneratorProvider.java

http://testability-explorer.googlecode.com/
Java | 176 lines | 132 code | 15 blank | 29 comment | 2 complexity | 18e095947ed9b6d244102f3b21d4dda8 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;
 17
 18import com.google.classpath.ClassPath;
 19import com.google.inject.Inject;
 20import com.google.inject.Provider;
 21import com.google.test.metric.ConfigModule.Output;
 22import com.google.test.metric.report.ClassPathTemplateLoader;
 23import com.google.test.metric.report.FreemarkerReportGenerator;
 24import com.google.test.metric.report.GradeCategories;
 25import com.google.test.metric.report.PropertiesReportGenerator;
 26import com.google.test.metric.report.ReportGenerator;
 27import com.google.test.metric.report.ReportModel;
 28import com.google.test.metric.report.ReportOptions;
 29import com.google.test.metric.report.SourceLinker;
 30import com.google.test.metric.report.SourceLoader;
 31import com.google.test.metric.report.SourceReportGenerator;
 32import com.google.test.metric.report.TextReportGenerator;
 33import com.google.test.metric.report.XMLReportGenerator;
 34import com.google.test.metric.report.about.AboutTestabilityReport;
 35import com.google.test.metric.report.html.HtmlReportModel;
 36import com.google.test.metric.report.html.SourceLinkerModel;
 37import com.google.test.metric.report.issues.ClassIssues;
 38import com.google.test.metric.report.issues.HypotheticalCostModel;
 39import com.google.test.metric.report.issues.IssuesReporter;
 40import com.google.test.metric.report.issues.TriageIssuesQueue;
 41
 42import freemarker.ext.beans.BeansWrapper;
 43import freemarker.ext.beans.ResourceBundleModel;
 44import freemarker.template.Configuration;
 45import freemarker.template.DefaultObjectWrapper;
 46
 47import org.apache.xml.serialize.OutputFormat;
 48import org.apache.xml.serialize.XMLSerializer;
 49
 50import java.io.File;
 51import java.io.PrintStream;
 52import java.util.Date;
 53import static java.util.ResourceBundle.getBundle;
 54
 55/**
 56 * Builds a Report, using various formats, and given all of the needed options.
 57 *
 58 * @author alexeagle@google.com (Alex Eagle)
 59 */
 60public class ReportGeneratorProvider implements Provider<ReportGenerator> {
 61
 62  public static final String PREFIX = "com/google/test/metric/report/";
 63  private final ClassPath classPath;
 64  private final ReportOptions options;
 65  private final PrintStream out;
 66  private final HypotheticalCostModel hypotheticalCostModel;
 67  private final ReportFormat reportFormat;
 68
 69  @Inject
 70  public ReportGeneratorProvider(ClassPath classPath, ReportOptions options,
 71                                 @Output PrintStream out,
 72                                 HypotheticalCostModel hypotheticalCostModel,
 73                                 ReportFormat reportFormat) {
 74    this.classPath = classPath;
 75    this.options = options;
 76    this.hypotheticalCostModel = hypotheticalCostModel;
 77    this.out = out;
 78    this.reportFormat = reportFormat;
 79  }
 80
 81  public enum ReportFormat {
 82    summary,
 83    detail,
 84    xml,
 85    props,
 86    source,
 87    html,
 88    about
 89  }
 90
 91  /**
 92   * Method to allow retaining a handle on preconfigured model objects.
 93   *
 94   * @param costModel Cost Model for the {@link ReportGenerator}
 95   * @param reportModel Can be {@code null} if {@link ReportFormat} is not
 96   *    {@link ReportFormat#html} or {@link ReportFormat#about}
 97   * @param sourceLoader Source Loader used by {@link ReportFormat#source} reports.
 98   * @return a ready to use {@link ReportGenerator}
 99   */
100  public ReportGenerator build(CostModel costModel, ReportModel reportModel,
101      SourceLoader sourceLoader) {
102    SourceLinker linker = new SourceLinker(
103        options.getSrcFileLineUrl(), options.getSrcFileUrl());
104    Configuration cfg = new Configuration();
105    cfg.setTemplateLoader(new ClassPathTemplateLoader(PREFIX));
106    BeansWrapper objectWrapper = new DefaultObjectWrapper();
107    cfg.setObjectWrapper(objectWrapper);
108    ResourceBundleModel bundleModel = new ResourceBundleModel(getBundle("messages"),
109        objectWrapper);
110
111    ReportGenerator report;
112    switch (reportFormat) {
113      case summary:
114        report = new TextReportGenerator(out, costModel, options);
115        break;
116      case html:
117        reportModel.setMessageBundle(bundleModel);
118        reportModel.setSourceLinker(new SourceLinkerModel(linker));
119        report = new FreemarkerReportGenerator(reportModel, out,
120            FreemarkerReportGenerator.HTML_REPORT_TEMPLATE, cfg);
121        break;
122      case props:
123        report = new PropertiesReportGenerator(out, costModel);
124        break;
125      case source:
126        GradeCategories gradeCategories = new GradeCategories(options.getMaxExcellentCost(),
127            options.getMaxAcceptableCost());
128        report = new SourceReportGenerator(gradeCategories, sourceLoader,
129            new File("te-report"), costModel, new Date(), options.getWorstOffenderCount(), cfg);
130        break;
131      case xml:
132        XMLSerializer xmlSerializer = new XMLSerializer();
133        xmlSerializer.setOutputByteStream(out);
134        OutputFormat format = new OutputFormat();
135        format.setIndenting(true);
136        xmlSerializer.setOutputFormat(format);
137        report = new XMLReportGenerator(xmlSerializer, costModel, options);
138        break;
139      case about:
140        reportModel.setMessageBundle(bundleModel);
141        reportModel.setSourceLinker(new SourceLinkerModel(linker));
142        report = new FreemarkerReportGenerator(reportModel, out, "about/Report.html", cfg);
143        break;
144      default:
145        throw new IllegalStateException("Unknown report format " + reportFormat);
146    }
147    return report;
148  }
149
150  public ReportGenerator get() {
151    CostModel costModel = new CostModel(options.getCyclomaticMultiplier(),
152        options.getGlobalMultiplier(), options.getConstructorMultiplier());
153    TriageIssuesQueue<ClassIssues> mostImportantIssues =
154        new TriageIssuesQueue<ClassIssues>(options.getMaxExcellentCost(),
155            options.getWorstOffenderCount(), new ClassIssues.TotalCostComparator());
156    SourceLoader sourceLoader = new SourceLoader(classPath);
157
158    IssuesReporter issuesReporter = new IssuesReporter(mostImportantIssues, hypotheticalCostModel);
159    AnalysisModel analysisModel = new AnalysisModel(issuesReporter);
160    ReportModel reportModel;
161
162    switch (reportFormat) {
163      case html:
164        reportModel = new HtmlReportModel(costModel, analysisModel, options);
165        break;
166
167      case about:
168        reportModel = new AboutTestabilityReport(issuesReporter, sourceLoader);
169        break;
170
171      default:
172        reportModel = null;
173    }
174    return build(costModel, reportModel, sourceLoader);
175  }
176}