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