* @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");
+ }
}
/**
}
/**
- * 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
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());
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);
+ }
}