import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
-import org.sonar.api.violations.ViolationQuery;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.duplication.DuplicationUtils;
import org.sonar.batch.scan.measure.MeasureCache;
return this;
}
- /**
- * {@inheritDoc}
- */
- @Override
- public List<Violation> getViolations(ViolationQuery violationQuery) {
- return sonarIndex.getViolations(violationQuery);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public List<Violation> getViolations() {
- return sonarIndex.getViolations(resource);
- }
-
@Override
public Dependency saveDependency(Dependency dependency) {
checkReadOnly("addDependency");
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.batch;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.rules.Violation;
-import org.sonar.api.rules.ViolationFilter;
-
-public class ViolationFilters {
-
- private static final Logger LOG = LoggerFactory.getLogger(ViolationFilters.class);
-
- private ViolationFilter[] filters;
-
- public ViolationFilters(ViolationFilter[] filters) {
- this.filters = filters;
- }
-
- public ViolationFilters() {
- this(new ViolationFilter[0]);
- }
-
- public boolean isEmpty() {
- return filters.length==0;
- }
-
- /**
- * Return true if the violation must be saved. If false then it is ignored.
- */
- public boolean isIgnored(Violation violation) {
- boolean ignored = false;
- int index = 0;
- while (!ignored && index < filters.length) {
- ignored = filters[index].isIgnored(violation);
- if (ignored && LOG.isDebugEnabled()) {
- LOG.debug("Violation {} is excluded by the filter {}", violation, filters[index]);
- }
- index++;
- }
- return ignored;
- }
-}
import org.sonar.api.rules.Violation;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.SonarException;
-import org.sonar.api.violations.ViolationQuery;
import org.sonar.batch.ProjectTree;
-import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.batch.scan.measure.MeasureCache;
import org.sonar.batch.scan2.DefaultSensorContext;
private Map<Resource, Map<Resource, Dependency>> outgoingDependenciesByResource = Maps.newLinkedHashMap();
private Map<Resource, Map<Resource, Dependency>> incomingDependenciesByResource = Maps.newLinkedHashMap();
private ProjectTree projectTree;
- private final DeprecatedViolations deprecatedViolations;
private ModuleIssues moduleIssues;
private final MeasureCache measureCache;
private final ResourceKeyMigration migration;
public DefaultIndex(ResourceCache resourceCache, DependencyPersister dependencyPersister,
LinkPersister linkPersister, EventPersister eventPersister, ProjectTree projectTree, MetricFinder metricFinder,
- DeprecatedViolations deprecatedViolations, ResourceKeyMigration migration, MeasureCache measureCache) {
+ ResourceKeyMigration migration, MeasureCache measureCache) {
this.resourceCache = resourceCache;
this.dependencyPersister = dependencyPersister;
this.linkPersister = linkPersister;
this.eventPersister = eventPersister;
this.projectTree = projectTree;
this.metricFinder = metricFinder;
- this.deprecatedViolations = deprecatedViolations;
+ this.migration = migration;
+ this.measureCache = measureCache;
+ }
+
+ public DefaultIndex(ResourceCache resourceCache, ProjectTree projectTree, MetricFinder metricFinder, ResourceKeyMigration migration, MeasureCache measureCache) {
+ this.resourceCache = resourceCache;
+ this.dependencyPersister = null;
+ this.linkPersister = null;
+ this.eventPersister = null;
+ this.projectTree = projectTree;
+ this.metricFinder = metricFinder;
this.migration = migration;
this.measureCache = measureCache;
}
addDependency(parentDependency);
}
- if (registerDependency(dependency)) {
+ if (registerDependency(dependency) && dependencyPersister != null) {
dependencyPersister.saveDependency(currentProject, from, to, dependency, parentDependency);
}
return dependency;
//
//
- /**
- * {@inheritDoc}
- */
- @Override
- public List<Violation> getViolations(ViolationQuery violationQuery) {
- Resource resource = violationQuery.getResource();
- if (resource == null) {
- throw new IllegalArgumentException("A resource must be set on the ViolationQuery in order to search for violations.");
- }
-
- if (!Scopes.isHigherThanOrEquals(resource, Scopes.FILE)) {
- return Collections.emptyList();
- }
-
- Bucket bucket = buckets.get(resource);
- if (bucket == null) {
- return Collections.emptyList();
- }
-
- List<Violation> violations = deprecatedViolations.get(bucket.getResource().getEffectiveKey());
- if (violationQuery.getSwitchMode() == ViolationQuery.SwitchMode.BOTH) {
- return violations;
- }
-
- List<Violation> filteredViolations = Lists.newArrayList();
- for (Violation violation : violations) {
- if (isFiltered(violation, violationQuery.getSwitchMode())) {
- filteredViolations.add(violation);
- }
- }
- return filteredViolations;
- }
-
- private static boolean isFiltered(Violation violation, ViolationQuery.SwitchMode mode) {
- return mode == ViolationQuery.SwitchMode.BOTH || isSwitchOff(violation, mode) || isSwitchOn(violation, mode);
- }
-
- private static boolean isSwitchOff(Violation violation, ViolationQuery.SwitchMode mode) {
- return mode == ViolationQuery.SwitchMode.OFF && violation.isSwitchedOff();
- }
-
- private static boolean isSwitchOn(Violation violation, ViolationQuery.SwitchMode mode) {
- return mode == ViolationQuery.SwitchMode.ON && !violation.isSwitchedOff();
- }
-
@Override
public void addViolation(Violation violation, boolean force) {
Resource resource = violation.getResource();
@Override
public void addLink(ProjectLink link) {
- linkPersister.saveLink(currentProject, link);
+ if (linkPersister != null) {
+ linkPersister.saveLink(currentProject, link);
+ }
}
@Override
public void deleteLink(String key) {
- linkPersister.deleteLink(currentProject, key);
+ if (linkPersister != null) {
+ linkPersister.deleteLink(currentProject, key);
+ }
}
//
if (reload == null) {
return Collections.emptyList();
}
+ if (eventPersister == null) {
+ throw new UnsupportedOperationException("Event are not available in preview mode");
+ }
return eventPersister.getEvents(reload);
}
@Override
public void deleteEvent(Event event) {
- eventPersister.deleteEvent(event);
+ if (eventPersister != null) {
+ eventPersister.deleteEvent(event);
+ }
}
@Override
event.setDate(date);
event.setCreatedAt(new Date());
- eventPersister.saveEvent(resource, event);
+ if (eventPersister != null) {
+ eventPersister.saveEvent(resource, event);
+ }
return null;
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.batch.issue;
-
-import com.google.common.collect.Lists;
-import org.sonar.api.BatchComponent;
-import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RulePriority;
-import org.sonar.api.rules.Violation;
-import org.sonar.batch.index.ResourceCache;
-
-import java.util.List;
-
-/**
- * Bridge with violations, that have been deprecated in 3.6.
- *
- * @since 3.6
- */
-public class DeprecatedViolations implements BatchComponent {
-
- private final IssueCache issueCache;
- private final RuleFinder ruleFinder;
- private final ResourceCache resourceCache;
-
- public DeprecatedViolations(IssueCache issueCache, RuleFinder ruleFinder, ResourceCache resourceCache) {
- this.issueCache = issueCache;
- this.ruleFinder = ruleFinder;
- this.resourceCache = resourceCache;
- }
-
- public List<Violation> get(String componentKey) {
- Iterable<DefaultIssue> issues = issueCache.byComponent(componentKey);
- List<Violation> violations = Lists.newArrayList();
- for (DefaultIssue issue : issues) {
- violations.add(toViolation(issue));
- }
- return violations;
- }
-
- public Violation toViolation(DefaultIssue issue) {
- Rule rule = ruleFinder.findByKey(issue.ruleKey());
- Resource resource = resourceCache.get(issue.componentKey()).resource();
- Violation violation = new Violation(rule, resource);
- violation.setNew(issue.isNew());
- violation.setChecksum(issue.checksum());
- violation.setMessage(issue.message());
- violation.setCost(issue.effortToFix());
- violation.setLineId(issue.line());
- violation.setCreatedAt(issue.creationDate());
- violation.setManual(issue.reporter() != null);
- violation.setSeverity(RulePriority.valueOf(issue.severity()));
- violation.setSwitchedOff(issue.resolution() != null);
- return violation;
- }
-}
import org.sonar.api.BatchComponent;
import org.sonar.api.issue.batch.IssueFilter;
import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.rules.Violation;
-import org.sonar.batch.ViolationFilters;
-
-import javax.annotation.Nullable;
public class IssueFilters implements BatchComponent {
- private final ViolationFilters deprecatedFilters;
- private final DeprecatedViolations deprecatedViolations;
private final org.sonar.api.issue.IssueFilter[] exclusionFilters;
private final IssueFilter[] filters;
- public IssueFilters(@Nullable ViolationFilters deprecatedFilters, @Nullable DeprecatedViolations deprecatedViolations, org.sonar.api.issue.IssueFilter[] exclusionFilters,
- IssueFilter[] filters) {
- this.deprecatedFilters = deprecatedFilters;
- this.deprecatedViolations = deprecatedViolations;
+ public IssueFilters(org.sonar.api.issue.IssueFilter[] exclusionFilters, IssueFilter[] filters) {
this.exclusionFilters = exclusionFilters;
this.filters = filters;
}
- public IssueFilters(@Nullable ViolationFilters deprecatedFilters, @Nullable DeprecatedViolations deprecatedViolations, IssueFilter[] filters) {
- this(deprecatedFilters, deprecatedViolations, new org.sonar.api.issue.IssueFilter[0], filters);
- }
-
- public IssueFilters(@Nullable ViolationFilters deprecatedFilters, @Nullable DeprecatedViolations deprecatedViolations, org.sonar.api.issue.IssueFilter[] exclusionFilters) {
- this(deprecatedFilters, deprecatedViolations, exclusionFilters, new IssueFilter[0]);
- }
-
- public IssueFilters(@Nullable ViolationFilters deprecatedFilters, @Nullable DeprecatedViolations deprecatedViolations) {
- this(deprecatedFilters, deprecatedViolations, new org.sonar.api.issue.IssueFilter[0]);
- }
-
- /**
- * Used by scan2
- */
- public IssueFilters(org.sonar.api.issue.IssueFilter[] exclusionFilters, IssueFilter[] filters) {
- this(null, null, exclusionFilters, filters);
- }
-
public IssueFilters(org.sonar.api.issue.IssueFilter[] exclusionFilters) {
- this(null, null, exclusionFilters, new IssueFilter[0]);
+ this(exclusionFilters, new IssueFilter[0]);
}
public IssueFilters(IssueFilter[] filters) {
- this(null, null, new org.sonar.api.issue.IssueFilter[0], filters);
+ this(new org.sonar.api.issue.IssueFilter[0], filters);
}
public IssueFilters() {
- this(null, null, new org.sonar.api.issue.IssueFilter[0], new IssueFilter[0]);
+ this(new org.sonar.api.issue.IssueFilter[0], new IssueFilter[0]);
}
- public boolean accept(DefaultIssue issue, @Nullable Violation violation) {
+ public boolean accept(DefaultIssue issue) {
if (new DefaultIssueFilterChain(filters).accept(issue)) {
// Apply deprecated rules only if filter chain accepts the current issue
for (org.sonar.api.issue.IssueFilter filter : exclusionFilters) {
return false;
}
}
- if (deprecatedFilters != null && !deprecatedFilters.isEmpty() && deprecatedViolations != null) {
- Violation v = violation != null ? violation : deprecatedViolations.toViolation(issue);
- return !deprecatedFilters.isIgnored(v);
- }
return true;
} else {
return false;
this(activeRules, rules, cache, null, filters);
}
- public boolean initAndAddIssue(DefaultIssue issue) {
- return initAndAddIssue(issue, null);
- }
-
public boolean initAndAddViolation(Violation violation) {
DefaultIssue issue = newIssue(violation);
- return initAndAddIssue(issue, violation);
+ return initAndAddIssue(issue);
}
private DefaultIssue newIssue(Violation violation) {
.build();
}
- private boolean initAndAddIssue(DefaultIssue issue, @Nullable Violation violation) {
+ public boolean initAndAddIssue(DefaultIssue issue) {
RuleKey ruleKey = issue.ruleKey();
Rule rule = rules.find(ruleKey);
validateRule(issue, rule);
return false;
}
updateIssue(issue, rule, activeRule);
- if (filters.accept(issue, violation)) {
+ if (filters.accept(issue)) {
cache.put(issue);
return true;
}
import org.sonar.batch.DefaultTimeMachine;
import org.sonar.batch.ProjectTree;
import org.sonar.batch.ResourceFilters;
-import org.sonar.batch.ViolationFilters;
import org.sonar.batch.bootstrap.BatchExtensionDictionnary;
import org.sonar.batch.bootstrap.ExtensionInstaller;
import org.sonar.batch.bootstrap.ExtensionMatcher;
import org.sonar.batch.qualitygate.GenerateQualityGateEvents;
import org.sonar.batch.qualitygate.QualityGateProvider;
import org.sonar.batch.qualitygate.QualityGateVerifier;
-import org.sonar.batch.report.IssuesPublisher;
import org.sonar.batch.report.ComponentsPublisher;
+import org.sonar.batch.report.IssuesPublisher;
import org.sonar.batch.rule.ActiveRulesProvider;
import org.sonar.batch.rule.ModuleQProfiles;
import org.sonar.batch.rule.QProfileDecorator;
SensorContextAdapter.class,
BatchExtensionDictionnary.class,
DefaultTimeMachine.class,
- ViolationFilters.class,
IssueFilters.class,
MeasurementFilters.class,
ResourceFilters.class,
import org.sonar.batch.DefaultResourceCreationLock;
import org.sonar.batch.ProjectConfigurator;
import org.sonar.batch.ProjectTree;
+import org.sonar.batch.bootstrap.BootstrapProperties;
import org.sonar.batch.bootstrap.ExtensionInstaller;
import org.sonar.batch.bootstrap.ExtensionMatcher;
import org.sonar.batch.bootstrap.ExtensionUtils;
import org.sonar.batch.index.Caches;
import org.sonar.batch.index.ComponentDataCache;
import org.sonar.batch.index.DefaultIndex;
-import org.sonar.batch.index.ResourcePersister;
import org.sonar.batch.index.DependencyPersister;
import org.sonar.batch.index.DuplicationPersister;
import org.sonar.batch.index.EventPersister;
import org.sonar.batch.index.MeasurePersister;
import org.sonar.batch.index.ResourceCache;
import org.sonar.batch.index.ResourceKeyMigration;
+import org.sonar.batch.index.ResourcePersister;
import org.sonar.batch.index.SourcePersister;
import org.sonar.batch.issue.DefaultProjectIssues;
-import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.IssueCache;
import org.sonar.batch.languages.DefaultLanguagesReferential;
import org.sonar.batch.phases.GraphPersister;
import org.sonar.core.user.DefaultUserFinder;
public class ProjectScanContainer extends ComponentContainer {
+ private boolean sensorMode;
+
public ProjectScanContainer(ComponentContainer taskContainer) {
super(taskContainer);
+ sensorMode = CoreProperties.ANALYSIS_MODE_SENSOR.equals(taskContainer.getComponentByType(BootstrapProperties.class).property(CoreProperties.ANALYSIS_MODE));
}
@Override
protected void doBeforeStart() {
projectBootstrap();
addBatchComponents();
+ if (!sensorMode) {
+ addDataBaseComponents();
+ }
fixMavenExecutor();
addBatchExtensions();
Settings settings = getComponentByType(Settings.class);
add(
new ProjectReferentialsProvider(),
DefaultResourceCreationLock.class,
- DependencyPersister.class,
- EventPersister.class,
- LinkPersister.class,
- MeasurePersister.class,
- DuplicationPersister.class,
- ResourcePersister.class,
- SourcePersister.class,
CodeColorizers.class,
DefaultNotificationManager.class,
MetricProvider.class,
IssueUpdater.class,
FunctionExecutor.class,
IssueWorkflow.class,
- DeprecatedViolations.class,
IssueCache.class,
IssueNotifications.class,
DefaultProjectIssues.class,
ProjectSettings.class);
}
+ private void addDataBaseComponents() {
+ add(
+ DependencyPersister.class,
+ EventPersister.class,
+ LinkPersister.class,
+ MeasurePersister.class,
+ DuplicationPersister.class,
+ ResourcePersister.class,
+ SourcePersister.class);
+ }
+
private void fixMavenExecutor() {
if (getComponentByType(MavenPluginExecutor.class) == null) {
add(FakeMavenPluginExecutor.class);
updateIssue((DefaultIssue) issue, activeRule);
- if (!issueFilters.accept(SensorContextAdapter.toDefaultIssue(def.getKey(), resourceKey, issue), null)) {
+ if (!issueFilters.accept(SensorContextAdapter.toDefaultIssue(def.getKey(), resourceKey, issue))) {
LOG.debug("Issue {} was excluded by some filters.", issue);
return;
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.batch;
-
-import org.junit.Test;
-import org.sonar.api.rules.Violation;
-import org.sonar.api.rules.ViolationFilter;
-
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
-
-public class ViolationFiltersTest {
-
- @Test
- public void doNotFailIfNoFilters() {
- ViolationFilters filters = new ViolationFilters();
- assertThat(filters.isIgnored(new Violation(null)), is(false));
- }
-
- @Test
- public void ignoreViolation() {
- ViolationFilters filters = new ViolationFilters(new ViolationFilter[]{
- new FakeFilter(false),
- new FakeFilter(true),
- new FakeFilter(false),
- });
- assertThat(filters.isIgnored(new Violation(null)), is(true));
- }
-
- @Test
- public void doNotIgnoreValidViolations() {
- ViolationFilters filters = new ViolationFilters(new ViolationFilter[]{
- new FakeFilter(false),
- new FakeFilter(false),
- new FakeFilter(false),
- });
- assertThat(filters.isIgnored(new Violation(null)), is(false));
- }
-
- private static class FakeFilter implements ViolationFilter {
- private boolean ignore;
-
- private FakeFilter(boolean ignore) {
- this.ignore = ignore;
- }
-
- public boolean isIgnored(Violation violation) {
- return ignore;
- }
- }
-}
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.Violation;
-import org.sonar.api.violations.ViolationQuery;
import org.sonar.batch.ProjectTree;
-import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.batch.scan.measure.MeasureCache;
import java.io.IOException;
-import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public TemporaryFolder temp = new TemporaryFolder();
DefaultIndex index = null;
- DeprecatedViolations deprecatedViolations;
Rule rule;
RuleFinder ruleFinder;
Project project;
@Before
public void createIndex() throws IOException {
- deprecatedViolations = mock(DeprecatedViolations.class);
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
ruleFinder = mock(RuleFinder.class);
ProjectTree projectTree = mock(ProjectTree.class);
ResourceCache resourceCache = new ResourceCache();
- index = new DefaultIndex(resourceCache, null, null, null, projectTree, metricFinder, deprecatedViolations,
+ index = new DefaultIndex(resourceCache, null, null, null, projectTree, metricFinder,
mock(ResourceKeyMigration.class),
mock(MeasureCache.class));
assertThat(index.getMeasures(dir, MeasuresFilters.metric("ncloc"))).isNull();
}
- /**
- * See http://jira.codehaus.org/browse/SONAR-2107
- */
- @Test
- public void shouldNotFailWhenSavingViolationOnNullRule() {
- File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false);
- Violation violation = Violation.create((Rule) null, file);
- index.addViolation(violation);
-
- assertThat(index.getViolations(file)).isEmpty();
- }
-
- /**
- * See https://jira.codehaus.org/browse/SONAR-3583
- */
- @Test
- public void should_ignore_violation_on_unknown_rules() {
- Rule ruleWithoutID = Rule.create("repoKey", "ruleKey", "Rule");
-
- File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false);
- Violation violation = Violation.create(ruleWithoutID, file);
- index.addViolation(violation);
-
- assertThat(index.getViolations(file)).isEmpty();
- }
-
- @Test
- public void should_get_violation() {
- Rule rule = Rule.create("repoKey", "ruleKey", "Rule");
- File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false);
- Violation violation = Violation.create(rule, file);
- when(deprecatedViolations.get(anyString())).thenReturn(newArrayList(violation));
-
- index.index(file);
- index.addViolation(violation);
-
- assertThat(index.getViolations(file)).hasSize(1);
- }
-
- @Test
- public void should_not_save_violation_if_resource_not_indexed() {
- Rule rule = Rule.create("repoKey", "ruleKey", "Rule");
- File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false);
- Violation violation = Violation.create(rule, file);
- when(deprecatedViolations.get(anyString())).thenReturn(newArrayList(violation));
-
- index.addViolation(violation);
-
- assertThat(index.getViolations(file)).hasSize(0);
- }
-
- @Test
- public void should_get_filtered_violation_with_off_switch_mode() {
- Rule rule = Rule.create("repoKey", "ruleKey", "Rule");
- File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false);
- Violation violation = Violation.create(rule, file).setSwitchedOff(true);
-
- when(deprecatedViolations.get(anyString())).thenReturn(newArrayList(violation));
-
- index.index(file);
- index.addViolation(violation);
-
- assertThat(index.getViolations(ViolationQuery.create().forResource(file).setSwitchMode(ViolationQuery.SwitchMode.OFF))).hasSize(1);
- }
-
- @Test
- public void should_get_filtered_violation_with_on_switch_mode() {
- Rule rule = Rule.create("repoKey", "ruleKey", "Rule");
- File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false);
- Violation violation = Violation.create(rule, file).setSwitchedOff(false);
-
- when(deprecatedViolations.get(anyString())).thenReturn(newArrayList(violation));
-
- index.index(file);
- index.addViolation(violation);
-
- assertThat(index.getViolations(ViolationQuery.create().forResource(file).setSwitchMode(ViolationQuery.SwitchMode.ON))).hasSize(1);
- }
-
@Test
public void shouldComputePathOfIndexedModules() {
assertThat(index.getResource(project).getPath()).isNull();
assertThat(index.getResource(moduleB1).getPath()).isEqualTo("moduleB1");
}
- @Test(expected = IllegalArgumentException.class)
- public void testGetViolationsWithQueryWithNoResource() {
- index.getViolations(ViolationQuery.create());
- }
-
}
import org.sonar.api.resources.Resource;
import org.sonar.api.security.ResourcePermissions;
import org.sonar.batch.ProjectTree;
-import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.scan.measure.MeasureCache;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ScanGraph;
when(projectTree.getProjectDefinition(moduleB1)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleB/moduleB1")));
DefaultIndex index = new DefaultIndex(resourceCache, null, null, null, projectTree, mock(MetricFinder.class),
- mock(DeprecatedViolations.class),
mock(ResourceKeyMigration.class),
mock(MeasureCache.class));
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.batch.issue;
-
-import org.junit.Test;
-import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.resources.Project;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RulePriority;
-import org.sonar.api.rules.Violation;
-import org.sonar.batch.index.BatchResource;
-import org.sonar.batch.index.ResourceCache;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class DeprecatedViolationsTest {
-
- IssueCache issueCache = mock(IssueCache.class);
- RuleFinder ruleFinder = mock(RuleFinder.class);
- ResourceCache resourceCache = mock(ResourceCache.class);
- DeprecatedViolations deprecatedViolations = new DeprecatedViolations(issueCache, ruleFinder, resourceCache);
-
- @Test
- public void test_toViolation() throws Exception {
- RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
- when(ruleFinder.findByKey(ruleKey)).thenReturn(new Rule("squid", "AvoidCycles"));
- when(resourceCache.get("org.apache:struts")).thenReturn(new BatchResource(1, new Project("org.apache:struts"), null));
-
- DefaultIssue issue = newIssue(ruleKey);
-
- Violation violation = deprecatedViolations.toViolation(issue);
- assertThat(violation.getLineId()).isEqualTo(42);
- assertThat(violation.getSeverity()).isEqualTo(RulePriority.BLOCKER);
- assertThat(violation.isManual()).isTrue();
- assertThat(violation.getRule().getRepositoryKey()).isEqualTo("squid");
- assertThat(violation.getRule().getKey()).isEqualTo("AvoidCycles");
- assertThat(violation.getResource()).isNotNull();
- assertThat(violation.isSwitchedOff()).isFalse();
- }
-
- private DefaultIssue newIssue(RuleKey ruleKey) {
- DefaultIssue issue = new DefaultIssue();
- issue.setKey("ABCDE");
- issue.setRuleKey(ruleKey);
- issue.setComponentKey("org.apache:struts");
- issue.setLine(42);
- issue.setEffortToFix(3.14);
- issue.setReporter("leon");
- issue.setSeverity(Severity.BLOCKER);
- return issue;
- }
-
- @Test
- public void test_get() throws Exception {
- RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
- when(ruleFinder.findByKey(ruleKey)).thenReturn(new Rule("squid", "AvoidCycles"));
- when(resourceCache.get("org.apache:struts")).thenReturn(new BatchResource(1, new Project("org.apache:struts"), null));
- when(issueCache.byComponent("org.apache:struts")).thenReturn(Arrays.asList(newIssue(ruleKey)));
-
- List<Violation> violations = deprecatedViolations.get("org.apache:struts");
-
- assertThat(violations).hasSize(1);
- }
-}
import org.junit.Test;
import org.sonar.api.issue.Issue;
-import org.sonar.api.issue.batch.IssueFilter;
import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.rules.Violation;
-import org.sonar.batch.ViolationFilters;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
public class IssueFiltersTest {
- DeprecatedViolations deprecatedViolations = mock(DeprecatedViolations.class);
- ViolationFilters deprecatedFilters = mock(ViolationFilters.class);
-
@Test
public void accept_when_filter_chain_is_empty() throws Exception {
org.sonar.api.issue.IssueFilter ok = mock(org.sonar.api.issue.IssueFilter.class);
org.sonar.api.issue.IssueFilter ko = mock(org.sonar.api.issue.IssueFilter.class);
when(ko.accept(any(Issue.class))).thenReturn(false);
- when(deprecatedFilters.isEmpty()).thenReturn(true);
- IssueFilters filters = new IssueFilters(deprecatedFilters, deprecatedViolations, new org.sonar.api.issue.IssueFilter[]{ok, ko});
- assertThat(filters.accept(new DefaultIssue(), null)).isFalse();
+ IssueFilters filters = new IssueFilters(new org.sonar.api.issue.IssueFilter[] {ok, ko});
+ assertThat(filters.accept(new DefaultIssue())).isFalse();
- filters = new IssueFilters(deprecatedFilters, deprecatedViolations, new org.sonar.api.issue.IssueFilter[]{ok});
- assertThat(filters.accept(new DefaultIssue(), null)).isTrue();
+ filters = new IssueFilters(new org.sonar.api.issue.IssueFilter[] {ok});
+ assertThat(filters.accept(new DefaultIssue())).isTrue();
- filters = new IssueFilters(deprecatedFilters, deprecatedViolations, new org.sonar.api.issue.IssueFilter[]{ko});
- assertThat(filters.accept(new DefaultIssue(), null)).isFalse();
+ filters = new IssueFilters(new org.sonar.api.issue.IssueFilter[] {ko});
+ assertThat(filters.accept(new DefaultIssue())).isFalse();
}
@Test
public void should_always_accept_if_no_filters() {
- when(deprecatedFilters.isEmpty()).thenReturn(true);
- IssueFilters filters = new IssueFilters(deprecatedFilters, deprecatedViolations);
- assertThat(filters.accept(new DefaultIssue(), null)).isTrue();
- }
-
- @Test
- public void should_check_deprecated_violation_filters() throws Exception {
- when(deprecatedFilters.isEmpty()).thenReturn(false);
- when(deprecatedFilters.isIgnored(any(Violation.class))).thenReturn(true);
- IssueFilters filters = new IssueFilters(deprecatedFilters, deprecatedViolations, new org.sonar.api.issue.IssueFilter[0]);
- assertThat(filters.accept(new DefaultIssue(), null)).isFalse();
-
+ IssueFilters filters = new IssueFilters();
+ assertThat(filters.accept(new DefaultIssue())).isTrue();
}
}
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
.setKey("ABCDE")
.setRuleKey(SQUID_RULE_KEY)
.setSeverity(Severity.CRITICAL);
- when(filters.accept(issue, null)).thenReturn(true);
+ when(filters.accept(issue)).thenReturn(true);
boolean added = moduleIssues.initAndAddIssue(issue);
when(project.getAnalysisDate()).thenReturn(analysisDate);
DefaultIssue issue = new DefaultIssue().setRuleKey(SQUID_RULE_KEY).setSeverity(null);
- when(filters.accept(issue, null)).thenReturn(true);
+ when(filters.accept(issue)).thenReturn(true);
moduleIssues.initAndAddIssue(issue);
ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
.setRuleKey(SQUID_RULE_KEY)
.setSeverity(Severity.CRITICAL)
.setMessage("");
- when(filters.accept(issue, null)).thenReturn(true);
+ when(filters.accept(issue)).thenReturn(true);
boolean added = moduleIssues.initAndAddIssue(issue);
violation.setSeverity(RulePriority.CRITICAL);
violation.setMessage("the message");
- when(filters.accept(any(DefaultIssue.class), eq(violation))).thenReturn(true);
+ when(filters.accept(any(DefaultIssue.class))).thenReturn(true);
boolean added = moduleIssues.initAndAddViolation(violation);
assertThat(added).isTrue();
.setRuleKey(SQUID_RULE_KEY)
.setSeverity(Severity.CRITICAL);
- when(filters.accept(issue, null)).thenReturn(false);
+ when(filters.accept(issue)).thenReturn(false);
boolean added = moduleIssues.initAndAddIssue(issue);
.setSeverity(Severity.CRITICAL)
.setEffortToFix(2d);
- when(filters.accept(issue, null)).thenReturn(true);
+ when(filters.accept(issue)).thenReturn(true);
moduleIssues.initAndAddIssue(issue);
ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
.setSeverity(Severity.CRITICAL)
.setEffortToFix(2d);
- when(filters.accept(issue, null)).thenReturn(true);
+ when(filters.accept(issue)).thenReturn(true);
moduleIssues.initAndAddIssue(issue);
ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
.setSeverity(Severity.CRITICAL)
.setEffortToFix(null);
- when(filters.accept(issue, null)).thenReturn(true);
+ when(filters.accept(issue)).thenReturn(true);
moduleIssues.initAndAddIssue(issue);
ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
.setSeverity(Severity.CRITICAL)
.setEffortToFix(2d);
- when(filters.accept(issue, null)).thenReturn(true);
+ when(filters.accept(issue)).thenReturn(true);
try {
moduleIssues.initAndAddIssue(issue);
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Violation;
-import org.sonar.api.violations.ViolationQuery;
import java.util.Collection;
import java.util.Date;
// RULES
- /**
- * Returns the violations that match the {@link ViolationQuery} parameters.
- *
- * @since 2.8
- * @param violationQuery
- * the request parameters specified as a {@link ViolationQuery}
- * @return the list of violations that match those parameters
- * @deprecated in 3.6, replaced by {@link org.sonar.api.issue.Issuable}
- */
- @Deprecated
- List<Violation> getViolations(ViolationQuery violationQuery);
-
- /**
- * Returns all the active (= non switched-off) violations found on the current resource.
- *
- * @return the list of violations
- * @deprecated in 3.6, replaced by {@link org.sonar.api.issue.Issuable}
- */
- @Deprecated
- List<Violation> getViolations();
-
/**
* Save a coding rule violation. The decorator which calls this method must be depended upon BatchBarriers.END_OF_VIOLATIONS_GENERATION.
*
import org.sonar.api.resources.ProjectLink;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Violation;
-import org.sonar.api.violations.ViolationQuery;
import org.sonar.graph.DirectedGraphAccessor;
import javax.annotation.CheckForNull;
@CheckForNull
public abstract <M> M getMeasures(Resource resource, MeasuresFilter<M> filter);
- /**
- * Returns the violations that match the {@link ViolationQuery} parameters.
- *
- * @since 2.8
- * @param violationQuery
- * the request parameters specified as a {@link ViolationQuery}
- * @return the list of violations that match those parameters
- * @deprecated in 3.6
- */
- @Deprecated
- public abstract List<Violation> getViolations(ViolationQuery violationQuery);
-
- /**
- * Returns all the active (= non switched-off) violations found on the given resource. Equivalent to
- * {@link #getViolations(ViolationQuery)} called with <code>ViolationQuery.create().forResource(resource).ignoreSwitchedOff(true)</code>
- * as a parameter.
- *
- * @since 2.7
- * @return the list of violations
- * @deprecated in 3.6
- */
- @Deprecated
- public final List<Violation> getViolations(Resource resource) {
- return getViolations(ViolationQuery.create().forResource(resource));
- }
-
/**
* @since 2.5
* @deprecated in 3.6
/**
* Creates of a violation from a rule. Will need to define the resource later on
*
- * @deprecated since 2.3. Use the factory method create()
+ * @deprecated since 2.3. Use the factory method {@link #create(ActiveRule, Resource)}
*/
@Deprecated
public Violation(Rule rule) {
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.rules;
-
-import org.sonar.api.BatchExtension;
-import org.sonar.api.batch.DecoratorBarriers;
-import org.sonar.api.batch.DependedUpon;
-
-/**
- * Filter violations to save. For example, ignore a violation if it occurs on a line of code commented with //NOSONAR
- *
- * @since 1.12
- * @deprecated in 3.6. Replaced by {@link org.sonar.api.issue.IssueFilter}.
- */
-@DependedUpon(value = DecoratorBarriers.START_VIOLATIONS_GENERATION)
-@Deprecated
-public interface ViolationFilter extends BatchExtension {
-
- /**
- * Return true if the violation must be ignored, else it's saved into database.
- */
- boolean isIgnored(Violation violation);
-
-}