]> source.dussan.org Git - sonarqube.git/commitdiff
Ensure that cost to fix violation cannot be set to negative or NaN value
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 27 Apr 2011 19:59:31 +0000 (23:59 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 27 Apr 2011 20:08:00 +0000 (00:08 +0400)
sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java

index 46f9dcaf42856639e84cd2a4c211dc575d34e723..4cca0ad934bbe3295062357c74ccfc79e82d6c6a 100644 (file)
@@ -194,8 +194,12 @@ public class Violation {
    * @since 2.4
    */
   public Violation setCost(Double d) {
-    this.cost = d;
-    return this;
+    if (d >= 0) {
+      this.cost = d;
+      return this;
+    } else {
+      throw new IllegalArgumentException("Cost to fix violation can't be negative or NaN");
+    }
   }
 
   /**
@@ -228,7 +232,7 @@ public class Violation {
   }
 
   /**
-   * Tells wether this violation is ON or OFF.
+   * Tells whether this violation is ON or OFF.
    * 
    * @since 2.8
    * @return true if the violation has been switched off
index a77bcd44cc9bd6794b02716588b54a9d80f116ae..2ba51cfbf7cc656f5fffb65e7256910780593ac6 100644 (file)
@@ -23,16 +23,22 @@ import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
 
+import org.junit.Before;
 import org.junit.Test;
 
 public class ViolationTest {
+  private Violation violation;
+
+  @Before
+  public void setUp() {
+    violation = Violation.create((Rule) null, null);
+  }
+
   /**
    * See http://jira.codehaus.org/browse/SONAR-2386
    */
   @Test
   public void testLineIdContract() {
-    Violation violation = Violation.create((Rule) null, null);
-
     violation.setLineId(null);
     assertThat(violation.hasLineId(), is(false));
     assertThat(violation.getLineId(), nullValue());
@@ -45,4 +51,14 @@ public class ViolationTest {
     assertThat(violation.hasLineId(), is(true));
     assertThat(violation.getLineId(), is(1));
   }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testCostContract_NaN() {
+    violation.setCost(Double.NaN);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testCostContract_Negative() {
+    violation.setCost(-1.0);
+  }
 }