diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-14 17:20:56 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-14 17:20:56 +0100 |
commit | d708b5393189a7fbb54685ed8cfda9ece4076cae (patch) | |
tree | fd10c8f2cd987c24f8703873f056802510af8fd1 /sonar-plugin-api | |
parent | eda36f17ec11950bc73719dd84677591e92f8888 (diff) | |
download | sonarqube-d708b5393189a7fbb54685ed8cfda9ece4076cae.tar.gz sonarqube-d708b5393189a7fbb54685ed8cfda9ece4076cae.zip |
SONAR-3024 Fix backward compatibility of NoSonarFilter
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/checks/NoSonarFilter.java | 12 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java | 19 |
2 files changed, 24 insertions, 7 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/checks/NoSonarFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/checks/NoSonarFilter.java index eb014513054..c4769c944c5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/checks/NoSonarFilter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/checks/NoSonarFilter.java @@ -21,6 +21,7 @@ package org.sonar.api.checks; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; +import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Resource; import org.sonar.api.rules.Violation; import org.sonar.api.rules.ViolationFilter; @@ -36,10 +37,19 @@ import java.util.Set; public class NoSonarFilter implements ViolationFilter { private final Map<Resource, Set<Integer>> noSonarLinesByResource = Maps.newHashMap(); + private SensorContext context; + + public NoSonarFilter(SensorContext context) { + this.context = context; + } public void addResource(Resource resource, Set<Integer> noSonarLines) { if (resource != null && noSonarLines != null) { - noSonarLinesByResource.put(resource, noSonarLines); + // Reload resource to handle backward compatibility of resource keys + resource = context.getResource(resource); + if (resource != null) { + noSonarLinesByResource.put(resource, noSonarLines); + } } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java index a8940dcd39b..70a25607d94 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java @@ -19,7 +19,9 @@ */ package org.sonar.api.checks; +import org.junit.Before; import org.junit.Test; +import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.JavaFile; import org.sonar.api.rules.Rule; import org.sonar.api.rules.Violation; @@ -28,15 +30,23 @@ import java.util.HashSet; 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(); + private SensorContext sensorContext = mock(SensorContext.class); + NoSonarFilter filter = new NoSonarFilter(sensorContext); + private JavaFile javaFile; + + @Before + public void prepare() { + javaFile = new JavaFile("org.foo.Bar"); + when(sensorContext.getResource(javaFile)).thenReturn(javaFile); + } @Test public void ignoreLinesCommentedWithNoSonar() { - JavaFile javaFile = new JavaFile("org.foo.Bar"); - Set<Integer> noSonarLines = new HashSet<Integer>(); noSonarLines.add(31); noSonarLines.add(55); @@ -50,17 +60,14 @@ public class NoSonarFilterTest { assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(31))).isTrue(); } - @Test public void doNotIgnoreWhenNotFoundInSquid() { - 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); |