*/
package org.sonar.batch;
+import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.ResourceFilter;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.resources.Scopes;
/**
* @since 1.12
*/
public class ResourceFilters {
- private static final Logger LOG = LoggerFactory.getLogger(ResourceFilters.class);
-
- private ResourceFilter[] filters;
-
public ResourceFilters(ResourceFilter[] filters) {
- this.filters = filters == null ? new ResourceFilter[0] : filters;
+ this(LoggerFactory.getLogger(ResourceFilters.class), filters);
}
public ResourceFilters() {
- this(null);
+ // perfect
}
- public ResourceFilter[] getFilters() {
- return filters;
+ ResourceFilters(Logger logger, ResourceFilter[] filters) {
+ check(logger, filters);
}
- /**
- * Return true if the violation must be saved. If false then it is ignored.
- */
- public boolean isExcluded(Resource resource) {
- boolean ignored = false;
- if (Scopes.isHigherThanOrEquals(resource, Scopes.FILE)) {
- int index = 0;
- while (!ignored && index < filters.length) {
- ResourceFilter filter = filters[index];
- ignored = filter.isIgnored(resource);
- if (ignored && LOG.isDebugEnabled()) {
- LOG.debug("Resource {} is excluded by the filter {}", resource, filter);
- }
- index++;
- }
+ private void check(Logger logger, ResourceFilter[] filters) {
+ if (filters.length > 0) {
+ logger.warn("ResourceFilters are not supported since version 4.2: " + Joiner.on(", ").join(filters));
}
- return ignored;
}
-
}
return filter.filter(unfiltered);
}
- public boolean isExcluded() {
- return resource.isExcluded();
- }
-
@Override
public boolean equals(Object o) {
if (this == o) {
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.design.Dependency;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MeasuresFilter;
-import org.sonar.api.measures.MeasuresFilters;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
-import org.sonar.api.resources.Project;
-import org.sonar.api.resources.ProjectLink;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.resources.ResourceUtils;
-import org.sonar.api.resources.Scopes;
+import org.sonar.api.measures.*;
+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.api.violations.ViolationQuery;
import org.sonar.batch.ProjectTree;
-import org.sonar.batch.ResourceFilters;
import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.component.ScanGraph;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
public class DefaultIndex extends SonarIndex {
private MetricFinder metricFinder;
private final ScanGraph graph;
- // filters
- private ResourceFilters resourceFilters;
-
// caches
private Project currentProject;
private Map<Resource, Bucket> buckets = Maps.newHashMap();
return currentProject;
}
- public void setCurrentProject(Project project, ResourceFilters resourceFilters, ModuleIssues moduleIssues) {
+ public void setCurrentProject(Project project, ModuleIssues moduleIssues) {
this.currentProject = project;
// the following components depend on the current module, so they need to be reloaded.
- this.resourceFilters = resourceFilters;
this.moduleIssues = moduleIssues;
}
@Override
public Measure addMeasure(Resource resource, Measure measure) {
Bucket bucket = checkIndexed(resource);
- if (bucket != null && !bucket.isExcluded()) {
+ if (bucket != null) {
Metric metric = metricFinder.findByKey(measure.getMetricKey());
if (metric == null) {
throw new SonarException("Unknown metric: " + measure.getMetricKey());
Bucket fromBucket = doIndex(dependency.getFrom());
Bucket toBucket = doIndex(dependency.getTo());
- if (fromBucket != null && !fromBucket.isExcluded() && toBucket != null && !toBucket.isExcluded()) {
+ if (fromBucket != null && toBucket != null) {
dependencies.add(dependency);
registerOutgoingDependency(dependency);
registerIncomingDependency(dependency);
}
Bucket bucket = getBucket(resource, true);
- if (bucket == null || bucket.isExcluded()) {
+ if (bucket == null) {
LOG.warn("Resource is not indexed. Ignoring violation {}", violation);
return;
}
@Override
public void setSource(Resource reference, String source) {
Bucket bucket = checkIndexed(reference);
- if (bucket != null && !bucket.isExcluded()) {
+ if (bucket != null) {
persistence.setSource(reference, source);
}
}
return null;
}
- private boolean checkExclusion(Resource resource, Bucket parent) {
- boolean excluded = (parent != null && parent.isExcluded()) || (resourceFilters != null && resourceFilters.isExcluded(resource));
- resource.setExcluded(excluded);
- return excluded;
- }
-
@Override
public List<Resource> getChildren(Resource resource) {
return getChildren(resource, false);
Bucket bucket = getBucket(resource, acceptExcluded);
if (bucket != null) {
for (Bucket childBucket : bucket.getChildren()) {
- if (acceptExcluded || !childBucket.isExcluded()) {
- children.add(childBucket.getResource());
- }
+ children.add(childBucket.getResource());
}
}
return children;
@Override
public boolean index(Resource resource) {
Bucket bucket = doIndex(resource);
- return bucket != null && !bucket.isExcluded();
+ return bucket != null;
}
private Bucket doIndex(Resource resource) {
@Override
public boolean index(Resource resource, Resource parentReference) {
Bucket bucket = doIndex(resource, parentReference);
- return bucket != null && !bucket.isExcluded();
+ return bucket != null;
}
private Bucket doIndex(Resource resource, Resource parentReference) {
bucket = new Bucket(resource).setParent(parentBucket);
addBucket(resource, bucket);
- boolean excluded = checkExclusion(resource, parentBucket);
- if (!excluded) {
- Resource parentSnapshot = parentBucket != null ? parentBucket.getResource() : null;
- Snapshot snapshot = persistence.saveResource(currentProject, resource, parentSnapshot);
- if (ResourceUtils.isPersistable(resource) && !Qualifiers.LIBRARY.equals(resource.getQualifier())) {
- graph.addComponent(resource, snapshot);
- }
+ Resource parentSnapshot = parentBucket != null ? parentBucket.getResource() : null;
+ Snapshot snapshot = persistence.saveResource(currentProject, resource, parentSnapshot);
+ if (ResourceUtils.isPersistable(resource) && !Qualifiers.LIBRARY.equals(resource.getQualifier())) {
+ graph.addComponent(resource, snapshot);
}
return bucket;
@Override
public boolean isExcluded(Resource reference) {
- Bucket bucket = getBucket(reference, true);
- return bucket != null && bucket.isExcluded();
+ return false;
}
@Override
Bucket bucket = null;
if (resource != null) {
bucket = getBucket(resource);
- if (!acceptExcluded && bucket != null && bucket.isExcluded()) {
- bucket = null;
- }
}
return bucket;
}
protected void doAfterStart() {
DefaultIndex index = getComponentByType(DefaultIndex.class);
index.setCurrentProject(module,
- getComponentByType(ResourceFilters.class),
getComponentByType(ModuleIssues.class));
getComponentByType(PhaseExecutor.class).execute(module);
--- /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.batch;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.sonar.api.batch.ResourceFilter;
+
+import static org.mockito.Matchers.startsWith;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class ResourceFiltersTest {
+ @Test
+ public void warn_on_resource_filters() throws Exception {
+ Logger logger = mock(Logger.class);
+ ResourceFilter[] filters = {mock(ResourceFilter.class)};
+ new ResourceFilters(logger, filters);
+ verify(logger).warn(startsWith("ResourceFilters are not supported since version 4.2"));
+
+ // verify that the standard constructor does not fail
+ new ResourceFilters(filters);
+ }
+
+ @Test
+ public void ok_if_no_resource_filters() throws Exception {
+ // just for verify that it does not fail. Should check that no warning is logged.
+ new ResourceFilters();
+ }
+}
*/
package org.sonar.batch.index;
-import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.ResourceFilter;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilters;
import org.sonar.api.measures.MetricFinder;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.Library;
-import org.sonar.api.resources.Project;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.*;
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.ResourceFilters;
import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.core.component.ScanGraph;
@org.junit.Rule
public TemporaryFolder temp = new TemporaryFolder();
- private DefaultIndex index = null;
- private DeprecatedViolations deprecatedViolations;
- private Rule rule;
- private RuleFinder ruleFinder;
-
- private Project project;
-
- private Project moduleA;
-
- private Project moduleB;
-
- private Project moduleB1;
+ DefaultIndex index = null;
+ DeprecatedViolations deprecatedViolations;
+ Rule rule;
+ RuleFinder ruleFinder;
+ Project project;
+ Project moduleA;
+ Project moduleB;
+ Project moduleB1;
@Before
public void createIndex() throws IOException {
moduleB1 = new Project("moduleB1").setParent(moduleB);
when(projectTree.getProjectDefinition(moduleB1)).thenReturn(ProjectDefinition.create().setBaseDir(new java.io.File(baseDir, "moduleB/moduleB1")));
- ResourceFilter filter = new ResourceFilter() {
-
- public boolean isIgnored(Resource resource) {
- return StringUtils.containsIgnoreCase(resource.getKey(), "excluded");
- }
- };
RulesProfile rulesProfile = RulesProfile.create();
rule = Rule.create("repoKey", "ruleKey", "Rule");
rule.setId(1);
rulesProfile.activateRule(rule, null);
- index.setCurrentProject(project, new ResourceFilters(new ResourceFilter[] {filter}), mock(ModuleIssues.class));
+ index.setCurrentProject(project, mock(ModuleIssues.class));
index.doStart(project);
}
assertThat(index.getParent(fileRef)).isNull();
}
- @Test
- public void shouldBeExcluded() {
- File file = File.create("src/org/foo/ExcludedBar.java", "org/foo/ExcludedBar.java", null, false);
- assertThat(index.index(file)).isFalse();
- assertThat(index.isIndexed(file, true)).isTrue();
- assertThat(index.isIndexed(file, false)).isFalse();
- assertThat(index.isExcluded(file)).isTrue();
- }
-
@Test
public void shouldIndexResourceWhenAddingMeasure() {
Resource dir = Directory.create("src/org/foo", "org/foo");
--- /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.batch;
+
+import org.sonar.api.BatchExtension;
+import org.sonar.api.resources.Resource;
+
+/**
+ * Filter resources to save. For example, ignore a resource if its path matches an exclusion pattern (defined on the project).
+ * Filters are applied to files, directories and packages only.
+ *
+ * If the method start(), without parameters, exists, then it is executed at startup.
+ *
+ * @since 1.12
+ * @deprecated since 4.2. Analysis is file-system oriented. See {@link org.sonar.api.scan.filesystem.InputFileFilter}
+ */
+@Deprecated
+public interface ResourceFilter extends BatchExtension {
+
+ /**
+ * Return true if the resource must be ignored, else it's saved into database.
+ */
+ boolean isIgnored(Resource resource);
+
+}
+++ /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.batch;
-
-import org.sonar.api.BatchExtension;
-import org.sonar.api.resources.Resource;
-
-/**
- * Filter resources to save. For example, ignore a resource if its path matches an exclusion pattern (defined on the project).
- * Filters are applied to files, directories and packages only.
- *
- * If the method start(), without parameters, exists, then it is executed at startup.
- *
- * @since 1.12
- */
-public interface ResourceFilter extends BatchExtension {
-
- /**
- * Return true if the resource must be ignored, else it's saved into database.
- */
- boolean isIgnored(Resource resource);
-
-}
private String effectiveKey = null;
- private boolean isExcluded = false;
-
/**
* @return the resource key
*/
}
/**
- * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier.
+ * @deprecated since 2.6.
*/
@Deprecated
public final boolean isExcluded() {
- return isExcluded;
+ return false;
}
/**
*/
@Deprecated
public final Resource setExcluded(boolean b) {
- isExcluded = b;
return this;
}