Skip to content

Commit

Permalink
JENKINS-43866 Don't round up 99.x% coverage to 100% (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
cizezsy authored and jxpearce-godaddy committed Mar 3, 2018
1 parent 0db4272 commit c612ade
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/main/java/hudson/plugins/cobertura/Ratio.java
@@ -1,6 +1,7 @@
package hudson.plugins.cobertura;

import java.io.Serializable;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

Expand Down Expand Up @@ -35,11 +36,19 @@ private String print(float f) {

/**
* Gets the percentage in integer.
* If float percentage is less than 100 and larger than 95.5, then return rounded down value,
* else return rounded off value
*
* @return percentage
*/
public int getPercentage() {
return Math.round(getPercentageFloat());
float floatPercentage = getPercentageFloat();
int intPercentage = Math.round(floatPercentage);
if (intPercentage == 100 && (int) floatPercentage < 100) {
return (int) floatPercentage;
} else {
return intPercentage;
}
}

/**
Expand All @@ -54,14 +63,28 @@ public float getPercentageFloat() {
}

static NumberFormat dataFormat = new DecimalFormat("000.00");
static NumberFormat roundDownDataFormat;

static {
roundDownDataFormat = new DecimalFormat("000.000");
roundDownDataFormat.setRoundingMode(RoundingMode.DOWN);
}


/**
* Gets the percentage as a formatted string used for sorting the html table
* Gets the percentage as a formatted string used for sorting the html table.
* If float percentage is less than 100 and larger than 99.995, then return rounded down value,
* else return rounded off value.
*
* @return percentage
*/
public String getPercentageString() {
return dataFormat.format(getPercentageFloat());
float floatPercentage = getPercentageFloat();
if (Float.compare(floatPercentage, 99.995f) >= 0) {
return roundDownDataFormat.format(floatPercentage);
} else {
return dataFormat.format(floatPercentage);
}
}

/**
Expand Down Expand Up @@ -98,12 +121,9 @@ public int hashCode() {

/**
* Creates a new instance of {@link Ratio}.
*/
/**
*
* @param x numerator
* @param y denominator
*
* @return the ratio
*/
public static Ratio create(float x, float y) {
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/hudson/plugins/cobertura/RatioTest.java
Expand Up @@ -19,6 +19,20 @@ final void assertRatio(Ratio r, float numerator, float denominator) {
* @throws Exception
*/
public void testParseValue() throws Exception {
assertRatio(Ratio.create(1,2), 1.0f, 2.0f);
assertRatio(Ratio.create(1, 2), 1.0f, 2.0f);
}

public void testGetPercentage() {
assertEquals(98, Ratio.create(246, 250).getPercentage());
assertEquals(99, Ratio.create(247, 250).getPercentage());
assertEquals(99, Ratio.create(248, 250).getPercentage());
assertEquals(99, Ratio.create(249, 250).getPercentage());
assertEquals(100, Ratio.create(250, 250).getPercentage());
}

public void testGetPercentageFloat() {
assertEquals("099.99", Ratio.create(24998, 25000).getPercentageString());
assertEquals("099.996", Ratio.create(24999, 25000).getPercentageString());
assertEquals("100.000", Ratio.create(25000, 25000).getPercentageString());
}
}

0 comments on commit c612ade

Please sign in to comment.