From: Evgeny Mandrikov Date: Sat, 24 Mar 2012 21:00:37 +0000 (+0600) Subject: SONAR-3295 sonar.jacoco.excludes must exclude classes from coverage report X-Git-Tag: 3.0~134 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cf3eaf5efb2b200607689c8978c872c84e8ae794;p=sonarqube.git SONAR-3295 sonar.jacoco.excludes must exclude classes from coverage report --- diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java index 5042d506bcb..6ce0673318e 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java @@ -19,6 +19,7 @@ */ package org.sonar.plugins.jacoco; +import com.google.common.annotations.VisibleForTesting; import org.apache.commons.lang.StringUtils; import org.jacoco.core.analysis.*; import org.jacoco.core.data.ExecutionDataReader; @@ -30,6 +31,7 @@ import org.sonar.api.measures.Measure; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.Project; import org.sonar.api.utils.SonarException; +import org.sonar.api.utils.WildcardPattern; import java.io.File; import java.io.FileInputStream; @@ -49,14 +51,15 @@ public abstract class AbstractAnalyzer { } String path = getReportPath(project); File jacocoExecutionData = project.getFileSystem().resolvePath(path); + WildcardPattern[] excludes = WildcardPattern.create(getExcludes(project)); try { - readExecutionData(jacocoExecutionData, buildOutputDir, context); + readExecutionData(jacocoExecutionData, buildOutputDir, context, excludes); } catch (IOException e) { throw new SonarException(e); } } - public final void readExecutionData(File jacocoExecutionData, File buildOutputDir, SensorContext context) throws IOException { + public final void readExecutionData(File jacocoExecutionData, File buildOutputDir, SensorContext context, WildcardPattern[] excludes) throws IOException { SessionInfoStore sessionInfoStore = new SessionInfoStore(); ExecutionDataStore executionDataStore = new ExecutionDataStore(); @@ -79,7 +82,9 @@ public abstract class AbstractAnalyzer { JavaFile resource = getResource(coverage); // Do not save measures on resource which doesn't exist in the context if (context.getResource(resource) != null) { - analyzeFile(resource, coverage, context); + if (!isExcluded(coverage, excludes)) { + analyzeFile(resource, coverage, context); + } analyzedResources++; } } @@ -88,6 +93,17 @@ public abstract class AbstractAnalyzer { } } + private static boolean isExcluded(ISourceFileCoverage coverage, WildcardPattern[] excludes) { + String name = coverage.getPackageName() + "/" + coverage.getName(); + for (WildcardPattern pattern : excludes) { + if (pattern.match(name)) { + return true; + } + } + return false; + } + + @VisibleForTesting static JavaFile getResource(ISourceFileCoverage coverage) { String packageName = StringUtils.replaceChars(coverage.getPackageName(), '/', '.'); String fileName = StringUtils.substringBeforeLast(coverage.getName(), "."); @@ -147,4 +163,6 @@ public abstract class AbstractAnalyzer { protected abstract String getReportPath(Project project); + protected abstract String[] getExcludes(Project project); + } diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java index 37cc58a21e0..ae19078aef7 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java @@ -57,6 +57,11 @@ public class JaCoCoItSensor implements Sensor { return configuration.getItReportPath(); } + @Override + protected String[] getExcludes(Project project) { + return configuration.getExcludes(); + } + @Override protected void saveMeasures(SensorContext context, JavaFile resource, Collection measures) { for (Measure measure : measures) { diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoSensor.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoSensor.java index 63671d43515..d40b17d5141 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoSensor.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoSensor.java @@ -54,6 +54,11 @@ public class JaCoCoSensor implements Sensor, CoverageExtension { return configuration.getReportPath(); } + @Override + protected String[] getExcludes(Project project) { + return configuration.getExcludes(); + } + @Override protected void saveMeasures(SensorContext context, JavaFile resource, Collection measures) { for (Measure measure : measures) { diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoConfiguration.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoConfiguration.java index ea442494987..986d6694ab6 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoConfiguration.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoConfiguration.java @@ -144,4 +144,10 @@ public class JacocoConfiguration implements BatchExtension { public String[] getAntTargets() { return settings.getStringArray(ANT_TARGETS_PROPERTY); } + + public String[] getExcludes() { + String values = settings.getString(EXCLUDES_PROPERTY); + return StringUtils.split(values, ':'); + } + }