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

http://testability-explorer.googlecode.com/ · Java · 105 lines · 89 code · 12 blank · 4 comment · 7 complexity · 1fb6958380f6ff5b3983bd45fd8f812e MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. package com.google.maven;
  3. import com.google.classpath.ClassPath;
  4. import com.google.classpath.ClassPathFactory;
  5. import com.google.inject.AbstractModule;
  6. import com.google.inject.Provides;
  7. import com.google.inject.TypeLiteral;
  8. import com.google.inject.name.Names;
  9. import com.google.test.metric.ClassRepository;
  10. import com.google.test.metric.ConfigModule.Error;
  11. import com.google.test.metric.ConfigModule.Output;
  12. import com.google.test.metric.JavaClassRepository;
  13. import com.google.test.metric.JavaTestabilityRunner;
  14. import com.google.test.metric.RegExpWhiteList;
  15. import com.google.test.metric.ReportGeneratorProvider;
  16. import com.google.test.metric.ReportGeneratorProvider.ReportFormat;
  17. import com.google.test.metric.WhiteList;
  18. import com.google.test.metric.report.MultiReportGenerator;
  19. import com.google.test.metric.report.ReportGenerator;
  20. import com.google.test.metric.report.ReportOptions;
  21. import com.google.test.metric.report.issues.HypotheticalCostModel;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.FileOutputStream;
  25. import java.io.PrintStream;
  26. import java.util.Arrays;
  27. import java.util.List;
  28. /**
  29. * @author alexeagle@google.com (Alex Eagle)
  30. */
  31. public class MavenConfigModule extends AbstractModule {
  32. private TestabilityExplorerMojo testabilityExplorerMojo;
  33. public MavenConfigModule(TestabilityExplorerMojo testabilityExplorerMojo) {
  34. this.testabilityExplorerMojo = testabilityExplorerMojo;
  35. }
  36. @Override
  37. protected void configure() {
  38. bind(ClassPath.class).toInstance(new ClassPathFactory().createFromPath(
  39. testabilityExplorerMojo.mavenProject.getBuild().getOutputDirectory()));
  40. ReportOptions options = new ReportOptions(testabilityExplorerMojo.cyclomatic,
  41. testabilityExplorerMojo.global, testabilityExplorerMojo.constructor,
  42. testabilityExplorerMojo.maxExcellentCost,
  43. testabilityExplorerMojo.maxAcceptableCost,
  44. testabilityExplorerMojo.worstOffenderCount, 0, 0, testabilityExplorerMojo.printDepth,
  45. testabilityExplorerMojo.minCost, "", "");
  46. bind(ReportOptions.class).toInstance(options);
  47. bind(TestabilityExplorerMojo.class).toInstance(testabilityExplorerMojo);
  48. bind(WhiteList.class).toInstance(new RegExpWhiteList(testabilityExplorerMojo.whiteList));
  49. bind(ReportFormat.class).toInstance(ReportFormat.valueOf(testabilityExplorerMojo.format));
  50. bindConstant().annotatedWith(Names.named("printDepth")).to(testabilityExplorerMojo.printDepth);
  51. bind(new TypeLiteral<List<String>>() {}).toInstance(Arrays.asList(testabilityExplorerMojo.filter));
  52. bind(Runnable.class).to(JavaTestabilityRunner.class);
  53. }
  54. @Provides ReportGenerator generateHtmlReportAsWellAsRequestedFormat(
  55. ReportGeneratorProvider requestedReportProvider,
  56. ClassPath classPath, ReportOptions options,
  57. HypotheticalCostModel hypotheticalCostModel,
  58. TestabilityExplorerMojo mojo,
  59. ReportFormat requestedFormat) {
  60. if (requestedFormat == ReportFormat.html) {
  61. return requestedReportProvider.get();
  62. }
  63. ReportGenerator htmlReportGenerator =
  64. new ReportGeneratorProvider(classPath, options, getOutput(mojo, ReportFormat.html),
  65. hypotheticalCostModel, ReportFormat.html).get();
  66. return new MultiReportGenerator(htmlReportGenerator, requestedReportProvider.get());
  67. }
  68. @Provides ClassRepository getClassRepo(TestabilityExplorerMojo mojo) {
  69. return new JavaClassRepository(mojo.getProjectClasspath());
  70. }
  71. @Provides @Output PrintStream getOutput(TestabilityExplorerMojo mojo, ReportFormat format) {
  72. File directory = (format == ReportFormat.html ? mojo.outputDirectory : mojo.targetDirectory);
  73. if (!directory.exists()) {
  74. directory.mkdirs();
  75. }
  76. try {
  77. String outFile = mojo.resultfile + "." + format.toString();
  78. return new PrintStream(new FileOutputStream(new File(directory, outFile)));
  79. } catch (FileNotFoundException e) {
  80. throw new RuntimeException(e);
  81. }
  82. }
  83. @Provides @Error PrintStream getError(TestabilityExplorerMojo mojo) {
  84. if (mojo.errorfile != null && mojo.errorfile.exists()) {
  85. try {
  86. return new PrintStream(new FileOutputStream(mojo.errorfile));
  87. } catch (FileNotFoundException e) {
  88. throw new RuntimeException(e);
  89. }
  90. }
  91. return System.err;
  92. }
  93. }