From c22c2f412714a7d9ff8edee8a07d5589fc250f3b Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Thu, 28 Apr 2011 17:14:29 +0400 Subject: [PATCH] Fix NPE --- .../src/main/java/org/sonar/api/rules/Violation.java | 2 +- .../src/test/java/org/sonar/api/rules/ViolationTest.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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); -- 2.39.5