From 25f963bfb953346ee85337009b1af7d665494aa0 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 27 Apr 2011 20:29:11 +0400 Subject: SONAR-2386 Define contract for lineId in Violation 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. --- .../org/sonar/wsclient/services/Violation.java | 22 +++++++++++++++++++++- .../unmarshallers/ViolationUnmarshallerTest.java | 12 ++++++++++++ .../violations/violation-with-incorrect-line.json | 19 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 sonar-ws-client/src/test/resources/violations/violation-with-incorrect-line.json (limited to 'sonar-ws-client/src') diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Violation.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Violation.java index 0d5b816425f..4e170124c6b 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Violation.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Violation.java @@ -74,12 +74,32 @@ public class Violation extends Model { this.severity = priority; } + /** + * @return line number (numeration starts from 1), or null 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 true if violation belongs to concrete line + * @since 2.8 + */ + public boolean hasLine() { + return line != null; } public String getResourceKey() { diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java index dc9a8c4cc2d..4652c2cd9d9 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java @@ -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 index 00000000000..ef2ed50b85c --- /dev/null +++ b/sonar-ws-client/src/test/resources/violations/violation-with-incorrect-line.json @@ -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" + } + } +] -- cgit v1.2.3