PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

/maven-testability-plugin/src/main/java/com/google/maven/TestabilityExplorerMojo.java

http://testability-explorer.googlecode.com/
Java | 209 lines | 80 code | 31 blank | 98 comment | 1 complexity | 41c0033c2007256f22effa945de17dee MD5 | raw file
  1package com.google.maven;
  2
  3import com.google.classpath.ClassPath;
  4import com.google.classpath.ClassPathFactory;
  5import com.google.inject.Guice;
  6import com.google.test.metric.JavaTestabilityRunner;
  7
  8import org.apache.maven.artifact.DependencyResolutionRequiredException;
  9import org.apache.maven.project.MavenProject;
 10import org.apache.maven.reporting.AbstractMavenReport;
 11import org.codehaus.doxia.site.renderer.SiteRenderer;
 12
 13import java.io.File;
 14import java.util.List;
 15import java.util.Locale;
 16import java.util.ResourceBundle;
 17
 18/**
 19 * Executes the Testability Explorer with the specified parameters.
 20 *
 21 * @goal testability
 22 * @description Generates a Testability Report when the site plugin is run.
 23 * @execute phase="compile"
 24 * @requiresDependencyResolution compile
 25 * @requiresProject
 26 */
 27public class TestabilityExplorerMojo extends AbstractMavenReport {
 28
 29  /**
 30   * The root package to inspect.
 31   *
 32   * @parameter expression="."
 33   */
 34  String filter;
 35
 36  /**
 37   * The output directory for the intermediate XML report.
 38   *
 39   * @parameter expression="${project.build.directory}"
 40   * @required
 41   */
 42  File targetDirectory;
 43
 44  /**
 45   * The output directory for the final HTML report. Note that this parameter is only evaluated if the goal is run
 46   * directly from the command line or during the default lifecycle. If the goal is run indirectly as part of a site
 47   * generation, the output directory configured in the Maven Site Plugin is used instead.
 48   *
 49   * @parameter expression="${project.reporting.outputDirectory}"
 50   * @required
 51   */
 52  File outputDirectory;
 53
 54  /**
 55   * Filename of the output file, without the extension
 56   *
 57   * @parameter default-value="testability"
 58   */
 59  String resultfile = "testability";
 60
 61  /**
 62   * Where to write errors from execution
 63   *
 64   * @parameter
 65   */
 66  File errorfile;
 67
 68  /**
 69   * Weight of cyclomatic complexity cost
 70   *
 71   * @parameter default-value=1
 72   */
 73  Integer cyclomatic;
 74
 75  /**
 76   * Weight of global state cost
 77   *
 78   * @parameter default-value=10
 79   */
 80  Integer global;
 81
 82  /**
 83   * Extra multiplier applied to any costs where work is in a constructor
 84   *
 85   * @parameter default-value=1
 86   */
 87  Integer constructor;
 88
 89  /**
 90   * Maximum recursion depth of printed result
 91   *
 92   * @parameter default-value=2
 93   */
 94  Integer printDepth;
 95
 96  /**
 97   * Minimum cost to print a class metrics
 98   *
 99   * @parameter default-value=1
100   */
101  Integer minCost;
102
103  /**
104   * Max cost for a class to be called excellent
105   *
106   * @parameter default-value=50
107   */
108  Integer maxExcellentCost;
109
110  /**
111   * Max cost for a class to be called acceptable
112   *
113   * @parameter default-value=100
114   */
115  Integer maxAcceptableCost;
116
117  /**
118   * Print this many of the worst classes
119   *
120   * @parameter default-value=20
121   */
122  Integer worstOffenderCount;
123
124  /**
125   * Colon-delimited packages to whitelist
126   *
127   * @parameter default-value=" "
128   */
129  String whiteList;
130
131  /**
132   * Set the output format type, in addition to the HTML report.  Must be one of: "xml",
133   * "summary", "source", "detail".
134   * XML is required if the testability:check goal is being used.
135   *
136   * @parameter expression="${format}" default-value="xml"
137   */
138  String format = "xml";
139
140  /**
141   * @parameter expression="${project}"
142   * @required
143   * @readonly
144   */
145  MavenProject mavenProject;
146
147  /**
148   * @component
149   */
150  private SiteRenderer siteRenderer;
151  private static final String BUNDLE_NAME = "testability";
152  private static final String NAME_KEY = "report.testability.name";
153  private static final String DESCRIPTION_KEY = "report.testability.description";
154
155  @Override
156  protected SiteRenderer getSiteRenderer() {
157    return siteRenderer;
158  }
159
160  @Override
161  protected String getOutputDirectory() {
162    return outputDirectory.getAbsolutePath();
163  }
164
165  @Override
166  protected MavenProject getProject() {
167    return mavenProject;
168  }
169
170  @Override
171  protected void executeReport(Locale locale) {
172    if ("pom".equals(mavenProject.getPackaging())) {
173      getLog().info(String.format("Not running testability explorer for project %s " +
174          "because it is a \"pom\" packaging", mavenProject.getName()));
175      return;
176    }
177    Guice.createInjector(new MavenConfigModule(this)).
178        getInstance(JavaTestabilityRunner.class).
179        run();
180  }
181
182
183  public String getOutputName() {
184    return resultfile;
185  }
186
187  public String getName(Locale locale) {
188    return getProperty(locale, NAME_KEY);
189  }
190
191  private String getProperty(Locale locale, String key) {
192    return ResourceBundle.getBundle(BUNDLE_NAME, locale).getString(key);
193  }
194
195  public String getDescription(Locale locale) {
196    return getProperty(locale, DESCRIPTION_KEY);
197  }
198
199  ClassPath getProjectClasspath() {
200    List<String> compileClasspathElements;
201    try {
202      compileClasspathElements = mavenProject.getCompileClasspathElements();
203    } catch (DependencyResolutionRequiredException e) {
204      throw new RuntimeException(e);
205    }
206    return new ClassPathFactory().createFromPaths(
207        compileClasspathElements.toArray(new String[compileClasspathElements.size()]));
208  }
209}