ActionPlansWidget.class,
UnresolvedIssuesPerAssigneeWidget.class,
UnresolvedIssuesStatusesWidget.class,
+ org.sonar.api.issue.NoSonarFilter.class,
// issue notifications
SendIssueNotificationsPostJob.class,
package org.sonar.api.checks;
import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Violation;
import org.sonar.api.rules.ViolationFilter;
/**
* @since 2.1
+ * @deprecated in 3.6. Replaced by {@link org.sonar.api.issue.NoSonarFilter}
*/
+@Deprecated
public class NoSonarFilter implements ViolationFilter {
private final Map<Resource, Set<Integer>> noSonarLinesByResource = Maps.newHashMap();
}
public boolean isIgnored(Violation violation) {
+ boolean ignored = false;
if (violation.getResource() != null && violation.getLineId() != null) {
Set<Integer> noSonarLines = noSonarLinesByResource.get(violation.getResource());
- return (noSonarLines != null && noSonarLines.contains(violation.getLineId()));
+ ignored = noSonarLines != null && noSonarLines.contains(violation.getLineId());
+ if (ignored && violation.getRule() != null && StringUtils.containsIgnoreCase(violation.getRule().getKey(), "nosonar")) {
+ ignored = false;
+ }
}
- return false;
+ return ignored;
}
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.issue;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @since 3.6
+ */
+public class NoSonarFilter implements IssueFilter {
+
+ private final Map<String, Set<Integer>> noSonarLinesByResource = Maps.newHashMap();
+
+ public NoSonarFilter addComponent(String componentKey, Set<Integer> noSonarLines) {
+ noSonarLinesByResource.put(componentKey, noSonarLines);
+ return this;
+ }
+
+ @Override
+ public boolean accept(Issue issue) {
+ boolean accepted = true;
+ if (issue.line() != null) {
+ Set<Integer> noSonarLines = noSonarLinesByResource.get(issue.componentKey());
+ accepted = noSonarLines == null || !noSonarLines.contains(issue.line());
+ if (!accepted && StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar")) {
+ accepted = true;
+ }
+ }
+ return accepted;
+ }
+}
import org.junit.Test;
import org.sonar.api.resources.JavaFile;
+import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;
import java.util.HashSet;
import java.util.Set;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
public class NoSonarFilterTest {
+
+ NoSonarFilter filter = new NoSonarFilter();
+
@Test
public void ignoreLinesCommentedWithNoSonar() {
- NoSonarFilter filter = new NoSonarFilter();
JavaFile javaFile = new JavaFile("org.foo.Bar");
Set<Integer> noSonarLines = new HashSet<Integer>();
filter.addResource(javaFile, noSonarLines);
// violation on class
- assertThat(filter.isIgnored(new Violation(null, javaFile)), is(false));
+ assertThat(filter.isIgnored(new Violation(null, javaFile))).isFalse();
// violation on lines
- assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30)), is(false));
- assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(31)), is(true));
+ assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30))).isFalse();
+ assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(31))).isTrue();
}
@Test
public void doNotIgnoreWhenNotFoundInSquid() {
- NoSonarFilter filter = new NoSonarFilter();
JavaFile javaFile = new JavaFile("org.foo.Bar");
+ assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30))).isFalse();
+ }
+
+ @Test
+ public void should_accept_violations_from_no_sonar_rules() throws Exception {
+ // The "No Sonar" rule logs violations on the lines that are flagged with "NOSONAR" !!
+ JavaFile javaFile = new JavaFile("org.foo.Bar");
+
+ Set<Integer> noSonarLines = new HashSet<Integer>();
+ noSonarLines.add(31);
+ filter.addResource(javaFile, noSonarLines);
+
+ Rule noSonarRule = new Rule("squid", "NoSonarCheck");
+ assertThat(filter.isIgnored(new Violation(noSonarRule, javaFile).setLineId(31))).isFalse();
- assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30)), is(false));
}
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.issue;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+
+import java.util.Set;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class NoSonarFilterTest {
+
+ NoSonarFilter filter = new NoSonarFilter();
+
+ @Test
+ public void should_ignore_lines_commented_with_nosonar() {
+ Issue issue = mock(Issue.class);
+ when(issue.componentKey()).thenReturn("struts:org.apache.Action");
+ when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "AvoidCycles"));
+
+ Set<Integer> noSonarLines = ImmutableSet.of(31, 55);
+ filter.addComponent("struts:org.apache.Action", noSonarLines);
+
+ // issue on file
+ assertThat(filter.accept(issue)).isTrue();
+
+ // issue on lines
+ when(issue.line()).thenReturn(31);
+ assertThat(filter.accept(issue)).isFalse();
+
+ when(issue.line()).thenReturn(222);
+ assertThat(filter.accept(issue)).isTrue();
+ }
+
+ @Test
+ public void should_accept_issues_on_no_sonar_rules() throws Exception {
+ // The "No Sonar" rule logs violations on the lines that are flagged with "NOSONAR" !!
+ Issue issue = mock(Issue.class);
+ when(issue.componentKey()).thenReturn("struts:org.apache.Action");
+ when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "NoSonarCheck"));
+
+ Set<Integer> noSonarLines = ImmutableSet.of(31, 55);
+ filter.addComponent("struts:org.apache.Action", noSonarLines);
+
+ when(issue.line()).thenReturn(31);
+ assertThat(filter.accept(issue)).isTrue();
+
+ when(issue.line()).thenReturn(222);
+ assertThat(filter.accept(issue)).isTrue();
+ }
+}