() {
@Override
public void execute(JavaPlugin javaPlugin) {
@@ -212,6 +214,7 @@ public void execute(JavaPlugin javaPlugin) {
public void execute(Test task) {
if (task.getName().equals(JavaPlugin.TEST_TASK_NAME)) {
addDefaultReportTask(extension, task);
+ addDefaultCoverageVerificationTask(task);
}
}
});
@@ -221,6 +224,8 @@ public void execute(Test task) {
private void addDefaultReportTask(final JacocoPluginExtension extension, final Test task) {
final JacocoReport reportTask = project.getTasks().create("jacoco" + StringUtils.capitalise(task.getName()) + "Report", JacocoReport.class);
+ reportTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
+ reportTask.setDescription(String.format("Generates code coverage report for the %s task.", task.getName()));
reportTask.executionData(task);
reportTask.sourceSets(project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName("main"));
ConventionMapping taskMapping = ((IConventionAware) reportTask).getConventionMapping();
@@ -247,4 +252,12 @@ public File call() {
}
});
}
+
+ private void addDefaultCoverageVerificationTask(final Test task) {
+ final JacocoCoverageVerification coverageVerificationTask = project.getTasks().create("jacoco" + StringUtils.capitalise(task.getName()) + "CoverageVerification", JacocoCoverageVerification.class);
+ coverageVerificationTask.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
+ coverageVerificationTask.setDescription(String.format("Verifies code coverage metrics based on specified rules for the %s task.", task.getName()));
+ coverageVerificationTask.executionData(task);
+ coverageVerificationTask.sourceSets(project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName("main"));
+ }
}
diff --git a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoCoverageVerification.java b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoCoverageVerification.java
new file mode 100644
index 000000000000..e3dce19532ce
--- /dev/null
+++ b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoCoverageVerification.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.testing.jacoco.tasks;
+
+import groovy.lang.Closure;
+import groovy.lang.DelegatesTo;
+import org.gradle.api.Action;
+import org.gradle.api.Incubating;
+import org.gradle.api.internal.ClosureBackedAction;
+import org.gradle.api.tasks.Nested;
+import org.gradle.api.tasks.TaskAction;
+import org.gradle.internal.jacoco.AntJacocoCheck;
+import org.gradle.internal.jacoco.rules.JacocoViolationRulesContainerImpl;
+import org.gradle.testing.jacoco.tasks.rules.JacocoViolationRulesContainer;
+
+import static groovy.lang.Closure.DELEGATE_FIRST;
+
+/**
+ * Task for verifying code coverage metrics. Fails the task if violations are detected based on specified rules.
+ *
+ * Requires JaCoCo version >= 0.6.3.
+ *
+ * @since 3.4
+ */
+@Incubating
+public class JacocoCoverageVerification extends JacocoReportBase {
+
+ private final JacocoViolationRulesContainer violationRules;
+
+ public JacocoCoverageVerification() {
+ super();
+ violationRules = getInstantiator().newInstance(JacocoViolationRulesContainerImpl.class);
+ }
+
+ /**
+ * Returns the violation rules set for this task.
+ *
+ * @return Violation rules container
+ */
+ @Nested
+ public JacocoViolationRulesContainer getViolationRules() {
+ return violationRules;
+ }
+
+ /**
+ * Configures the violation rules for this task.
+ */
+ @Incubating
+ public JacocoViolationRulesContainer violationRules(@DelegatesTo(value = JacocoViolationRulesContainer.class, strategy = DELEGATE_FIRST) Closure closure) {
+ return violationRules(new ClosureBackedAction(closure));
+ }
+
+ /**
+ * Configures the violation rules for this task.
+ */
+ @Incubating
+ public JacocoViolationRulesContainer violationRules(Action super JacocoViolationRulesContainer> configureAction) {
+ configureAction.execute(violationRules);
+ return violationRules;
+ }
+
+ @TaskAction
+ public void check() {
+ new AntJacocoCheck(getAntBuilder()).execute(
+ getJacocoClasspath(),
+ getProject().getName(),
+ getAllClassDirs().filter(fileExistsSpec),
+ getAllSourceDirs().filter(fileExistsSpec),
+ getExecutionData(),
+ getViolationRules()
+ );
+ }
+}
diff --git a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoReport.java b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoReport.java
index 3818b9923766..db2117e0f082 100644
--- a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoReport.java
+++ b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoReport.java
@@ -15,79 +15,29 @@
*/
package org.gradle.testing.jacoco.tasks;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.api.Incubating;
-import org.gradle.api.Project;
-import org.gradle.api.Task;
-import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.ClosureBackedAction;
-import org.gradle.api.internal.project.IsolatedAntBuilder;
import org.gradle.api.reporting.Reporting;
-import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.CacheableTask;
-import org.gradle.api.tasks.InputFiles;
-import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
-import org.gradle.api.tasks.Optional;
-import org.gradle.api.tasks.PathSensitive;
-import org.gradle.api.tasks.PathSensitivity;
-import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
-import org.gradle.api.tasks.TaskCollection;
import org.gradle.internal.jacoco.AntJacocoReport;
import org.gradle.internal.jacoco.JacocoReportsContainerImpl;
-import org.gradle.internal.reflect.Instantiator;
-import org.gradle.testing.jacoco.plugins.JacocoTaskExtension;
-
-import javax.inject.Inject;
-import java.io.File;
-import java.util.Arrays;
-import java.util.concurrent.Callable;
/**
* Task to generate HTML, Xml and CSV reports of Jacoco coverage data.
*/
@CacheableTask
@Incubating
-public class JacocoReport extends JacocoBase implements Reporting {
+public class JacocoReport extends JacocoReportBase implements Reporting {
private final JacocoReportsContainer reports;
- private FileCollection executionData;
- private FileCollection sourceDirectories;
- private FileCollection classDirectories;
- private FileCollection additionalClassDirs;
- private FileCollection additionalSourceDirs;
-
public JacocoReport() {
+ super();
reports = getInstantiator().newInstance(JacocoReportsContainerImpl.class, this);
- onlyIf(new Spec() {
- @Override
- public boolean isSatisfiedBy(Task element) {
- //TODO SF it should be 'any' instead of 'all'
- return Iterables.all(getExecutionData(), new Predicate() {
- @Override
- public boolean apply(File file) {
- return file.exists();
- }
-
- });
- }
-
- });
- }
-
- @Inject
- protected Instantiator getInstantiator() {
- throw new UnsupportedOperationException();
- }
-
- @Inject
- protected IsolatedAntBuilder getAntBuilder() {
- throw new UnsupportedOperationException();
}
/**
@@ -113,81 +63,8 @@ public JacocoReportsContainer reports(Action super JacocoReportsContainer> con
return reports;
}
- /**
- * Collection of execution data files to analyze.
- */
- @PathSensitive(PathSensitivity.NONE)
- @InputFiles
- public FileCollection getExecutionData() {
- return executionData;
- }
-
- public void setExecutionData(FileCollection executionData) {
- this.executionData = executionData;
- }
-
- /**
- * Source sets that coverage should be reported for.
- */
- @PathSensitive(PathSensitivity.RELATIVE)
- @InputFiles
- public FileCollection getSourceDirectories() {
- return sourceDirectories;
- }
-
- public void setSourceDirectories(FileCollection sourceDirectories) {
- this.sourceDirectories = sourceDirectories;
- }
-
- /**
- * Source sets that coverage should be reported for.
- */
- @PathSensitive(PathSensitivity.RELATIVE)
- @InputFiles
- public FileCollection getClassDirectories() {
- return classDirectories;
- }
-
- public void setClassDirectories(FileCollection classDirectories) {
- this.classDirectories = classDirectories;
- }
-
- /**
- * Additional class dirs that coverage data should be reported for.
- */
- @Optional
- @PathSensitive(PathSensitivity.RELATIVE)
- @InputFiles
- public FileCollection getAdditionalClassDirs() {
- return additionalClassDirs;
- }
-
- public void setAdditionalClassDirs(FileCollection additionalClassDirs) {
- this.additionalClassDirs = additionalClassDirs;
- }
-
- /**
- * Additional source dirs for the classes coverage data is being reported for.
- */
- @Optional
- @PathSensitive(PathSensitivity.RELATIVE)
- @InputFiles
- public FileCollection getAdditionalSourceDirs() {
- return additionalSourceDirs;
- }
-
- public void setAdditionalSourceDirs(FileCollection additionalSourceDirs) {
- this.additionalSourceDirs = additionalSourceDirs;
- }
-
@TaskAction
public void generate() {
- Spec fileExistsSpec = new Spec() {
- @Override
- public boolean isSatisfiedBy(File file) {
- return file.exists();
- }
- };
new AntJacocoReport(getAntBuilder()).execute(
getJacocoClasspath(),
getProject().getName(),
@@ -197,150 +74,4 @@ public boolean isSatisfiedBy(File file) {
getReports()
);
}
-
- /**
- * Adds execution data files to be used during coverage analysis.
- *
- * @param files one or more files to add
- */
- public void executionData(Object... files) {
- if (executionData == null) {
- executionData = getProject().files(files);
- } else {
- executionData = executionData.plus(getProject().files(files));
- }
- }
-
- /**
- * Adds execution data generated by a task to the list of those used during coverage analysis. Only tasks with a {@link JacocoTaskExtension} will be included; all others will be ignored.
- *
- * @param tasks one or more tasks to add
- */
- public void executionData(Task... tasks) {
- for (Task task : tasks) {
- final JacocoTaskExtension extension = task.getExtensions().findByType(JacocoTaskExtension.class);
- if (extension != null) {
- executionData(new Callable() {
- @Override
- public File call() {
- return extension.getDestinationFile();
- }
- });
- mustRunAfter(task);
- }
- }
- }
-
- /**
- * Adds execution data generated by the given tasks to the list of those used during coverage analysis. Only tasks with a {@link JacocoTaskExtension} will be included; all others will be ignored.
- *
- * @param tasks one or more tasks to add
- */
- public void executionData(TaskCollection tasks) {
- tasks.all(new Action() {
- @Override
- public void execute(Task task) {
- executionData(task);
- }
- });
- }
-
- /**
- * Gets the class directories that coverage will be reported for. All classes in these directories will be included in the report.
- *
- * @return class dirs to report coverage of
- */
- @Internal
- public FileCollection getAllClassDirs() {
- FileCollection additionalDirs = getAdditionalClassDirs();
- if (additionalDirs == null) {
- return classDirectories;
- }
- return classDirectories.plus(getAdditionalClassDirs());
- }
-
- /**
- * Gets the source directories for the classes that will be reported on. Source will be obtained from these directories only for the classes included in the report.
- *
- * @return source directories for the classes reported on
- * @see #getAllClassDirs()
- */
- @Internal
- public FileCollection getAllSourceDirs() {
- FileCollection additionalDirs = getAdditionalSourceDirs();
- if (additionalDirs == null) {
- return sourceDirectories;
- }
- return sourceDirectories.plus(getAdditionalSourceDirs());
- }
-
- /**
- * Adds a source set to the list to be reported on. The output of this source set will be used as classes to include in the report. The source for this source set will be used for any classes
- * included in the report.
- *
- * @param sourceSets one or more source sets to report on
- */
- public void sourceSets(final SourceSet... sourceSets) {
- getProject().afterEvaluate(new Action() {
- @Override
- public void execute(Project project) {
- for (SourceSet sourceSet : sourceSets) {
- if (getSourceDirectories() == null) {
- setSourceDirectories(getProject().files(sourceSet.getAllJava().getSrcDirs()));
- } else {
- setSourceDirectories(getSourceDirectories().plus(getProject().files(sourceSet.getAllJava().getSrcDirs())));
- }
- if (getClassDirectories() == null) {
- setClassDirectories(sourceSet.getOutput());
- } else {
- setClassDirectories(getClassDirectories().plus(sourceSet.getOutput()));
- }
- }
- }
- });
- }
-
- /**
- * Adds additional class directories to those that will be included in the report.
- *
- * @param dirs one or more directories containing classes to report coverage of
- */
- public void additionalClassDirs(File... dirs) {
- additionalClassDirs(getProject().files(Arrays.asList(dirs)));
- }
-
- /**
- * Adds additional class directories to those that will be included in the report.
- *
- * @param dirs a {@code FileCollection} of directories containing classes to report coverage of
- */
- public void additionalClassDirs(FileCollection dirs) {
- if (additionalClassDirs == null) {
- additionalClassDirs = dirs;
- } else {
- additionalClassDirs = additionalClassDirs.plus(dirs);
- }
- }
-
- /**
- * Adds additional source directories to be used for any classes included in the report.
- *
- * @param dirs one or more directories containing source files for the classes included in the report
- */
- public void additionalSourceDirs(File... dirs) {
- additionalSourceDirs(getProject().files(Arrays.asList(dirs)));
- }
-
- /**
- * Adds additional source directories to be used for any classes included in the report.
- *
- * @param dirs a {@code FileCollection} of directories containing source files for the classes included in the report
- */
- public void additionalSourceDirs(FileCollection dirs) {
- if (additionalSourceDirs == null) {
- additionalSourceDirs = dirs;
- } else {
- additionalSourceDirs = additionalSourceDirs.plus(dirs);
- }
- }
}
diff --git a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoReportBase.java b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoReportBase.java
new file mode 100644
index 000000000000..09f2eea09d80
--- /dev/null
+++ b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/JacocoReportBase.java
@@ -0,0 +1,298 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.testing.jacoco.tasks;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import org.gradle.api.Action;
+import org.gradle.api.Incubating;
+import org.gradle.api.Project;
+import org.gradle.api.Task;
+import org.gradle.api.file.FileCollection;
+import org.gradle.api.internal.project.IsolatedAntBuilder;
+import org.gradle.api.specs.Spec;
+import org.gradle.api.tasks.InputFiles;
+import org.gradle.api.tasks.Internal;
+import org.gradle.api.tasks.Optional;
+import org.gradle.api.tasks.PathSensitive;
+import org.gradle.api.tasks.PathSensitivity;
+import org.gradle.api.tasks.SourceSet;
+import org.gradle.api.tasks.TaskCollection;
+import org.gradle.internal.reflect.Instantiator;
+import org.gradle.testing.jacoco.plugins.JacocoTaskExtension;
+
+import javax.inject.Inject;
+import java.io.File;
+import java.util.Arrays;
+import java.util.concurrent.Callable;
+
+/**
+ * Base class for Jacoco report tasks.
+ */
+@Incubating
+public abstract class JacocoReportBase extends JacocoBase {
+
+ protected final Spec fileExistsSpec = new Spec() {
+ @Override
+ public boolean isSatisfiedBy(File file) {
+ return file.exists();
+ }
+ };
+
+ private FileCollection executionData;
+ private FileCollection sourceDirectories;
+ private FileCollection classDirectories;
+ private FileCollection additionalClassDirs;
+ private FileCollection additionalSourceDirs;
+
+ public JacocoReportBase() {
+ onlyIf(new Spec() {
+ @Override
+ public boolean isSatisfiedBy(Task element) {
+ return Iterables.any(getExecutionData(), new Predicate() {
+ @Override
+ public boolean apply(File file) {
+ return file.exists();
+ }
+ });
+ }
+ });
+ }
+
+ @Inject
+ protected Instantiator getInstantiator() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Inject
+ protected IsolatedAntBuilder getAntBuilder() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Collection of execution data files to analyze.
+ */
+ @PathSensitive(PathSensitivity.NONE)
+ @InputFiles
+ public FileCollection getExecutionData() {
+ return executionData;
+ }
+
+ public void setExecutionData(FileCollection executionData) {
+ this.executionData = executionData;
+ }
+
+ /**
+ * Source sets that coverage should be reported for.
+ */
+ @PathSensitive(PathSensitivity.RELATIVE)
+ @InputFiles
+ public FileCollection getSourceDirectories() {
+ return sourceDirectories;
+ }
+
+ public void setSourceDirectories(FileCollection sourceDirectories) {
+ this.sourceDirectories = sourceDirectories;
+ }
+
+ /**
+ * Source sets that coverage should be reported for.
+ */
+ @PathSensitive(PathSensitivity.RELATIVE)
+ @InputFiles
+ public FileCollection getClassDirectories() {
+ return classDirectories;
+ }
+
+ public void setClassDirectories(FileCollection classDirectories) {
+ this.classDirectories = classDirectories;
+ }
+
+ /**
+ * Additional class dirs that coverage data should be reported for.
+ */
+ @Optional
+ @PathSensitive(PathSensitivity.RELATIVE)
+ @InputFiles
+ public FileCollection getAdditionalClassDirs() {
+ return additionalClassDirs;
+ }
+
+ public void setAdditionalClassDirs(FileCollection additionalClassDirs) {
+ this.additionalClassDirs = additionalClassDirs;
+ }
+
+ /**
+ * Additional source dirs for the classes coverage data is being reported for.
+ */
+ @Optional
+ @PathSensitive(PathSensitivity.RELATIVE)
+ @InputFiles
+ public FileCollection getAdditionalSourceDirs() {
+ return additionalSourceDirs;
+ }
+
+ public void setAdditionalSourceDirs(FileCollection additionalSourceDirs) {
+ this.additionalSourceDirs = additionalSourceDirs;
+ }
+
+ /**
+ * Adds execution data files to be used during coverage analysis.
+ *
+ * @param files one or more files to add
+ */
+ public void executionData(Object... files) {
+ if (executionData == null) {
+ executionData = getProject().files(files);
+ } else {
+ executionData = executionData.plus(getProject().files(files));
+ }
+ }
+
+ /**
+ * Adds execution data generated by a task to the list of those used during coverage analysis. Only tasks with a {@link JacocoTaskExtension} will be included; all others will be ignored.
+ *
+ * @param tasks one or more tasks to add
+ */
+ public void executionData(Task... tasks) {
+ for (Task task : tasks) {
+ final JacocoTaskExtension extension = task.getExtensions().findByType(JacocoTaskExtension.class);
+ if (extension != null) {
+ executionData(new Callable() {
+ @Override
+ public File call() {
+ return extension.getDestinationFile();
+ }
+ });
+ mustRunAfter(task);
+ }
+ }
+ }
+
+ /**
+ * Adds execution data generated by the given tasks to the list of those used during coverage analysis. Only tasks with a {@link JacocoTaskExtension} will be included; all others will be ignored.
+ *
+ * @param tasks one or more tasks to add
+ */
+ public void executionData(TaskCollection tasks) {
+ tasks.all(new Action() {
+ @Override
+ public void execute(Task task) {
+ executionData(task);
+ }
+ });
+ }
+
+ /**
+ * Gets the class directories that coverage will be reported for. All classes in these directories will be included in the report.
+ *
+ * @return class dirs to report coverage of
+ */
+ @Internal
+ public FileCollection getAllClassDirs() {
+ FileCollection additionalDirs = getAdditionalClassDirs();
+ if (additionalDirs == null) {
+ return classDirectories;
+ }
+ return classDirectories.plus(getAdditionalClassDirs());
+ }
+
+ /**
+ * Gets the source directories for the classes that will be reported on. Source will be obtained from these directories only for the classes included in the report.
+ *
+ * @return source directories for the classes reported on
+ * @see #getAllClassDirs()
+ */
+ @Internal
+ public FileCollection getAllSourceDirs() {
+ FileCollection additionalDirs = getAdditionalSourceDirs();
+ if (additionalDirs == null) {
+ return sourceDirectories;
+ }
+ return sourceDirectories.plus(getAdditionalSourceDirs());
+ }
+
+ /**
+ * Adds a source set to the list to be reported on. The output of this source set will be used as classes to include in the report. The source for this source set will be used for any classes
+ * included in the report.
+ *
+ * @param sourceSets one or more source sets to report on
+ */
+ public void sourceSets(final SourceSet... sourceSets) {
+ getProject().afterEvaluate(new Action() {
+ @Override
+ public void execute(Project project) {
+ for (SourceSet sourceSet : sourceSets) {
+ if (getSourceDirectories() == null) {
+ setSourceDirectories(getProject().files(sourceSet.getAllJava().getSrcDirs()));
+ } else {
+ setSourceDirectories(getSourceDirectories().plus(getProject().files(sourceSet.getAllJava().getSrcDirs())));
+ }
+ if (getClassDirectories() == null) {
+ setClassDirectories(sourceSet.getOutput());
+ } else {
+ setClassDirectories(getClassDirectories().plus(sourceSet.getOutput()));
+ }
+ }
+ }
+ });
+ }
+
+ /**
+ * Adds additional class directories to those that will be included in the report.
+ *
+ * @param dirs one or more directories containing classes to report coverage of
+ */
+ public void additionalClassDirs(File... dirs) {
+ additionalClassDirs(getProject().files(Arrays.asList(dirs)));
+ }
+
+ /**
+ * Adds additional class directories to those that will be included in the report.
+ *
+ * @param dirs a {@code FileCollection} of directories containing classes to report coverage of
+ */
+ public void additionalClassDirs(FileCollection dirs) {
+ if (additionalClassDirs == null) {
+ additionalClassDirs = dirs;
+ } else {
+ additionalClassDirs = additionalClassDirs.plus(dirs);
+ }
+ }
+
+ /**
+ * Adds additional source directories to be used for any classes included in the report.
+ *
+ * @param dirs one or more directories containing source files for the classes included in the report
+ */
+ public void additionalSourceDirs(File... dirs) {
+ additionalSourceDirs(getProject().files(Arrays.asList(dirs)));
+ }
+
+ /**
+ * Adds additional source directories to be used for any classes included in the report.
+ *
+ * @param dirs a {@code FileCollection} of directories containing source files for the classes included in the report
+ */
+ public void additionalSourceDirs(FileCollection dirs) {
+ if (additionalSourceDirs == null) {
+ additionalSourceDirs = dirs;
+ } else {
+ additionalSourceDirs = additionalSourceDirs.plus(dirs);
+ }
+ }
+}
diff --git a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoLimit.java b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoLimit.java
new file mode 100644
index 000000000000..035c33f68f47
--- /dev/null
+++ b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoLimit.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.testing.jacoco.tasks.rules;
+
+import org.gradle.api.Incubating;
+import org.gradle.api.tasks.Input;
+import org.gradle.api.tasks.Optional;
+
+import java.io.Serializable;
+
+/**
+ * Defines a Jacoco rule limit.
+ *
+ * @since 3.4
+ */
+@Incubating
+public interface JacocoLimit extends Serializable {
+
+ /**
+ * The counter that applies to the limit as defined by
+ * org.jacoco.core.analysis.ICoverageNode.CounterEntity.
+ * Valid values are INSTRUCTION, LINE, BRANCH, COMPLEXITY, METHOD and CLASS. Defaults to INSTRUCTION.
+ */
+ @Input
+ String getCounter();
+
+ void setCounter(String counter);
+
+ /**
+ * The value that applies to the limit as defined by
+ * org.jacoco.core.analysis.ICounter.CounterValue.
+ * Valid values are TOTALCOUNT, MISSEDCOUNT, COVEREDCOUNT, MISSEDRATIO and COVEREDRATIO. Defaults to COVEREDRATIO.
+ */
+ @Input
+ String getValue();
+
+ void setValue(String value);
+
+ /**
+ * Gets the minimum expected value for limit. Default to null.
+ */
+ @Input
+ @Optional
+ Double getMinimum();
+
+ void setMinimum(Double minimum);
+
+ /**
+ * Gets the maximum expected value for limit. Default to null.
+ */
+ @Input
+ @Optional
+ Double getMaximum();
+
+ void setMaximum(Double maximum);
+}
diff --git a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoViolationRule.java b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoViolationRule.java
new file mode 100644
index 000000000000..5b6803907155
--- /dev/null
+++ b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoViolationRule.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.testing.jacoco.tasks.rules;
+
+import groovy.lang.Closure;
+import groovy.lang.DelegatesTo;
+import org.gradle.api.Action;
+import org.gradle.api.Incubating;
+import org.gradle.api.tasks.Input;
+import org.gradle.api.tasks.Nested;
+
+import java.io.Serializable;
+import java.util.List;
+
+import static groovy.lang.Closure.DELEGATE_FIRST;
+
+/**
+ * Defines a Jacoco violation rule.
+ *
+ * @since 3.4
+ */
+@Incubating
+public interface JacocoViolationRule extends Serializable {
+
+ void setEnabled(boolean enabled);
+
+ /**
+ * Indicates if the rule should be used when checking generated coverage metrics. Defaults to true.
+ */
+ @Input
+ boolean isEnabled();
+
+ void setElement(String element);
+
+ /**
+ * Gets the element for the rule as defined by
+ * org.jacoco.core.analysis.ICoverageNode.ElementType.
+ * Valid scope values are BUNDLE, PACKAGE, CLASS, SOURCEFILE and METHOD. Defaults to BUNDLE.
+ */
+ @Input
+ String getElement();
+
+ void setIncludes(List includes);
+
+ /**
+ * List of elements that should be included in check. Names can use wildcards (* and ?).
+ * If left empty, all elements will be included. Defaults to [*].
+ */
+ @Input
+ List getIncludes();
+
+ void setExcludes(List excludes);
+
+ /**
+ * List of elements that should be excluded from check. Names can use wildcards (* and ?).
+ * If left empty, no elements will be excluded. Defaults to an empty list.
+ */
+ @Input
+ List getExcludes();
+
+ /**
+ * Gets all limits defined for this rule. Defaults to an empty list.
+ */
+ @Nested
+ List getLimits();
+
+ /**
+ * Adds a limit for this rule. Any number of limits can be added.
+ */
+ JacocoLimit limit(@DelegatesTo(value = JacocoLimit.class, strategy = DELEGATE_FIRST) Closure configureClosure);
+
+ JacocoLimit limit(Action super JacocoLimit> configureAction);
+}
diff --git a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoViolationRulesContainer.java b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoViolationRulesContainer.java
new file mode 100644
index 000000000000..fe60db042e11
--- /dev/null
+++ b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/JacocoViolationRulesContainer.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.testing.jacoco.tasks.rules;
+
+import groovy.lang.Closure;
+import groovy.lang.DelegatesTo;
+import org.gradle.api.Action;
+import org.gradle.api.Incubating;
+import org.gradle.api.tasks.Input;
+
+import java.util.List;
+
+import static groovy.lang.Closure.DELEGATE_FIRST;
+
+/**
+ * The violation rules configuration for the {@link org.gradle.testing.jacoco.tasks.JacocoReport} task.
+ *
+ * @since 3.4
+ */
+@Incubating
+public interface JacocoViolationRulesContainer {
+
+ void setFailOnViolation(boolean ignore);
+
+ /**
+ * Specifies whether build should fail in case of rule violations. Defaults to true.
+ */
+ @Input
+ boolean isFailOnViolation();
+
+ /**
+ * Gets all violation rules. Defaults to an empty list.
+ */
+ @Input
+ List getRules();
+
+ /**
+ * Adds a violation rule. Any number of rules can be added.
+ */
+ JacocoViolationRule rule(@DelegatesTo(value = JacocoViolationRule.class, strategy = DELEGATE_FIRST) Closure configureClosure);
+
+ JacocoViolationRule rule(Action super JacocoViolationRule> configureAction);
+}
diff --git a/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/package-info.java b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/package-info.java
new file mode 100644
index 000000000000..983b97986fff
--- /dev/null
+++ b/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/tasks/rules/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementations for Jacoco code coverage rules.
+ */
+@org.gradle.api.Incubating
+package org.gradle.testing.jacoco.tasks.rules;
\ No newline at end of file
diff --git a/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoLimitImplTest.groovy b/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoLimitImplTest.groovy
new file mode 100644
index 000000000000..5d431c6079c7
--- /dev/null
+++ b/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoLimitImplTest.groovy
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.internal.jacoco.rules
+
+import spock.lang.Specification
+
+class JacocoLimitImplTest extends Specification {
+
+ JacocoLimitImpl limit = new JacocoLimitImpl()
+
+ def "provides expected default field values"() {
+ expect:
+ limit.counter == 'INSTRUCTION'
+ limit.value == 'COVEREDRATIO'
+ !limit.minimum
+ !limit.maximum
+ }
+}
diff --git a/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoViolationRuleImplTest.groovy b/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoViolationRuleImplTest.groovy
new file mode 100644
index 000000000000..d15aa0aace4d
--- /dev/null
+++ b/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoViolationRuleImplTest.groovy
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.internal.jacoco.rules
+
+import org.gradle.api.Action
+import org.gradle.testing.jacoco.tasks.rules.JacocoLimit
+import spock.lang.Specification
+
+class JacocoViolationRuleImplTest extends Specification {
+
+ JacocoViolationRuleImpl rule = new JacocoViolationRuleImpl()
+
+ def "provides expected default field values"() {
+ expect:
+ rule.enabled
+ rule.element == 'BUNDLE'
+ rule.includes == ['*']
+ rule.excludes == []
+ }
+
+ def "can add limits"() {
+ when:
+ def limit = rule.limit {
+ counter = 'CLASS'
+ value = 'TOTALCOUNT'
+ minimum = 0.0
+ maximum = 1.0
+ }
+
+ then:
+ rule.limits.size() == 1
+ rule.limits[0] == limit
+
+ when:
+ limit = rule.limit(new Action() {
+ @Override
+ void execute(JacocoLimit jacocoLimit) {
+ jacocoLimit.with {
+ counter = 'COMPLEXITY'
+ value = 'MISSEDCOUNT'
+ minimum = 0.2
+ maximum = 0.6
+ }
+ }
+ })
+
+ then:
+ rule.limits.size() == 2
+ rule.limits[1] == limit
+ }
+
+ def "returned includes, excludes and limits are unmodifiable"() {
+ when:
+ rule.includes << ['*']
+
+ then:
+ thrown(UnsupportedOperationException)
+
+ when:
+ rule.excludes << ['*']
+
+ then:
+ thrown(UnsupportedOperationException)
+
+ when:
+ rule.limits << new JacocoLimitImpl()
+
+ then:
+ thrown(UnsupportedOperationException)
+ }
+}
diff --git a/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoViolationRulesContainerImplTest.groovy b/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoViolationRulesContainerImplTest.groovy
new file mode 100644
index 000000000000..11933cb83a64
--- /dev/null
+++ b/subprojects/jacoco/src/test/groovy/org/gradle/internal/jacoco/rules/JacocoViolationRulesContainerImplTest.groovy
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.gradle.internal.jacoco.rules
+
+import org.gradle.api.Action
+import org.gradle.testing.jacoco.tasks.rules.JacocoViolationRule
+import spock.lang.Specification
+
+class JacocoViolationRulesContainerImplTest extends Specification {
+
+ JacocoViolationRulesContainerImpl violationRulesContainer = new JacocoViolationRulesContainerImpl()
+
+ def "provides expected default field values"() {
+ expect:
+ !violationRulesContainer.failOnViolation
+ violationRulesContainer.rules.empty
+ }
+
+ def "can add rules"() {
+ when:
+ def rule = violationRulesContainer.rule {
+ enabled = false
+ element = 'CLASS'
+ includes = ['**/*.class']
+ excludes = ['*Special*.class']
+ }
+
+ then:
+ violationRulesContainer.rules.size() == 1
+ violationRulesContainer.rules[0] == rule
+
+ when:
+ rule = violationRulesContainer.rule(new Action() {
+ @Override
+ void execute(JacocoViolationRule jacocoValidationRule) {
+ jacocoValidationRule.with {
+ enabled = true
+ element = 'PACKAGE'
+ includes = ['**/*']
+ excludes = ['**/special/*']
+ }
+ }
+ })
+
+ then:
+ violationRulesContainer.rules.size() == 2
+ violationRulesContainer.rules[1] == rule
+ }
+
+ def "returned rules are unmodifiable"() {
+ when:
+ violationRulesContainer.rules << new JacocoViolationRuleImpl()
+
+ then:
+ thrown(UnsupportedOperationException)
+ }
+}
diff --git a/subprojects/jacoco/src/test/groovy/org/gradle/testing/jacoco/plugins/JacocoPluginSpec.groovy b/subprojects/jacoco/src/test/groovy/org/gradle/testing/jacoco/plugins/JacocoPluginSpec.groovy
index bfbd171f0a46..eb20dc8b5961 100644
--- a/subprojects/jacoco/src/test/groovy/org/gradle/testing/jacoco/plugins/JacocoPluginSpec.groovy
+++ b/subprojects/jacoco/src/test/groovy/org/gradle/testing/jacoco/plugins/JacocoPluginSpec.groovy
@@ -17,6 +17,7 @@ package org.gradle.testing.jacoco.plugins
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.testing.Test
+import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.test.fixtures.AbstractProjectBuilderSpec
import spock.lang.Issue
import spock.lang.Unroll
@@ -92,4 +93,17 @@ class JacocoPluginSpec extends AbstractProjectBuilderSpec {
where:
includeNoLocationClassesValue << [true, false]
}
+
+ def "declares task property values for group and description"() {
+ given:
+ project.apply plugin: 'java'
+
+ expect:
+ def jacocoTestReportTask = project.tasks.getByName('jacocoTestReport')
+ def jacocoTestCoverageVerificationTask = project.tasks.getByName('jacocoTestCoverageVerification')
+ jacocoTestReportTask.group == LifecycleBasePlugin.VERIFICATION_GROUP
+ jacocoTestCoverageVerificationTask.group == LifecycleBasePlugin.VERIFICATION_GROUP
+ jacocoTestReportTask.description == 'Generates code coverage report for the test task.'
+ jacocoTestCoverageVerificationTask.description == 'Verifies code coverage metrics based on specified rules for the test task.'
+ }
}