aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-03-27 18:17:09 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-03-27 18:17:09 +0100
commit1f281463046e63d81e398737344969942f6c1a4a (patch)
tree0a50db3762b627b9d598c7cb1c08805e3cbf1501 /sonar-batch
parent6e44175ed6e81ae9989aa767439d91b9a502f5bf (diff)
downloadsonarqube-1f281463046e63d81e398737344969942f6c1a4a.tar.gz
sonarqube-1f281463046e63d81e398737344969942f6c1a4a.zip
Revert "Remove all deprecated Violation APIs"
This reverts commit 5446d877b4e67f2f32ac869e76d9ad02ca226773.
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java23
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DefaultDecoratorContext.java15
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java40
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java37
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java3
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java35
6 files changed, 146 insertions, 7 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java
index b22660d590f..2a70c13ac9d 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java
@@ -37,6 +37,7 @@ import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.*;
+import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.sensor.DefaultSensorContext;
import org.sonar.batch.sensor.coverage.CoverageExclusions;
@@ -182,6 +183,28 @@ public class DeprecatedSensorContext extends DefaultSensorContext implements Sen
}
@Override
+ public void saveViolation(Violation violation, boolean force) {
+ if (violation.getResource() == null) {
+ violation.setResource(resourceOrProject(violation.getResource()));
+ }
+ index.addViolation(violation, force);
+ }
+
+ @Override
+ public void saveViolation(Violation violation) {
+ saveViolation(violation, false);
+ }
+
+ @Override
+ public void saveViolations(Collection<Violation> violations) {
+ if (violations != null) {
+ for (Violation violation : violations) {
+ saveViolation(violation);
+ }
+ }
+ }
+
+ @Override
public Dependency saveDependency(Dependency dependency) {
return index.addDependency(dependency);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DefaultDecoratorContext.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DefaultDecoratorContext.java
index 0b63501eb80..47d75f4434d 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DefaultDecoratorContext.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DefaultDecoratorContext.java
@@ -29,6 +29,7 @@ import org.sonar.api.design.Dependency;
import org.sonar.api.measures.*;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
+import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.duplication.DuplicationUtils;
@@ -213,4 +214,18 @@ public class DefaultDecoratorContext implements DecoratorContext {
public Collection<Dependency> getOutgoingDependencies() {
return sonarIndex.getOutgoingEdges(resource);
}
+
+ @Override
+ public DefaultDecoratorContext saveViolation(Violation violation, boolean force) {
+ if (violation.getResource() == null) {
+ violation.setResource(resource);
+ }
+ sonarIndex.addViolation(violation, force);
+ return this;
+ }
+
+ @Override
+ public DefaultDecoratorContext saveViolation(Violation violation) {
+ return saveViolation(violation, false);
+ }
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
index 7a12b86eaab..33f6246ea8c 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
@@ -38,6 +38,8 @@ import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.MeasuresFilters;
import org.sonar.api.resources.*;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.Violation;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.ProjectTree;
@@ -359,6 +361,44 @@ public class DefaultIndex extends SonarIndex {
return result;
}
+ //
+ //
+ //
+ // VIOLATIONS
+ //
+ //
+ //
+
+ @Override
+ public void addViolation(Violation violation, boolean force) {
+ Resource resource = violation.getResource();
+ if (resource == null) {
+ violation.setResource(currentProject);
+ } else if (!Scopes.isHigherThanOrEquals(resource, Scopes.FILE)) {
+ throw new IllegalArgumentException("Violations are only supported on files, directories and project");
+ }
+
+ Rule rule = violation.getRule();
+ if (rule == null) {
+ LOG.warn("Rule is null. Ignoring violation {}", violation);
+ return;
+ }
+
+ Bucket bucket = getBucket(resource);
+ if (bucket == null) {
+ LOG.warn("Resource is not indexed. Ignoring violation {}", violation);
+ return;
+ }
+
+ // keep a limitation (bug?) of deprecated violations api : severity is always
+ // set by sonar. The severity set by plugins is overridden.
+ // This is not the case with issue api.
+ violation.setSeverity(null);
+
+ violation.setResource(bucket.getResource());
+ moduleIssues.initAndAddViolation(violation);
+ }
+
@Override
public String getSource(Resource reference) {
Resource resource = getResource(reference);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java
index 94938954ebf..64f7e8d2446 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java
@@ -30,8 +30,10 @@ import org.sonar.api.batch.rule.internal.DefaultActiveRule;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.resources.Project;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.Violation;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.MessageException;
+import org.sonar.core.issue.DefaultIssueBuilder;
import javax.annotation.Nullable;
@@ -46,7 +48,7 @@ public class ModuleIssues {
private final Project project;
private final IssueFilters filters;
- public ModuleIssues(ActiveRules activeRules, @Nullable Rules rules, IssueCache cache, Project project, IssueFilters filters) {
+ public ModuleIssues(ActiveRules activeRules, @Nullable Rules rules, IssueCache cache, @Nullable Project project, IssueFilters filters) {
this.activeRules = activeRules;
this.rules = rules;
this.cache = cache;
@@ -54,10 +56,35 @@ public class ModuleIssues {
this.filters = filters;
}
- public ModuleIssues(ActiveRules activeRules, IssueCache cache, Project project, IssueFilters filters) {
+ public ModuleIssues(ActiveRules activeRules, IssueCache cache, @Nullable Project project, IssueFilters filters) {
this(activeRules, null, cache, project, filters);
}
+ /**
+ * Used by scan2
+ */
+ public ModuleIssues(ActiveRules activeRules, Rules rules, IssueCache cache, IssueFilters filters) {
+ this(activeRules, rules, cache, null, filters);
+ }
+
+ public boolean initAndAddViolation(Violation violation) {
+ DefaultIssue issue = newIssue(violation);
+ return initAndAddIssue(issue);
+ }
+
+ private DefaultIssue newIssue(Violation violation) {
+ return new DefaultIssueBuilder()
+ .componentKey(violation.getResource().getEffectiveKey())
+ // Project can be null but Violation not used by scan2
+ .projectKey(project.getRoot().getEffectiveKey())
+ .ruleKey(RuleKey.of(violation.getRule().getRepositoryKey(), violation.getRule().getKey()))
+ .effortToFix(violation.getCost())
+ .line(violation.getLineId())
+ .message(violation.getMessage())
+ .severity(violation.getSeverity() != null ? violation.getSeverity().name() : null)
+ .build();
+ }
+
public boolean initAndAddIssue(DefaultIssue issue) {
RuleKey ruleKey = issue.ruleKey();
Rule rule = null;
@@ -92,8 +119,10 @@ public class ModuleIssues {
if (Strings.isNullOrEmpty(issue.message())) {
issue.setMessage(((DefaultActiveRule) activeRule).name());
}
- issue.setCreationDate(project.getAnalysisDate());
- issue.setUpdateDate(project.getAnalysisDate());
+ if (project != null) {
+ issue.setCreationDate(project.getAnalysisDate());
+ issue.setUpdateDate(project.getAnalysisDate());
+ }
if (issue.severity() == null) {
issue.setSeverity(activeRule.severity());
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java
index 1895e6d103b..c886d93bf51 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java
@@ -23,7 +23,6 @@ package org.sonar.batch.rule;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.picocontainer.injectors.ProviderAdapter;
-import org.sonar.api.batch.RequiresDB;
import org.sonar.api.batch.debt.DebtCharacteristic;
import org.sonar.api.batch.debt.DebtModel;
import org.sonar.api.batch.debt.DebtRemediationFunction;
@@ -40,13 +39,11 @@ import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import javax.annotation.Nullable;
-
import java.util.List;
/**
* Loads all enabled and non manual rules
*/
-@RequiresDB
public class RulesProvider extends ProviderAdapter {
private Rules singleton = null;
diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java
index 6b33dab0611..7ffa9905721 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java
@@ -30,9 +30,13 @@ import org.sonar.api.batch.debt.DebtRemediationFunction;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.RulesBuilder;
import org.sonar.api.issue.internal.DefaultIssue;
+import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
+import org.sonar.api.rules.RulePriority;
+import org.sonar.api.rules.Violation;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.MessageException;
@@ -41,6 +45,7 @@ import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
@@ -196,6 +201,36 @@ public class ModuleIssuesTest {
}
@Test
+ public void add_deprecated_violation() throws Exception {
+ ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME);
+ activeRulesBuilder.create(SQUID_RULE_KEY).setSeverity(Severity.INFO).activate();
+ initModuleIssues();
+
+ org.sonar.api.rules.Rule rule = org.sonar.api.rules.Rule.create("squid", "AvoidCycle", "Avoid Cycle");
+ Resource resource = File.create("org/struts/Action.java").setEffectiveKey("struts:src/org/struts/Action.java");
+ Violation violation = new Violation(rule, resource);
+ violation.setLineId(42);
+ violation.setSeverity(RulePriority.CRITICAL);
+ violation.setMessage("the message");
+
+ when(filters.accept(any(DefaultIssue.class))).thenReturn(true);
+
+ boolean added = moduleIssues.initAndAddViolation(violation);
+ assertThat(added).isTrue();
+
+ ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
+ verify(cache).put(argument.capture());
+ DefaultIssue issue = argument.getValue();
+ assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
+ assertThat(issue.line()).isEqualTo(42);
+ assertThat(issue.message()).isEqualTo("the message");
+ assertThat(issue.key()).isNotEmpty();
+ assertThat(issue.ruleKey().toString()).isEqualTo("squid:AvoidCycle");
+ assertThat(issue.componentKey()).isEqualTo("struts:src/org/struts/Action.java");
+ assertThat(issue.projectKey()).isEqualTo("org.apache:struts-core");
+ }
+
+ @Test
public void filter_issue() throws Exception {
ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME);
activeRulesBuilder.create(SQUID_RULE_KEY).setSeverity(Severity.INFO).activate();