From: Evgeny Mandrikov Date: Thu, 28 Apr 2011 13:14:29 +0000 (+0400) Subject: Fix NPE X-Git-Tag: 2.8~91 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c22c2f412714a7d9ff8edee8a07d5589fc250f3b;p=sonarqube.git Fix NPE --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java index 4cca0ad934b..4c3bf2e0a67 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java @@ -194,7 +194,7 @@ public class Violation { * @since 2.4 */ public Violation setCost(Double d) { - if (d >= 0) { + if (d == null || d >= 0) { this.cost = d; return this; } else { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java index 2ba51cfbf7c..e763b638c63 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java @@ -52,6 +52,15 @@ public class ViolationTest { assertThat(violation.getLineId(), is(1)); } + @Test + public void testCostContract() { + violation.setCost(null); + assertThat(violation.getCost(), nullValue()); + + violation.setCost(1.0); + assertThat(violation.getCost(), is(1.0)); + } + @Test(expected = IllegalArgumentException.class) public void testCostContract_NaN() { violation.setCost(Double.NaN);