Skip to content
Browse files

re-implemented decimal support so that plugin maintains backwards

compatibility. Also added improvement so that percentages cannot be >
100% or < 0%.

Fixed bug JENKINS-14658 and made improvement JENKINS-14659.

Signed-off-by: Po-Dar Wang <po-dar.wang@orbitz.com>
  • Loading branch information...
1 parent 0ac6702 commit 9f3026f2513d38ee89945e96b66d1e6dbe26ac9c Po-Dar Wang committed Aug 3, 2012
View
78 src/main/java/hudson/plugins/cobertura/CoberturaPublisher.java
@@ -85,13 +85,19 @@ public CoberturaPublisher(String coberturaReportFile, boolean onlyStable, boolea
*/
public List<CoberturaPublisherTarget> getTargets() {
Map<CoverageMetric, CoberturaPublisherTarget> targets = new TreeMap<CoverageMetric, CoberturaPublisherTarget>();
+ float checker;
for (CoverageMetric metric : healthyTarget.getTargets()) {
CoberturaPublisherTarget target = targets.get(metric);
if (target == null) {
target = new CoberturaPublisherTarget();
target.setMetric(metric);
}
- target.setHealthy(healthyTarget.getTarget(metric));
+ checker = (float)healthyTarget.getTarget(metric)/100000f;
+ if(checker <= 0.001f)
+ {
+ checker = (float)(Math.round(checker*100000f));
+ }
+ target.setHealthy(checker);
targets.put(metric, target);
}
for (CoverageMetric metric : unhealthyTarget.getTargets()) {
@@ -100,7 +106,12 @@ public CoberturaPublisher(String coberturaReportFile, boolean onlyStable, boolea
target = new CoberturaPublisherTarget();
target.setMetric(metric);
}
- target.setUnhealthy(unhealthyTarget.getTarget(metric));
+ checker = (float)unhealthyTarget.getTarget(metric)/100000f;
+ if(checker <= 0.001f)
+ {
+ checker = (float)(Math.round(checker*100000f));
+ }
+ target.setUnhealthy(checker);
targets.put(metric, target);
}
for (CoverageMetric metric : failingTarget.getTargets()) {
@@ -109,7 +120,12 @@ public CoberturaPublisher(String coberturaReportFile, boolean onlyStable, boolea
target = new CoberturaPublisherTarget();
target.setMetric(metric);
}
- target.setUnstable(failingTarget.getTarget(metric));
+ checker = (float)failingTarget.getTarget(metric)/100000f;
+ if(checker <= 0.001f)
+ {
+ checker = (float)(Math.round(checker*100000f));
+ }
+ target.setUnstable(checker);
targets.put(metric, target);
}
List<CoberturaPublisherTarget> result = new ArrayList<CoberturaPublisherTarget>(targets.values());
@@ -125,15 +141,22 @@ private void setTargets(List<CoberturaPublisherTarget> targets) {
healthyTarget.clear();
unhealthyTarget.clear();
failingTarget.clear();
+ float rounded;
for (CoberturaPublisherTarget target : targets) {
if (target.getHealthy() != null) {
- healthyTarget.setTarget(target.getMetric(), target.getHealthy());
+ rounded = (Math.round((float)100f*target.getHealthy()));
+ rounded = roundDecimalFloat(rounded);
+ healthyTarget.setTarget(target.getMetric(), (int)((float)100000f*rounded));
}
if (target.getUnhealthy() != null) {
- unhealthyTarget.setTarget(target.getMetric(), target.getUnhealthy());
+ rounded = (Math.round((float)100f*target.getUnhealthy()));
+ rounded = roundDecimalFloat(rounded);
+ unhealthyTarget.setTarget(target.getMetric(), (int)((float)100000f*rounded));
}
if (target.getUnstable() != null) {
- failingTarget.setTarget(target.getMetric(), target.getUnstable());
+ rounded = (Math.round((float)100f*target.getUnstable()));
+ rounded = roundDecimalFloat(rounded);
+ failingTarget.setTarget(target.getMetric(), (int)((float)100000f*rounded));
}
}
}
@@ -361,10 +384,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
Set<CoverageMetric> failingMetrics = failingTarget.getFailingMetrics(result);
if (!failingMetrics.isEmpty()) {
listener.getLogger().println("Code coverage enforcement failed for the following metrics:");
- int oldStabilityPercent;
+ float oldStabilityPercent;
+ float setStabilityPercent;
for (CoverageMetric metric : failingMetrics) {
- oldStabilityPercent = failingTarget.getSetPercent(result, metric);
- listener.getLogger().println(" " + metric.getName() + "'s stability is " + oldStabilityPercent);
+ oldStabilityPercent = failingTarget.getObservedPercent(result, metric);
+ setStabilityPercent = failingTarget.getSetPercent(result, metric);
+ listener.getLogger().println(" " + metric.getName() + "'s stability is " + roundDecimalFloat(oldStabilityPercent*100f) + " and set mininum stability is " + roundDecimalFloat(setStabilityPercent*100f) + ".");
}
if(!getFailUnstable())
{
@@ -382,10 +407,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
Set<CoverageMetric> unhealthyMetrics = unhealthyTarget.getFailingMetrics(result);
if (!unhealthyMetrics.isEmpty()) {
listener.getLogger().println("Unhealthy for the following metrics:");
- int oldHealthyPercent;
+ float oldHealthyPercent;
+ float setHealthyPercent;
for (CoverageMetric metric : unhealthyMetrics) {
- oldHealthyPercent = unhealthyTarget.getSetPercent(result, metric);
- listener.getLogger().println(" " + metric.getName() + "'s health is " + oldHealthyPercent);
+ oldHealthyPercent = unhealthyTarget.getObservedPercent(result, metric);
+ setHealthyPercent = unhealthyTarget.getSetPercent(result, metric);
+ listener.getLogger().println(" " + metric.getName() + "'s health is " + roundDecimalFloat(oldHealthyPercent*100f) + " and set minimum health is " + roundDecimalFloat(setHealthyPercent*100f) + ".");
}
listener.getLogger().println("Failing build because it is unhealthy.");
build.setResult(Result.FAILURE);
@@ -418,32 +445,35 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
private void setNewPercentages(CoverageResult result, boolean select, BuildListener listener)
{
Set<CoverageMetric> healthyMetrics = healthyTarget.getAllMetrics(result);
- int newPercent;
- int oldPercent;
+ float newPercent;
+ float oldPercent;
if(!healthyMetrics.isEmpty())
{
for (CoverageMetric metric : healthyMetrics)
{
newPercent = healthyTarget.getObservedPercent(result, metric);
+ newPercent = (float)(Math.round(newPercent*100f));
if(select)
{
oldPercent = unhealthyTarget.getSetPercent(result, metric);
+ oldPercent = (float)(Math.round(oldPercent*100f));
}
else
{
oldPercent = failingTarget.getSetPercent(result, metric);
+ oldPercent = (float)(Math.round(oldPercent*100f));
}
if(newPercent > oldPercent)
{
if(select)
{
- unhealthyTarget.setTarget(metric, newPercent);
- listener.getLogger().println(" " + metric.getName() + "'s new health minimum is: " + newPercent);
+ unhealthyTarget.setTarget(metric, (int)(newPercent*1000f));
+ listener.getLogger().println(" " + metric.getName() + "'s new health minimum is: " + roundDecimalFloat(newPercent));
}
else
{
- failingTarget.setTarget(metric, newPercent);
- listener.getLogger().println(" " + metric.getName() + "'s new stability minimum is: " + newPercent);
+ failingTarget.setTarget(metric, (int)(newPercent*1000f));
+ listener.getLogger().println(" " + metric.getName() + "'s new stability minimum is: " + roundDecimalFloat(newPercent));
}
}
}
@@ -532,9 +562,9 @@ public String getDisplayName() {
*/
public List<CoberturaPublisherTarget> getDefaultTargets() {
List<CoberturaPublisherTarget> result = new ArrayList<CoberturaPublisherTarget>();
- result.add(new CoberturaPublisherTarget(CoverageMetric.METHOD, 80, null, null));
- result.add(new CoberturaPublisherTarget(CoverageMetric.LINE, 80, null, null));
- result.add(new CoberturaPublisherTarget(CoverageMetric.CONDITIONAL, 70, null, null));
+ result.add(new CoberturaPublisherTarget(CoverageMetric.METHOD, 80f, null, null));
+ result.add(new CoberturaPublisherTarget(CoverageMetric.LINE, 80f, null, null));
+ result.add(new CoberturaPublisherTarget(CoverageMetric.CONDITIONAL, 70f, null, null));
return result;
}
@@ -600,4 +630,10 @@ private boolean didCoberturaRun(MavenBuild mavenBuild) {
}
return false;
}
+
+ public float roundDecimalFloat(Float input) {
+ float rounded = (float)Math.round(input);
+ rounded = rounded/100f;
+ return rounded;
+ }
}
View
98 src/main/java/hudson/plugins/cobertura/CoberturaPublisherTarget.java
@@ -11,9 +11,9 @@
*/
public final class CoberturaPublisherTarget {
private CoverageMetric metric;
- private Integer healthy;
- private Integer unhealthy;
- private Integer unstable;
+ private Float healthy;
+ private Float unhealthy;
+ private Float unstable;
public static final Converter CONVERTER = new TargetConverter();
@@ -30,7 +30,7 @@ public CoberturaPublisherTarget() {
* @param unstable
* @stapler-constructor
*/
- public CoberturaPublisherTarget(CoverageMetric metric, Integer healthy, Integer unhealthy, Integer unstable) {
+ public CoberturaPublisherTarget(CoverageMetric metric, Float healthy, Float unhealthy, Float unstable) {
this.metric = metric;
this.healthy = healthy;
this.unhealthy = unhealthy;
@@ -60,53 +60,119 @@ public void setMetric(CoverageMetric metric) {
*
* @return Value for property 'healthy'.
*/
- public Integer getHealthy() {
- return healthy == null ? 80 : healthy;
+ public Float getHealthy() {
+ if(healthy == null)
+ {
+ return 80f;
+ }
+ else
+ {
+ return healthy;
+ }
}
/**
* Setter for property 'healthy'.
*
* @param healthy Value to set for property 'healthy'.
*/
- public void setHealthy(Integer healthy) {
- this.healthy = healthy;
+ public void setHealthy(Float healthy) {
+ if(healthy == null)
+ {
+ this.healthy = null;
+ }
+ else if (healthy < 0f)
+ {
+ this.healthy = 0f;
+ }
+ else if (healthy > 100f)
+ {
+ this.healthy = 100f;
+ }
+ else
+ {
+ this.healthy = (float)(Math.round(healthy*100f)/100f);
+ }
}
/**
* Getter for property 'unhealthy'.
*
* @return Value for property 'unhealthy'.
*/
- public Integer getUnhealthy() {
- return unhealthy == null ? 0 : unhealthy;
+ public Float getUnhealthy() {
+ if(unhealthy == null)
+ {
+ return 0f;
+ }
+ else
+ {
+ return (float)(Math.round(unhealthy*100f)/100f);
+ }
}
/**
* Setter for property 'unhealthy'.
*
* @param unhealthy Value to set for property 'unhealthy'.
*/
- public void setUnhealthy(Integer unhealthy) {
- this.unhealthy = unhealthy;
+ public void setUnhealthy(Float unhealthy) {
+ if(unhealthy == null)
+ {
+ this.unhealthy = null;
+ }
+ else if (unhealthy < 0f)
+ {
+ this.unhealthy = 0f;
+ }
+ else if (unhealthy > 100f)
+ {
+ this.unhealthy = 100f;
+ }
+ else
+ {
+ this.unhealthy = (float)(Math.round(unhealthy*100f)/100f);
+ }
}
/**
* Getter for property 'unstable'.
*
* @return Value for property 'unstable'.
*/
- public Integer getUnstable() {
- return unstable == null ? 0 : unstable;
+ public Float getUnstable() {
+ if(unstable == null)
+ {
+ return 0f;
+ }
+ else
+ {
+ return (float)(Math.round(unstable*100f)/100f);
+ }
}
/**
* Setter for property 'unstable'.
*
* @param unstable Value to set for property 'unstable'.
*/
- public void setUnstable(Integer unstable) {
- this.unstable = unstable;
+ public void setUnstable(Float unstable) {
+ if(unstable == null)
+ {
+ this.unstable = null;
+ }
+ else if (unstable < 0f)
+ {
+ this.unstable = 0f;
+ }
+ else if (unstable > 100f)
+ {
+ this.unstable = 100f;
+ }
+ else
+ {
+ this.unstable = (float)(Math.round(unstable*100f)/100f);
+ }
}
private static class TargetConverter implements Converter {
View
12 src/main/java/hudson/plugins/cobertura/targets/CoverageTarget.java
@@ -62,7 +62,7 @@ public boolean isEmpty() {
Set<CoverageMetric> result = EnumSet.noneOf(CoverageMetric.class);
for (Map.Entry<CoverageMetric, Integer> target : this.targets.entrySet()) {
Ratio observed = coverage.getCoverage(target.getKey());
- if (observed != null && observed.getPercentage() < target.getValue()) {
+ if (observed != null && observed.getPercentageFloat() < (float)(target.getValue()/100000f)) {
result.add(target.getKey());
}
}
@@ -82,22 +82,22 @@ public boolean isEmpty() {
return result;
}
- public int getObservedPercent(CoverageResult coverage, CoverageMetric key)
+ public float getObservedPercent(CoverageResult coverage, CoverageMetric key)
{
for (Map.Entry<CoverageMetric, Integer> target : this.targets.entrySet()) {
Ratio observed = coverage.getCoverage(target.getKey());
if (target.getKey() == key) {
- return observed.getPercentage();
+ return observed.getPercentageFloat();
}
}
return 0;
}
- public int getSetPercent(CoverageResult coverage, CoverageMetric key)
+ public float getSetPercent(CoverageResult coverage, CoverageMetric key)
{
for (Map.Entry<CoverageMetric, Integer> target : this.targets.entrySet()) {
if (target.getKey() == key) {
- return target.getValue();
+ return (float)(target.getValue()/100000f);
}
}
return 0;
@@ -113,7 +113,7 @@ public int getSetPercent(CoverageResult coverage, CoverageMetric key)
for (Map.Entry<CoverageMetric, Integer> target : this.targets.entrySet()) {
Ratio observed = results.get(target.getKey());
if (observed != null) {
- j = CoverageTarget.calcRangeScore(target.getValue(), min.targets.get(target.getKey()), observed.getPercentage());
+ j = CoverageTarget.calcRangeScore(target.getValue()/100000, min.targets.get(target.getKey()), observed.getPercentage());
result.put(target.getKey(), Integer.valueOf(j));
}
}

0 comments on commit 9f3026f

Please sign in to comment.
Something went wrong with that request. Please try again.