]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2386 Define contract for lineId in Violation
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 27 Apr 2011 16:29:11 +0000 (20:29 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 27 Apr 2011 18:10:58 +0000 (22:10 +0400)
Value can be null or greater than zero, so setter must log warning if
not null and less than 1. It will throw an exception in future releases.

sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/services/Violation.java
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java
sonar-ws-client/src/test/resources/violations/violation-with-incorrect-line.json [new file with mode: 0644]

index 3e4aa063a8b15a0332cdbcac39aaf848495018c4..46f9dcaf42856639e84cd2a4c211dc575d34e723 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.commons.lang.builder.EqualsBuilder;
 import org.apache.commons.lang.builder.HashCodeBuilder;
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.Logs;
 
 import java.util.Date;
 
@@ -108,22 +109,38 @@ public class Violation {
   }
 
   /**
-   * @see #setLineId(Integer)
+   * @return line number (numeration starts from 1), or <code>null</code> if violation doesn't belong to concrete line
+   * @see #hasLineId()
    */
   public Integer getLineId() {
     return lineId;
   }
 
   /**
-   * Sets the violation line. Note that numbering starts from 1.
+   * Sets the violation line.
    * 
+   * @param lineId line number (numeration starts from 1), or <code>null</code> if violation doesn't belong to concrete line
    * @return the current object
    */
   public Violation setLineId(Integer lineId) {
-    this.lineId = lineId;
+    if (lineId != null && lineId < 1) {
+      // TODO this normalization was added in 2.8, throw exception in future versions - see http://jira.codehaus.org/browse/SONAR-2386
+      Logs.INFO.warn("line must not be less than 1 - in future versions this will cause IllegalArgumentException");
+      this.lineId = null;
+    } else {
+      this.lineId = lineId;
+    }
     return this;
   }
 
+  /**
+   * @return <code>true<code> if violation belongs to concrete line
+   * @since 2.8
+   */
+  public boolean hasLineId() {
+    return lineId != null;
+  }
+
   /**
    * @since 2.5
    */
@@ -222,7 +239,7 @@ public class Violation {
 
   @Override
   public boolean equals(Object obj) {
-    if ( !(obj instanceof Violation)) {
+    if (!(obj instanceof Violation)) {
       return false;
     }
     if (this == obj) {
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
new file mode 100644 (file)
index 0000000..a77bcd4
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.api.rules;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class ViolationTest {
+  /**
+   * 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());
+
+    violation.setLineId(0);
+    assertThat(violation.hasLineId(), is(false));
+    assertThat(violation.getLineId(), nullValue());
+
+    violation.setLineId(1);
+    assertThat(violation.hasLineId(), is(true));
+    assertThat(violation.getLineId(), is(1));
+  }
+}
index 0d5b816425fb3e35edcc6874ec973bed6cdd6ded..4e170124c6b3c98d58ac872f3275aeffd0675096 100644 (file)
@@ -74,12 +74,32 @@ public class Violation extends Model {
     this.severity = priority;
   }
 
+  /**
+   * @return line number (numeration starts from 1), or <code>null</code> if violation doesn't belong to concrete line
+   * @see #hasLine()
+   */
   public Integer getLine() {
     return line;
   }
 
   public void setLine(Integer line) {
-    this.line = line;
+    if (line != null && line < 1) {
+      /*
+       * This shouldn't happen, however line would be normalized to null if web service returns incorrect value (less than 1)
+       * in compliance with a contract for getLine method. Normalization added in 2.8 - see http://jira.codehaus.org/browse/SONAR-2386
+       */
+      this.line = null;
+    } else {
+      this.line = line;
+    }
+  }
+
+  /**
+   * @return <code>true<code> if violation belongs to concrete line
+   * @since 2.8
+   */
+  public boolean hasLine() {
+    return line != null;
   }
 
   public String getResourceKey() {
index dc9a8c4cc2d5c082cdb6b27c278dc2a5c9e6f60a..4652c2cd9d96c326e708ac598909d31a9a0fab06 100644 (file)
@@ -42,6 +42,7 @@ public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
 
     violation = violations.get(0);
     assertThat(violation.getMessage(), is("throw java.lang.Exception"));
+    assertThat(violation.hasLine(), is(true));
     assertThat(violation.getLine(), is(97));
     assertThat(violation.getCreatedAt(), notNullValue());
     assertThat(violation.getSeverity(), is("MAJOR"));
@@ -60,6 +61,7 @@ public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
   public void testViolationWithoutLineNumber() {
     Violation violation = new ViolationUnmarshaller().toModel(loadFile("/violations/violation-without-optional-fields.json"));
     assertThat(violation.getMessage(), not(nullValue()));
+    assertThat(violation.hasLine(), is(false));
     assertThat(violation.getLine(), nullValue());
     assertThat(violation.getCreatedAt(), nullValue());
   }
@@ -70,4 +72,14 @@ public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
     assertThat(violation.isFalsePositive(), is(true));
     assertThat(violation.getReviewId(), is(123L));
   }
+
+  /**
+   * See http://jira.codehaus.org/browse/SONAR-2386
+   */
+  @Test
+  public void testIncorrectLine() {
+    Violation violation = new ViolationUnmarshaller().toModel(loadFile("/violations/violation-with-incorrect-line.json"));
+    assertThat(violation.hasLine(), is(false));
+    assertThat(violation.getLine(), nullValue());
+  }
 }
diff --git a/sonar-ws-client/src/test/resources/violations/violation-with-incorrect-line.json b/sonar-ws-client/src/test/resources/violations/violation-with-incorrect-line.json
new file mode 100644 (file)
index 0000000..ef2ed50
--- /dev/null
@@ -0,0 +1,19 @@
+[
+       {
+               "message":"1 branches need to be covered by unit tests to reach the minimum threshold of 65.0% branch coverage.",
+               "line":0,
+               "priority":"MAJOR",
+               "createdAt":"2011-03-07T16:21:39+0000",
+               "rule":{
+                       "key":"sqale-java:BranchCoverageCheck",
+                       "name":"Insufficient branch coverage by unit tests"
+               },
+               "resource":{
+                       "key":"org.codehaus.sonar.plugins:sonar-pmd-plugin:org.sonar.plugins.pmd.PmdRuleRepository",
+                       "name":"PmdRuleRepository",
+                       "scope":"FIL",
+                       "qualifier":"CLA",
+                       "language":"java"
+               }
+       }
+]