import java.io.File;
import org.sonar.api.batch.Sensor;
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.issue.Issuable;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.Project;
import org.sonar.api.rule.RuleKey;
-import org.sonar.api.scan.filesystem.FileQuery;
-import org.sonar.api.scan.filesystem.ModuleFileSystem;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.xoo.Xoo;
public class DeprecatedResourceApiSensor implements Sensor {
public static final String RULE_KEY = "DeprecatedResourceApi";
- private final ModuleFileSystem fileSystem;
+ private final FileSystem fs;
private final ResourcePerspectives perspectives;
private final ActiveRules activeRules;
- public DeprecatedResourceApiSensor(ModuleFileSystem fileSystem, ResourcePerspectives perspectives, ActiveRules activeRules) {
- this.fileSystem = fileSystem;
+ public DeprecatedResourceApiSensor(FileSystem fileSystem, ResourcePerspectives perspectives, ActiveRules activeRules) {
+ this.fs = fileSystem;
this.perspectives = perspectives;
this.activeRules = activeRules;
}
@Override
public boolean shouldExecuteOnProject(Project project) {
- return !fileSystem.files(FileQuery.onMain().onLanguage(Xoo.KEY)).isEmpty() && activeRules.find(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY)) != null;
+ return fs.hasFiles(fs.predicates().and(fs.predicates().hasType(Type.MAIN), fs.predicates().hasLanguage(Xoo.KEY)))
+ && activeRules.find(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY)) != null;
}
@Override
public void analyse(Project module, org.sonar.api.batch.SensorContext context) {
- for (File f : fileSystem.files(FileQuery.onMain().onLanguage(Xoo.KEY))) {
- String relativePathFromSourceDir = new PathResolver().relativePath(fileSystem.baseDir(), f);
- org.sonar.api.resources.File sonarFile = org.sonar.api.resources.File.create(relativePathFromSourceDir);
+ for (File f : fs.files(fs.predicates().and(fs.predicates().hasType(Type.MAIN), fs.predicates().hasLanguage(Xoo.KEY)))) {
+ String relativePathFromBaseDir = new PathResolver().relativePath(fs.baseDir(), f);
+ org.sonar.api.resources.File sonarFile = org.sonar.api.resources.File.create(relativePathFromBaseDir);
Issuable issuable = perspectives.as(Issuable.class, sonarFile);
issuable.addIssue(issuable.newIssueBuilder()
.ruleKey(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY))
return workDir;
}
+ /**
+ * @deprecated since 6.1 notion of buildDir is not well defined
+ */
+ @Deprecated
public ProjectDefinition setBuildDir(File d) {
this.buildDir = d;
return this;
}
+ /**
+ * @deprecated since 6.1 notion of buildDir is not well defined
+ */
+ @Deprecated
public File getBuildDir() {
return buildDir;
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.scan.filesystem;
-
-import com.google.common.base.Function;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Sets;
-import org.apache.commons.lang.builder.EqualsBuilder;
-
-import javax.annotation.Nullable;
-
-import java.io.FileFilter;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * @since 3.5
- * @deprecated in 4.2. Replaced by {@link org.sonar.api.batch.fs.FileSystem} and
- * {@link org.sonar.api.batch.fs.FilePredicate}
- */
-@Deprecated
-public class FileQuery {
-
- private final ListMultimap<String, String> attributes = ArrayListMultimap.create();
- private final Set<String> inclusions = Sets.newHashSet();
- private final Set<String> exclusions = Sets.newHashSet();
-
- public static FileQuery on(FileType... types) {
- FileQuery query = new FileQuery();
- for (FileType type : types) {
- query.on("TYPE", type.typeValue());
- }
- return query;
- }
-
- public static FileQuery onSource() {
- return onMain();
- }
-
- /**
- * @since 4.2
- */
- public static FileQuery onMain() {
- FileQuery query = new FileQuery();
- return query.on("TYPE", "MAIN");
- }
-
- public static FileQuery onTest() {
- FileQuery query = new FileQuery();
- return query.on("TYPE", "TEST");
- }
-
- private FileQuery() {
- }
-
- public FileQuery on(String attribute, String... values) {
- for (String value : values) {
- attributes.put(attribute, value);
- }
- return this;
- }
-
- public Map<String, Collection<String>> attributes() {
- return attributes.asMap();
- }
-
- public Collection<FileType> types() {
- return Collections2.transform(attributes.get("TYPE"), new Function<String, FileType>() {
- @Override
- public FileType apply(@Nullable String input) {
- return input != null ? FileType.valueOf(input) : null;
- }
- });
- }
-
- public Collection<String> typeAttributes() {
- return attributes.get("TYPE");
- }
-
- public Collection<String> languages() {
- return attributes.get("LANG");
- }
-
- public FileQuery onLanguage(String... languages) {
- return on("LANG", languages);
- }
-
- public Collection<String> inclusions() {
- return inclusions;
- }
-
- public FileQuery withInclusions(String... inclusions) {
- this.inclusions.addAll(Arrays.asList(inclusions));
- return this;
- }
-
- public Collection<String> exclusions() {
- return exclusions;
- }
-
- public FileQuery withExclusions(String... exclusions) {
- this.exclusions.addAll(Arrays.asList(exclusions));
- return this;
- }
-
- public Collection<FileFilter> filters() {
- throw new UnsupportedOperationException("TODO");
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (obj.getClass() != getClass()) {
- return false;
- }
- FileQuery rhs = (FileQuery) obj;
- return new EqualsBuilder()
- .append(attributes, rhs.attributes)
- .append(exclusions, rhs.exclusions)
- .append(inclusions, rhs.inclusions)
- .isEquals();
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.scan.filesystem;
-
-import org.sonar.api.batch.ScannerSide;
-import org.sonar.api.ExtensionPoint;
-import org.sonar.api.batch.fs.InputFileFilter;
-
-import java.io.File;
-
-/**
- * Extension point to exclude some files from project scan. Some use-cases :
- * <ul>
- * <li>exclude the files that are older than x days</li>
- * <li>exclude the files which names start with Generated</li>
- * </ul>
- *
- * @since 3.5
- * @deprecated since 4.2 use {@link InputFileFilter}
- */
-@Deprecated
-@ScannerSide
-@ExtensionPoint
-public interface FileSystemFilter {
-
- /**
- * Plugins must not implement this interface. It is provided at runtime.
- */
- interface Context {
- ModuleFileSystem fileSystem();
-
- FileType type();
-
- /**
- * Changed in 5.1 as we don't keep track of relative path to source dir
- * File path relative to module base directory. Never return null.
- */
- String relativePath();
-
- /**
- * Absolute file path. Directory separator is slash, even on windows. Never return null.
- */
- String canonicalPath();
- }
-
- boolean accept(File file, Context context);
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.scan.filesystem;
-
-/**
- * @since 3.5
- * @deprecated in 4.2
- */
-@Deprecated
-public enum FileType {
- SOURCE("MAIN"), TEST("TEST"), MAIN("MAIN");
-
- private String typeValue;
-
- FileType(String typeValue) {
- this.typeValue = typeValue;
- }
-
- public String typeValue() {
- return typeValue;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.scan.filesystem;
-
-import java.io.File;
-import java.nio.charset.Charset;
-import java.util.List;
-import javax.annotation.CheckForNull;
-import org.sonar.api.batch.ScannerSide;
-import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * @since 3.5
- * @deprecated in 4.2. Replaced by {@link org.sonar.api.batch.fs.FileSystem}
- */
-@Deprecated
-@ScannerSide
-public interface ModuleFileSystem {
-
- /**
- * Base directory.
- */
- File baseDir();
-
- /**
- * Optional directory used by the build tool to generate various kinds of data (test reports, temp files, ...).
- * In Maven, it's given by the property ${project.build.directory}, which value is generally ${project.basedir}/target.
- */
- @CheckForNull
- File buildDir();
-
- /**
- * Source directories.
- * @deprecated since 4.2 use {@link FileSystem#files(org.sonar.api.batch.fs.FilePredicate)} to get all files with type {@link InputFile.Type#MAIN}.
- */
- List<File> sourceDirs();
-
- /**
- * Test directories. Non-existing directories are excluded.
- * Example in Maven : ${project.basedir}/src/test/java
- * @deprecated since 4.2 use {@link FileSystem#files(org.sonar.api.batch.fs.FilePredicate)} to get all files with type {@link InputFile.Type#TEST}.
- */
- List<File> testDirs();
-
- /**
- * Optional directories that contain the compiled sources, for example java bytecode.
- * Note that :
- * <ul>
- * <li>Maven projects have only a single binary directory, which is generally ${project.basedir}/target/classes</li>
- * <li>Binary directories can be empty</li>
- * <li>Test binary directories are not supported yet.</li>
- * </ul>
- * @deprecated since 4.2 sonar.binaries will be converted to java specific property
- */
- List<File> binaryDirs();
-
- /**
- * Search for files. Never return null.
- */
- List<File> files(FileQuery query);
-
- /**
- * Default charset for files of the module. If it's not defined, then
- * return the platform default charset.
- */
- Charset sourceCharset();
-
- /**
- * Working directory used by Sonar. This directory can be used for example to
- * store intermediary reports.
- */
- File workingDir();
-}
import org.sonar.scanner.rule.RulesProfileProvider;
import org.sonar.scanner.scan.filesystem.ComponentIndexer;
import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem;
-import org.sonar.scanner.scan.filesystem.DeprecatedFileFilters;
import org.sonar.scanner.scan.filesystem.ExclusionFilters;
import org.sonar.scanner.scan.filesystem.FileIndexer;
import org.sonar.scanner.scan.filesystem.FileSystemLogger;
ModuleInputFileCache.class,
FileExclusions.class,
ExclusionFilters.class,
- DeprecatedFileFilters.class,
InputFileBuilderFactory.class,
FileMetadata.class,
StatusDetectionFactory.class,
private static final String MODULE_KEY_PROPERTY = "sonar.moduleKey";
protected static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
+ /**
+ * @deprecated since 6.1 notion of buildDir is not well defined
+ */
+ @Deprecated
private static final String PROPERTY_PROJECT_BUILDDIR = "sonar.projectBuildDir";
private static final String PROPERTY_MODULES = "sonar.modules";
package org.sonar.scanner.scan.filesystem;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.File;
import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
import java.util.List;
-import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.InputFile.Status;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
-import org.sonar.api.scan.filesystem.FileQuery;
-import org.sonar.api.scan.filesystem.ModuleFileSystem;
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.analysis.DefaultAnalysisMode;
/**
* @since 3.5
*/
-public class DefaultModuleFileSystem extends DefaultFileSystem implements ModuleFileSystem {
+public class DefaultModuleFileSystem extends DefaultFileSystem {
private String moduleKey;
private FileIndexer indexer;
private Settings settings;
- private File buildDir;
private List<File> sourceDirsOrFiles = Lists.newArrayList();
private List<File> testDirsOrFiles = Lists.newArrayList();
private ComponentIndexer componentIndexer;
this.settings = settings;
this.indexer = indexer;
setWorkDir(initializer.workingDir());
- this.buildDir = initializer.buildDir();
this.sourceDirsOrFiles = initializer.sources();
this.testDirsOrFiles = initializer.tests();
return moduleKey;
}
- @Override
- @CheckForNull
- public File buildDir() {
- return buildDir;
- }
-
- @Override
- public List<File> sourceDirs() {
- return keepOnlyDirs(sourceDirsOrFiles);
- }
-
public List<File> sources() {
return sourceDirsOrFiles;
}
- @Override
- public List<File> testDirs() {
- return keepOnlyDirs(testDirsOrFiles);
- }
-
public List<File> tests() {
return testDirsOrFiles;
}
- private static List<File> keepOnlyDirs(List<File> dirsOrFiles) {
- List<File> result = new ArrayList<>();
- for (File f : dirsOrFiles) {
- if (f.isDirectory()) {
- result.add(f);
- }
- }
- return result;
- }
-
- @Override
- public List<File> binaryDirs() {
- return Collections.emptyList();
- }
-
@Override
public Charset encoding() {
final Charset charset;
return !settings.hasKey(CoreProperties.ENCODING_PROPERTY);
}
- /**
- * Should not be used - only for old plugins
- *
- * @deprecated since 4.0
- */
- @Deprecated
- void addSourceDir(File dir) {
- throw modificationNotPermitted();
- }
-
- /**
- * Should not be used - only for old plugins
- *
- * @deprecated since 4.0
- */
- @Deprecated
- void addTestDir(File dir) {
- throw modificationNotPermitted();
- }
-
private static UnsupportedOperationException modificationNotPermitted() {
return new UnsupportedOperationException("Modifications of the file system are not permitted");
}
- /**
- * @return
- * @deprecated in 4.2. Replaced by {@link #encoding()}
- */
- @Override
- @Deprecated
- public Charset sourceCharset() {
- return encoding();
- }
-
- /**
- * @deprecated in 4.2. Replaced by {@link #workDir()}
- */
- @Deprecated
- @Override
- public File workingDir() {
- return workDir();
- }
-
- @Override
- public List<File> files(FileQuery query) {
- doPreloadFiles();
- Collection<FilePredicate> predicates = Lists.newArrayList();
- for (Map.Entry<String, Collection<String>> entry : query.attributes().entrySet()) {
- predicates.add(fromDeprecatedAttribute(entry.getKey(), entry.getValue()));
- }
- return ImmutableList.copyOf(files(predicates().and(predicates)));
- }
-
@Override
protected void doPreloadFiles() {
if (!initialized) {
}
}
- private FilePredicate fromDeprecatedAttribute(String key, Collection<String> value) {
- if ("TYPE".equals(key)) {
- return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() {
- @Override
- public FilePredicate apply(@Nullable String s) {
- return s == null ? predicates().all() : predicates().hasType(org.sonar.api.batch.fs.InputFile.Type.valueOf(s));
- }
- }));
- }
- if ("STATUS".equals(key)) {
- return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() {
- @Override
- public FilePredicate apply(@Nullable String s) {
- return s == null ? predicates().all() : predicates().hasStatus(org.sonar.api.batch.fs.InputFile.Status.valueOf(s));
- }
- }));
- }
- if ("LANG".equals(key)) {
- return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() {
- @Override
- public FilePredicate apply(@Nullable String s) {
- return s == null ? predicates().all() : predicates().hasLanguage(s);
- }
- }));
- }
- if ("CMP_KEY".equals(key)) {
- return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() {
- @Override
- public FilePredicate apply(@Nullable String s) {
- return s == null ? predicates().all() : new AdditionalFilePredicates.KeyPredicate(s);
- }
- }));
- }
- throw new IllegalArgumentException("Unsupported file attribute: " + key);
- }
-
@Override
public boolean equals(Object o) {
if (this == o) {
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.scanner.scan.filesystem;
-
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.InputFileFilter;
-import org.sonar.api.scan.filesystem.FileSystemFilter;
-import org.sonar.api.scan.filesystem.FileType;
-import org.sonar.api.scan.filesystem.ModuleFileSystem;
-
-public class DeprecatedFileFilters implements InputFileFilter {
- private final FileSystemFilter[] filters;
-
- public DeprecatedFileFilters(FileSystemFilter[] filters) {
- this.filters = filters;
- }
-
- public DeprecatedFileFilters() {
- this(new FileSystemFilter[0]);
- }
-
- @Override
- public boolean accept(InputFile inputFile) {
- if (filters.length > 0) {
- DeprecatedContext context = new DeprecatedContext(inputFile);
- for (FileSystemFilter filter : filters) {
- if (!filter.accept(inputFile.file(), context)) {
- return false;
- }
- }
- }
- return true;
- }
-
- static class DeprecatedContext implements FileSystemFilter.Context {
- private final InputFile inputFile;
-
- DeprecatedContext(InputFile inputFile) {
- this.inputFile = inputFile;
- }
-
- @Override
- public ModuleFileSystem fileSystem() {
- throw new UnsupportedOperationException("Not supported since 4.0");
- }
-
- @Override
- public FileType type() {
- String type = inputFile.type().name();
- return FileType.valueOf(type);
- }
-
- @Override
- public String relativePath() {
- return inputFile.relativePath();
- }
-
- @Override
- public String canonicalPath() {
- return inputFile.absolutePath();
- }
- }
-}
package org.sonar.scanner.scan.filesystem;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.batch.ScannerSide;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.InputFile.Type;
-import org.sonar.api.batch.fs.InputFileFilter;
-import org.sonar.api.batch.fs.internal.DefaultInputDir;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.utils.MessageException;
-import org.sonar.scanner.util.ProgressReport;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystemLoopException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.ScannerSide;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.InputFile.Type;
+import org.sonar.api.batch.fs.InputFileFilter;
+import org.sonar.api.batch.fs.internal.DefaultInputDir;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.scan.filesystem.PathResolver;
+import org.sonar.api.utils.MessageException;
+import org.sonar.scanner.util.ProgressReport;
/**
* Index input files into {@link InputPathCache}.
public class FileIndexer {
private static final Logger LOG = LoggerFactory.getLogger(FileIndexer.class);
- private final List<InputFileFilter> filters;
+ private final InputFileFilter[] filters;
private final boolean isAggregator;
private final ExclusionFilters exclusionFilters;
private final InputFileBuilderFactory inputFileBuilderFactory;
private ExecutorService executorService;
private List<Future<Void>> tasks;
- public FileIndexer(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory,
- ProjectDefinition def) {
+ public FileIndexer(ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, ProjectDefinition def, InputFileFilter[] filters) {
this.filters = filters;
this.exclusionFilters = exclusionFilters;
this.inputFileBuilderFactory = inputFileBuilderFactory;
this.isAggregator = !def.getSubProjects().isEmpty();
}
+ public FileIndexer(ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, ProjectDefinition def) {
+ this(exclusionFilters, inputFileBuilderFactory, def, new InputFileFilter[0]);
+ }
+
void index(DefaultModuleFileSystem fileSystem) {
if (isAggregator) {
// No indexing for an aggregator module
package org.sonar.scanner.scan.filesystem;
import com.google.common.collect.Lists;
+import java.io.File;
+import java.util.List;
import org.apache.commons.io.FileUtils;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.TempFolder;
-import javax.annotation.CheckForNull;
-
-import java.io.File;
-import java.util.List;
-
/**
* @since 3.5
*/
private File baseDir;
private File workingDir;
- private File buildDir;
private List<File> sourceDirsOrFiles = Lists.newArrayList();
private List<File> testDirsOrFiles = Lists.newArrayList();
public ModuleFileSystemInitializer(ProjectDefinition module, TempFolder tempUtils, PathResolver pathResolver) {
baseDir = module.getBaseDir();
- buildDir = module.getBuildDir();
initWorkingDir(module, tempUtils);
initSources(module, pathResolver);
initTests(module, pathResolver);
return workingDir;
}
- @CheckForNull
- File buildDir() {
- return buildDir;
- }
-
List<File> sources() {
return sourceDirsOrFiles;
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.scanner.scan.filesystem;
-
-import com.google.common.collect.Lists;
-import java.io.File;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.util.Arrays;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-import org.mockito.Mockito;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.InputFile.Status;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Project;
-import org.sonar.scanner.analysis.DefaultAnalysisMode;
-import org.sonar.scanner.scan.filesystem.ComponentIndexer;
-import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem;
-import org.sonar.scanner.scan.filesystem.FileIndexer;
-import org.sonar.scanner.scan.filesystem.ModuleFileSystemInitializer;
-import org.sonar.scanner.scan.filesystem.ModuleInputFileCache;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
-
-public class DefaultModuleFileSystemTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- private Settings settings;
- private FileIndexer fileIndexer;
- private ModuleFileSystemInitializer initializer;
- private ComponentIndexer componentIndexer;
- private ModuleInputFileCache moduleInputFileCache;
- private DefaultAnalysisMode mode;
-
- @Before
- public void setUp() {
- settings = new Settings();
- fileIndexer = mock(FileIndexer.class);
- initializer = mock(ModuleFileSystemInitializer.class, Mockito.RETURNS_DEEP_STUBS);
- componentIndexer = mock(ComponentIndexer.class);
- moduleInputFileCache = mock(ModuleInputFileCache.class);
- mode = mock(DefaultAnalysisMode.class);
- }
-
- @Test
- public void test_equals_and_hashCode() throws Exception {
- DefaultModuleFileSystem foo1 = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
- DefaultModuleFileSystem foo2 = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
- DefaultModuleFileSystem bar = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("bar"), settings, fileIndexer, initializer, componentIndexer, mode);
- DefaultModuleFileSystem branch = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("bar", "branch", "My project"), settings, fileIndexer, initializer, componentIndexer, mode);
-
- assertThat(foo1.moduleKey()).isEqualTo("foo");
- assertThat(branch.moduleKey()).isEqualTo("bar:branch");
- assertThat(foo1.equals(foo1)).isTrue();
- assertThat(foo1.equals(foo2)).isTrue();
- assertThat(foo1.equals(bar)).isFalse();
- assertThat(foo1.equals("foo")).isFalse();
- assertThat(foo1.hashCode()).isEqualTo(foo1.hashCode());
- assertThat(foo1.hashCode()).isEqualTo(foo2.hashCode());
- }
-
- @Test
- public void default_source_encoding() {
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
-
- assertThat(fs.sourceCharset()).isEqualTo(Charset.defaultCharset());
- assertThat(fs.isDefaultJvmEncoding()).isTrue();
- }
-
- @Test
- public void source_encoding_is_set() {
- settings.setProperty(CoreProperties.ENCODING_PROPERTY, "Cp1124");
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
-
- assertThat(fs.encoding()).isEqualTo(Charset.forName("Cp1124"));
- assertThat(fs.sourceCharset()).isEqualTo(Charset.forName("Cp1124"));
-
- // This test fails when default Java encoding is "IBM AIX Ukraine". Sorry for that.
- assertThat(fs.isDefaultJvmEncoding()).isFalse();
- }
-
- @Test
- public void default_predicate_scan_only_changed() throws IOException {
- when(mode.scanAllFiles()).thenReturn(false);
-
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
-
- File baseDir = temp.newFile();
- InputFile mainInput = new DefaultInputFile("foo", "Main.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.MAIN);
- InputFile testInput = new DefaultInputFile("foo", "Test.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.TEST);
- InputFile mainSameInput = new DefaultInputFile("foo", "MainSame.java").setModuleBaseDir(baseDir.toPath())
- .setType(InputFile.Type.TEST).setStatus(Status.SAME);
- when(moduleInputFileCache.inputFiles()).thenReturn(Lists.newArrayList(mainInput, testInput, mainSameInput));
-
- fs.index();
- Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().all());
- assertThat(inputFiles).containsOnly(mainInput, testInput);
-
- Iterable<InputFile> allInputFiles = fs.inputFiles();
- assertThat(allInputFiles).containsOnly(mainInput, mainSameInput, testInput);
- }
-
- @Test
- public void test_dirs() throws IOException {
- File basedir = temp.newFolder("base");
- File buildDir = temp.newFolder("build");
- File workingDir = temp.newFolder("work");
- File additionalFile = temp.newFile("Main.java");
- File additionalTest = temp.newFile("Test.java");
- when(initializer.baseDir()).thenReturn(basedir);
- when(initializer.buildDir()).thenReturn(buildDir);
- when(initializer.workingDir()).thenReturn(workingDir);
- File javaSrc = new File(basedir, "src/main/java");
- javaSrc.mkdirs();
- File groovySrc = new File(basedir, "src/main/groovy");
- groovySrc.mkdirs();
- when(initializer.sources()).thenReturn(Arrays.asList(javaSrc, groovySrc, additionalFile));
- File javaTest = new File(basedir, "src/test/java");
- javaTest.mkdirs();
- when(initializer.tests()).thenReturn(Arrays.asList(javaTest, additionalTest));
-
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
-
- assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath());
- assertThat(fs.workDir().getCanonicalPath()).isEqualTo(workingDir.getCanonicalPath());
- assertThat(fs.buildDir().getCanonicalPath()).isEqualTo(buildDir.getCanonicalPath());
- assertThat(fs.sourceDirs()).hasSize(2);
- assertThat(fs.testDirs()).hasSize(1);
- }
-
- @Test
- public void should_search_input_files() throws Exception {
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
-
- File baseDir = temp.newFile();
- InputFile mainInput = new DefaultInputFile("foo", "Main.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.MAIN);
- InputFile testInput = new DefaultInputFile("foo", "Test.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.TEST);
- when(moduleInputFileCache.inputFiles()).thenReturn(Lists.newArrayList(mainInput, testInput));
-
- fs.index();
- Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().hasType(InputFile.Type.MAIN));
- assertThat(inputFiles).containsOnly(mainInput);
-
- Iterable<File> files = fs.files(fs.predicates().hasType(InputFile.Type.MAIN));
- assertThat(files).containsOnly(new File(baseDir, "Main.java"));
- }
-
- @Test
- public void should_index() {
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache,
- new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode);
-
- verifyZeroInteractions(fileIndexer);
-
- fs.index();
- verify(fileIndexer).index(fs);
- verify(componentIndexer).execute(fs);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.scanner.scan.filesystem;
-
-import org.apache.commons.io.FilenameUtils;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.mockito.ArgumentCaptor;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.scan.filesystem.FileSystemFilter;
-import org.sonar.api.scan.filesystem.FileType;
-import org.sonar.scanner.scan.filesystem.DeprecatedFileFilters;
-import java.io.File;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-public class DeprecatedFileFiltersTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- FileSystemFilter filter = mock(FileSystemFilter.class);
-
- @Test
- public void no_filters() {
- DeprecatedFileFilters filters = new DeprecatedFileFilters();
-
- InputFile inputFile = new DefaultInputFile("foo", "src/main/java/Foo.java");
- assertThat(filters.accept(inputFile)).isTrue();
- }
-
- @Test
- public void at_least_one_filter() throws Exception {
- DeprecatedFileFilters filters = new DeprecatedFileFilters(new FileSystemFilter[] {filter});
-
- File basedir = temp.newFolder();
- File file = new File(basedir, "src/main/java/Foo.java");
- InputFile inputFile = new DefaultInputFile("foo", "src/main/java/Foo.java")
- .setModuleBaseDir(basedir.toPath())
- .setType(InputFile.Type.MAIN);
- when(filter.accept(eq(file), any(DeprecatedFileFilters.DeprecatedContext.class))).thenReturn(false);
-
- assertThat(filters.accept(inputFile)).isFalse();
-
- ArgumentCaptor<DeprecatedFileFilters.DeprecatedContext> argument = ArgumentCaptor.forClass(DeprecatedFileFilters.DeprecatedContext.class);
- verify(filter).accept(eq(file), argument.capture());
-
- DeprecatedFileFilters.DeprecatedContext context = argument.getValue();
- assertThat(context.canonicalPath()).isEqualTo(FilenameUtils.separatorsToUnix(file.getAbsolutePath()));
- assertThat(context.relativePath()).isEqualTo("src/main/java/Foo.java");
- assertThat(context.type()).isEqualTo(FileType.MAIN);
- }
-}
*/
package org.sonar.scanner.scan.filesystem;
+import java.io.File;
+import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.junit.Rule;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.TempFolder;
-import org.sonar.scanner.scan.filesystem.ModuleFileSystemInitializer;
-import java.io.File;
-import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@Test
public void should_init_directories() throws IOException {
File baseDir = temp.newFolder("base");
- File buildDir = temp.newFolder("build");
File sourceDir = new File(baseDir, "src/main/java");
FileUtils.forceMkdir(sourceDir);
File testDir = new File(baseDir, "src/test/java");
ProjectDefinition project = ProjectDefinition.create()
.setBaseDir(baseDir)
- .setBuildDir(buildDir)
.addSources("src/main/java", "src/main/unknown")
.addTests("src/test/java", "src/test/unknown");
ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(project, mock(TempFolder.class), pathResolver);
assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath());
- assertThat(initializer.buildDir().getCanonicalPath()).isEqualTo(buildDir.getCanonicalPath());
assertThat(initializer.sources()).hasSize(1);
assertThat(path(initializer.sources().get(0))).endsWith("src/main/java");
assertThat(initializer.tests()).hasSize(1);