diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-04-27 20:29:11 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-04-27 22:10:58 +0400 |
commit | 25f963bfb953346ee85337009b1af7d665494aa0 (patch) | |
tree | f1ae028f197e0538460c5d3e349832915bf49bdb /sonar-plugin-api | |
parent | 70c03e6ac1e0f24277655f9e7666142d9418d270 (diff) | |
download | sonarqube-25f963bfb953346ee85337009b1af7d665494aa0.tar.gz sonarqube-25f963bfb953346ee85337009b1af7d665494aa0.zip |
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.
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java | 25 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java | 48 |
2 files changed, 69 insertions, 4 deletions
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 3e4aa063a8b..46f9dcaf428 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 @@ -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,23 +109,39 @@ 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 */ public RulePriority getSeverity() { @@ -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 index 00000000000..a77bcd44cc9 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/ViolationTest.java @@ -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)); + } +} |