context.saveDependency(dependency);
}
- protected static Resource toResource(final Project project, Artifact artifact, SensorContext context) {
+ protected Resource toResource(final Project project, Artifact artifact, SensorContext context) {
Project depWithBranch = Project.createFromMavenIds(artifact.getGroupId(), artifact.getArtifactId(), project.getBranch());
Resource result = context.getResource(depWithBranch);
if (result == null || !((Project) result).getAnalysisVersion().equals(artifact.getBaseVersion())) {
Library lib = Library.createFromMavenIds(artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion());
- context.saveResource(lib);
+ index.addResource(lib);
+ resourcePersister.persist();
result = context.getResource(lib);
}
return result;
import java.util.Date;
import java.util.List;
-public final class ResourcePersister implements ScanPersister {
+public class ResourcePersister implements ScanPersister {
private static final String RESOURCE_ID = "resourceId";
private static final String LAST = "last";
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.issue.Issue;
+import org.sonar.api.issue.batch.IssueFilterChain;
import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Violation;
-import org.sonar.api.rules.ViolationFilter;
import java.util.Map;
import java.util.Set;
* @deprecated in 3.6. Replaced by {@link org.sonar.api.issue.NoSonarFilter}
*/
@Deprecated
-public class NoSonarFilter implements ViolationFilter {
+public class NoSonarFilter implements org.sonar.api.issue.batch.IssueFilter {
- private final Map<Resource, Set<Integer>> noSonarLinesByResource = Maps.newHashMap();
+ private final Map<String, Set<Integer>> noSonarLinesByKey = Maps.newHashMap();
private SensorContext context;
public NoSonarFilter(SensorContext context) {
// Reload resource to handle backward compatibility of resource keys
Resource resource = context.getResource(model);
if (resource != null) {
- noSonarLinesByResource.put(resource, noSonarLines);
+ noSonarLinesByKey.put(resource.getEffectiveKey(), noSonarLines);
}
}
}
@Override
- public boolean isIgnored(Violation violation) {
- boolean ignored = false;
- if (violation.getResource() != null && violation.getLineId() != null) {
- Set<Integer> noSonarLines = noSonarLinesByResource.get(violation.getResource());
- ignored = noSonarLines != null && noSonarLines.contains(violation.getLineId());
- if (ignored && violation.getRule() != null && StringUtils.containsIgnoreCase(violation.getRule().getKey(), "nosonar")) {
- ignored = false;
+ public boolean accept(Issue issue, IssueFilterChain chain) {
+ boolean accepted = true;
+ if (issue.line() != null) {
+ Set<Integer> noSonarLines = noSonarLinesByKey.get(issue.componentKey());
+ accepted = noSonarLines == null || !noSonarLines.contains(issue.line());
+ if (!accepted && StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar")) {
+ accepted = true;
}
}
- return ignored;
+ if (accepted) {
+ accepted = chain.accept(issue);
+ }
+ return accepted;
}
}
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.issue.Issue;
+import org.sonar.api.issue.batch.IssueFilterChain;
import org.sonar.api.resources.File;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.Violation;
+import org.sonar.api.rule.RuleKey;
import java.util.HashSet;
import java.util.Set;
import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
private SensorContext sensorContext = mock(SensorContext.class);
NoSonarFilter filter = new NoSonarFilter(sensorContext);
private File javaFile;
+ IssueFilterChain chain = mock(IssueFilterChain.class);
@Before
public void prepare() {
+ when(chain.accept(isA(Issue.class))).thenReturn(true);
javaFile = new File("org.foo.Bar");
+ javaFile.setEffectiveKey("struts:org.apache.Action");
when(sensorContext.getResource(javaFile)).thenReturn(javaFile);
}
noSonarLines.add(55);
filter.addResource(javaFile, noSonarLines);
+ Issue issue = mock(Issue.class);
+ when(issue.componentKey()).thenReturn("struts:org.apache.Action");
+ when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "Foo"));
+
// violation on class
- assertThat(filter.isIgnored(new Violation(null, javaFile))).isFalse();
+ assertThat(filter.accept(issue, chain)).isTrue();
// violation on lines
- assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30))).isFalse();
- assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(31))).isTrue();
- }
-
- @Test
- public void doNotIgnoreWhenNotFoundInSquid() {
- assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30))).isFalse();
+ when(issue.line()).thenReturn(30);
+ assertThat(filter.accept(issue, chain)).isTrue();
+ when(issue.line()).thenReturn(31);
+ assertThat(filter.accept(issue, chain)).isFalse();
}
@Test
noSonarLines.add(31);
filter.addResource(javaFile, noSonarLines);
- Rule noSonarRule = new Rule("squid", "NoSonarCheck");
- assertThat(filter.isIgnored(new Violation(noSonarRule, javaFile).setLineId(31))).isFalse();
+ Issue issue = mock(Issue.class);
+ when(issue.componentKey()).thenReturn("struts:org.apache.Action");
+ when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "NoSonarCheck"));
+
+ when(issue.line()).thenReturn(31);
+ assertThat(filter.accept(issue, chain)).isTrue();
}
}