aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc R. Hoffmann <hoffmann@mountainminds.com>2017-05-18 21:31:41 +0200
committerGitHub <noreply@github.com>2017-05-18 21:31:41 +0200
commit938b0b4e0226f64ce7c5fac66a94fdb5d18f3f67 (patch)
treeba4ec5d3d844fa97ccdb31a31ca3574ad6f124e6
parent60b82f23c20c3ce18dad74ea6c3d37233aae4fd7 (diff)
parent129201d6413ce5a2fcde03e7af58eb989ba8625a (diff)
downloadjacoco-938b0b4e0226f64ce7c5fac66a94fdb5d18f3f67.tar.gz
Merge pull request #536 from mattnelson/percentages
Support limit ratios as percents (#534)
-rw-r--r--jacoco-maven-plugin/src/org/jacoco/maven/CheckMojo.java5
-rw-r--r--org.jacoco.doc/docroot/doc/ant.html8
-rw-r--r--org.jacoco.doc/docroot/doc/changes.html3
-rw-r--r--org.jacoco.report.test/src/org/jacoco/report/check/LimitTest.java30
-rw-r--r--org.jacoco.report/src/org/jacoco/report/check/Limit.java30
5 files changed, 63 insertions, 13 deletions
diff --git a/jacoco-maven-plugin/src/org/jacoco/maven/CheckMojo.java b/jacoco-maven-plugin/src/org/jacoco/maven/CheckMojo.java
index 43cf35f1..30ee795b 100644
--- a/jacoco-maven-plugin/src/org/jacoco/maven/CheckMojo.java
+++ b/jacoco-maven-plugin/src/org/jacoco/maven/CheckMojo.java
@@ -49,7 +49,8 @@ public class CheckMojo extends AbstractJacocoMojo implements IViolationsOutput {
* value (TOTALCOUNT, COVEREDCOUNT, MISSEDCOUNT, COVEREDRATIO, MISSEDRATIO).
* If a limit refers to a ratio the range is from 0.0 to 1.0 where the
* number of decimal places will also determine the precision in error
- * messages.
+ * messages. A limit ratio may optionally be declared as a percentage
+ * where 0.80 and 80% represent the same value, the value must end with %.
* </p>
*
* <p>
@@ -111,7 +112,7 @@ public class CheckMojo extends AbstractJacocoMojo implements IViolationsOutput {
* <limit>
* <counter>LINE</counter>
* <value>COVEREDRATIO</value>
- * <minimum>0.50</minimum>
+ * <minimum>50%</minimum>
* </limit>
* </limits>
* </rule>
diff --git a/org.jacoco.doc/docroot/doc/ant.html b/org.jacoco.doc/docroot/doc/ant.html
index 2e463d68..af070680 100644
--- a/org.jacoco.doc/docroot/doc/ant.html
+++ b/org.jacoco.doc/docroot/doc/ant.html
@@ -745,7 +745,7 @@
<pre class="source lang-xml linenums">
&lt;check&gt;
&lt;rule element="PACKAGE"&gt;
- &lt;limit counter="LINE" value="COVEREDRATIO" minimum="0.80"/&gt;
+ &lt;limit counter="LINE" value="COVEREDRATIO" minimum="80%"/&gt;
&lt;limit counter="CLASS" value="MISSEDCOUNT" maximum="0"/&gt;
&lt;/rule&gt;
&lt;/check&gt;
@@ -858,12 +858,14 @@
<td><code>minimum</code></td>
<td>Expected minimum value. If the minimum refers to a ratio the range is
from 0.0 to 1.0 where the number of decimal places will also determine
- the precision in error messages.</td>
+ the precision in error messages. A limit ratio may optionally be
+ declared as a percentage where 0.80 and 80% represent the same value,
+ the value must end with %.</td>
<td><i>none</i></td>
</tr>
<tr>
<td><code>maximum</code></td>
- <td>Expected maximum value.</td>
+ <td>Expected maximum value, see <code>minimum</code> for details.</td>
<td><i>none</i></td>
</tr>
</tbody>
diff --git a/org.jacoco.doc/docroot/doc/changes.html b/org.jacoco.doc/docroot/doc/changes.html
index 8d2e4f8c..e8a940c6 100644
--- a/org.jacoco.doc/docroot/doc/changes.html
+++ b/org.jacoco.doc/docroot/doc/changes.html
@@ -22,6 +22,9 @@
<h3>New Features</h3>
<ul>
+ <li>Limit configuration of Maven check goal and Ant check element now also
+ supports percentage values
+ (GitHub <a href="https://github.com/jacoco/jacoco/issues/534">#534</a>).</li>
<li>Exclude from a report enum methods <code>valueOf</code> and <code>values</code>
that are created by compiler
(GitHub <a href="https://github.com/jacoco/jacoco/issues/491">#491</a>).</li>
diff --git a/org.jacoco.report.test/src/org/jacoco/report/check/LimitTest.java b/org.jacoco.report.test/src/org/jacoco/report/check/LimitTest.java
index 7b9405c7..a7add963 100644
--- a/org.jacoco.report.test/src/org/jacoco/report/check/LimitTest.java
+++ b/org.jacoco.report.test/src/org/jacoco/report/check/LimitTest.java
@@ -262,6 +262,21 @@ public class LimitTest {
}
@Test
+ public void testMinPercent() {
+ limit.setMinimum("1.55%");
+ assertEquals("0.0155", limit.getMinimum());
+
+ limit.setMinimum("1.5%");
+ assertEquals("0.015", limit.getMinimum());
+
+ limit.setMinimum("1.00%");
+ assertEquals("0.0100", limit.getMinimum());
+
+ limit.setMinimum("1%");
+ assertEquals("0.01", limit.getMinimum());
+ }
+
+ @Test
public void testMax0() {
limit.setMaximum("0");
limit.setMaximum((String) null);
@@ -318,6 +333,21 @@ public class LimitTest {
}));
}
+ @Test
+ public void testMaxPercent() {
+ limit.setMaximum("1.55%");
+ assertEquals("0.0155", limit.getMaximum());
+
+ limit.setMaximum("1.5%");
+ assertEquals("0.015", limit.getMaximum());
+
+ limit.setMaximum("1.00%");
+ assertEquals("0.0100", limit.getMaximum());
+
+ limit.setMaximum("1%");
+ assertEquals("0.01", limit.getMaximum());
+ }
+
private static class TestNode extends CoverageNodeImpl {
public TestNode() {
diff --git a/org.jacoco.report/src/org/jacoco/report/check/Limit.java b/org.jacoco.report/src/org/jacoco/report/check/Limit.java
index fb5a7222..2b25e256 100644
--- a/org.jacoco.report/src/org/jacoco/report/check/Limit.java
+++ b/org.jacoco.report/src/org/jacoco/report/check/Limit.java
@@ -115,16 +115,16 @@ public class Limit {
}
/**
- * Sets allowed minimum value as decimal string representation. The given
- * precision is also considered in error messages. Coverage ratios are given
- * in the range from 0.0 to 1.0.
+ * Sets allowed maximum value as decimal string or percent representation.
+ * The given precision is also considered in error messages. Coverage ratios
+ * are given in the range from 0.0 to 1.0.
*
* @param minimum
* allowed minimum or <code>null</code>, if no minimum should be
* checked
*/
public void setMinimum(final String minimum) {
- this.minimum = minimum == null ? null : new BigDecimal(minimum);
+ this.minimum = parseValue(minimum);
}
/**
@@ -136,16 +136,30 @@ public class Limit {
}
/**
- * Sets allowed maximum value as decimal string representation. The given
- * precision is also considered in error messages. Coverage ratios are given
- * in the range from 0.0 to 1.0.
+ * Sets allowed maximum value as decimal string or percent representation.
+ * The given precision is also considered in error messages. Coverage ratios
+ * are given in the range from 0.0 to 1.0.
*
* @param maximum
* allowed maximum or <code>null</code>, if no maximum should be
* checked
*/
public void setMaximum(final String maximum) {
- this.maximum = maximum == null ? null : new BigDecimal(maximum);
+ this.maximum = parseValue(maximum);
+ }
+
+ private static BigDecimal parseValue(final String value) {
+ if (value == null) {
+ return null;
+ }
+
+ final String trimmedValue = value.trim();
+ if (trimmedValue.endsWith("%")) {
+ final String percent = trimmedValue.substring(0, trimmedValue.length() - 1);
+ return new BigDecimal(percent).movePointLeft(2);
+ }
+
+ return new BigDecimal(trimmedValue);
}
String check(final ICoverageNode node) {