import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
@Test
public void testNoExecutionIfNoCoverageFile() {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").build();
context.fileSystem().add(inputFile);
sensor.execute(context);
}
public void testLineHitNoConditions() throws IOException {
File coverage = new File(baseDir, "src/foo.xoo.itcoverage");
FileUtils.write(coverage, "1:3\n\n#comment");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo").setLines(10);
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setModuleBaseDir(baseDir.toPath()).setLanguage("xoo").setLines(10).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
public void testLineHitAndConditions() throws IOException {
File coverage = new File(baseDir, "src/foo.xoo.itcoverage");
FileUtils.write(coverage, "1:3:4:2");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo").setLines(10);
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setModuleBaseDir(baseDir.toPath()).setLanguage("xoo").setLines(10).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
@Test
public void testNoExecutionIfNoCoverageFile() {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
}
public void testLineHitNoConditions() throws IOException {
File coverage = new File(baseDir, "src/foo.xoo.overallcoverage");
FileUtils.write(coverage, "1:3\n\n#comment");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo").setLines(10);
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).setLines(10).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
public void testLineHitAndConditions() throws IOException {
File coverage = new File(baseDir, "src/foo.xoo.overallcoverage");
FileUtils.write(coverage, "1:3:4:2");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo").setLines(10);
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).setLines(10).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
@Test
public void testNoExecutionIfNoCoverageFile() {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
}
public void testLineHitNoConditions() throws IOException {
File coverage = new File(baseDir, "src/foo.xoo.coverage");
FileUtils.write(coverage, "1:3\n\n#comment");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo").setLines(10);
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).setLines(10).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
public void testLineHitAndConditions() throws IOException {
File coverage = new File(baseDir, "src/foo.xoo.coverage");
FileUtils.write(coverage, "1:3:4:2");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo").setLines(10);
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).setLines(10).build();
context.fileSystem().add(inputFile);
sensor.execute(context);
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
private void createSourceFile(String content) throws IOException {
File sourceFile = new File(baseDir, "src/foo.xoo");
FileUtils.write(sourceFile, content);
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo")
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
.setLanguage("xoo")
- .initMetadata(content);
+ .initMetadata(content)
+ .setModuleBaseDir(baseDir.toPath())
+ .build();
context.fileSystem().add(inputFile);
}
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
@Test
public void testNoExecutionIfNoMeasureFile() {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").build();
context.fileSystem().add(inputFile);
sensor.execute(context);
}
public void testExecution() throws IOException {
File measures = new File(baseDir, "src/foo.xoo.measures");
FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\n\n#comment");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
context.fileSystem().add(inputFile);
Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL)
public void failIfMetricNotFound() throws IOException {
File measures = new File(baseDir, "src/foo.xoo.measures");
FileUtils.write(measures, "unknow:12\n\n#comment");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
context.fileSystem().add(inputFile);
thrown.expect(IllegalStateException.class);
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.component.ResourcePerspectives;
@Test
public void testNoExecutionIfNoSymbolFile() {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
fileSystem.add(inputFile);
sensor.execute(context);
}
public void testExecution() throws IOException {
File symbol = new File(baseDir, "src/foo.xoo.symbol");
FileUtils.write(symbol, "1:4,7\n12:15,23:33\n\n#comment");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
fileSystem.add(inputFile);
Symbolizable symbolizable = mock(Symbolizable.class);
when(perspectives.as(Symbolizable.class, inputFile)).thenReturn(symbolizable);
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.component.ResourcePerspectives;
@Test
public void testNoExecutionIfNoSyntaxFile() {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
+ .setLanguage("xoo")
+ .setModuleBaseDir(baseDir.toPath())
+ .build();
context.fileSystem().add(inputFile);
sensor.execute(context);
}
public void testExecution() throws IOException {
File symbol = new File(baseDir, "src/foo.xoo.highlighting");
FileUtils.write(symbol, "1:4:k\n12:15:cppd\n\n#comment");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo")
- .initMetadata(" xoo\nazertyazer\nfoo");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
+ .setLanguage("xoo")
+ .setModuleBaseDir(baseDir.toPath())
+ .initMetadata(" xoo\nazertyazer\nfoo")
+ .build();
context.fileSystem().add(inputFile);
Highlightable highlightable = mock(Highlightable.class);
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.config.Settings;
public void testExecution() throws IOException {
File source = new File(baseDir, "src/foo.xoo");
FileUtils.write(source, "token1 token2 token3\ntoken4");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
+ .setLanguage("xoo")
+ .setModuleBaseDir(baseDir.toPath())
+ .build();
fileSystem.add(inputFile);
XooTokenizer tokenizer = new XooTokenizer(fileSystem);
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultTextPointer;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.error.AnalysisError;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
createErrorFile(baseDir);
int[] offsets = {10, 20, 30, 40};
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo")
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
.setLanguage("xoo")
.setOriginalLineOffsets(offsets)
- .setLines(4);
+ .setModuleBaseDir(baseDir)
+ .setLines(4)
+ .build();
context = SensorContextTester.create(baseDir);
context.fileSystem().add(inputFile);
import org.junit.rules.TemporaryFolder;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
@Test
public void testRule() throws IOException {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
+ .setLanguage(Xoo.KEY)
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
+ .build();
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
@Test
public void testForceSeverity() throws IOException {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
+ .setLanguage(Xoo.KEY)
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
+ .build();
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
@Test
public void testProvideGap() throws IOException {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
+ .setLanguage(Xoo.KEY)
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
+ .build();
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
@Test
public void testProvideGap_before_5_5() throws IOException {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY)
- .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo")
+ .setLanguage(Xoo.KEY)
+ .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n")
+ .build();
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.scm.BlameCommand.BlameInput;
import org.sonar.api.batch.scm.BlameCommand.BlameOutput;
import org.sonar.api.batch.scm.BlameLine;
FileUtils.write(source, "sample content");
File scm = new File(baseDir, "src/foo.xoo.scm");
FileUtils.write(scm, "123,julien,2014-12-12\n234,julien,2014-12-24");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage(Xoo.KEY);
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo")
+ .setLanguage(Xoo.KEY)
+ .setModuleBaseDir(baseDir.toPath())
+ .build();
fs.add(inputFile);
BlameOutput result = mock(BlameOutput.class);
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.Project;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class ComponentKeysTest {
@Rule
Directory dir = Directory.create("src/org/foo");
assertThat(ComponentKeys.createEffectiveKey(project, dir)).isEqualTo("my_project:src/org/foo");
- InputFile file = new DefaultInputFile("foo", "foo/Bar.php");
+ InputFile file = mock(InputFile.class);
+ when(file.relativePath()).thenReturn("foo/Bar.php");
assertThat(ComponentKeys.createEffectiveKey("my_project", file)).isEqualTo("my_project:foo/Bar.php");
}
/**
* Test if provided file is valid for this predicate
*/
- boolean apply(InputFile inputFile);
+ boolean apply(IndexedFile inputFile);
}
FilePredicate hasLanguages(String... languages);
- FilePredicate hasStatus(InputFile.Status status);
-
FilePredicate hasType(InputFile.Type type);
FilePredicate not(FilePredicate p);
--- /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.batch.fs;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import javax.annotation.CheckForNull;
+
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.InputPath;
+
+/**
+ * @since 6.3
+ */
+public interface IndexedFile extends InputPath {
+ /**
+ * Path relative to module base directory. Path is unique and identifies file
+ * within given <code>{@link FileSystem}</code>. File separator is the forward
+ * slash ('/'), even on Microsoft Windows.
+ * <br>
+ * Returns <code>src/main/java/com/Foo.java</code> if module base dir is
+ * <code>/path/to/module</code> and if file is
+ * <code>/path/to/module/src/main/java/com/Foo.java</code>.
+ * <br>
+ * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo').
+ */
+ @Override
+ String relativePath();
+
+ /**
+ * Normalized absolute path. File separator is forward slash ('/'), even on Microsoft Windows.
+ * <br>
+ * This is not canonical path. Symbolic links are not resolved. For example if /project/src links
+ * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use
+ * {@code file().getCanonicalPath()} to resolve symbolic link.
+ */
+ @Override
+ String absolutePath();
+
+ /**
+ * The underlying absolute {@link java.io.File}. It should not be used to read the file in the filesystem.
+ * @see #contents()
+ * @see #inputStream()
+ */
+ @Override
+ File file();
+
+ /**
+ * The underlying absolute {@link Path}.
+ * It should not be used to read the file in the filesystem.
+ * @see #contents()
+ * @see #inputStream()
+ * @since 5.1
+ */
+ @Override
+ Path path();
+
+ /**
+ * Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file.
+ */
+ @CheckForNull
+ String language();
+
+ /**
+ * Does it contain main or test code ?
+ */
+ InputFile.Type type();
+
+ /**
+ * Creates a stream of the file's contents. Depending on the runtime context, the source might be a file in a physical or virtual filesystem.
+ * Typically, it won't be buffered. <b>The stream must be closed by the caller</b>.
+ * Note that there is a default implementation.
+ * @since 6.2
+ */
+ default InputStream inputStream() throws IOException {
+ return Files.newInputStream(path());
+ }
+}
*
* @since 4.2
*/
-public interface InputFile extends InputPath {
+public interface InputFile extends IndexedFile, InputPath {
enum Type {
MAIN, TEST
* Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file.
*/
@CheckForNull
+ @Override
String language();
/**
* Does it contain main or test code ?
*/
+ @Override
Type type();
/**
* Note that there is a default implementation.
* @since 6.2
*/
+ @Override
default InputStream inputStream() throws IOException {
return Files.newInputStream(path());
}
*/
@ScannerSide
@ExtensionPoint
+@FunctionalInterface
public interface InputFileFilter {
boolean accept(InputFile f);
package org.sonar.api.batch.fs.internal;
import org.sonar.api.batch.fs.FileSystem.Index;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.PathUtils;
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
return path.equals(f.absolutePath());
}
import com.google.common.annotations.VisibleForTesting;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem.Index;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
import java.util.ArrayList;
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
for (OptimizedFilePredicate predicate : predicates) {
if (!predicate.apply(f)) {
return false;
return or(list);
}
- @Override
- public FilePredicate hasStatus(InputFile.Status status) {
- return new StatusPredicate(status);
- }
-
@Override
public FilePredicate hasType(InputFile.Type type) {
return new TypePredicate(type);
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
+import java.util.stream.StreamSupport;
+
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FilePredicates;
private Path workDir;
private Charset encoding;
protected final FilePredicates predicates;
- private FilePredicate defaultPredicate;
+ private InputFilePredicate defaultPredicate;
/**
* Only for testing
return this;
}
- public DefaultFileSystem setDefaultPredicate(@Nullable FilePredicate predicate) {
+ public DefaultFileSystem setDefaultPredicate(@Nullable InputFilePredicate predicate) {
this.defaultPredicate = predicate;
return this;
}
@Override
public Iterable<InputFile> inputFiles(FilePredicate predicate) {
doPreloadFiles();
- FilePredicate combinedPredicate = predicate;
+ Iterable<InputFile> iterable = OptimizedFilePredicateAdapter.create(predicate).get(cache);
if (defaultPredicate != null) {
- combinedPredicate = predicates().and(defaultPredicate, predicate);
+ return StreamSupport.stream(iterable.spliterator(), false)
+ .filter(defaultPredicate::apply)::iterator;
}
- return OptimizedFilePredicateAdapter.create(combinedPredicate).get(cache);
+ return iterable;
}
@Override
return cache.inputDir(relativePath);
}
- /**
- * Adds InputFile to the list and registers its language, if present.
- * Synchronized because PersistIt Exchange is not concurrent
- */
- public synchronized DefaultFileSystem add(DefaultInputFile inputFile) {
- if (this.baseDir == null) {
- throw new IllegalStateException("Please set basedir on filesystem before adding files");
- }
- inputFile.setModuleBaseDir(this.baseDir);
+ public DefaultFileSystem add(InputFile inputFile) {
cache.add(inputFile);
String language = inputFile.language();
if (language != null) {
return this;
}
- /**
- * Adds InputDir to the list.
- * Synchronized because PersistIt Exchange is not concurrent
- */
- public synchronized DefaultFileSystem add(DefaultInputDir inputDir) {
- if (this.baseDir == null) {
- throw new IllegalStateException("Please set basedir on filesystem before adding dirs");
- }
- inputDir.setModuleBaseDir(this.baseDir);
+ public DefaultFileSystem add(DefaultInputDir inputDir) {
cache.add(inputDir);
return this;
}
/**
* Adds a language to the list. To be used only for unit tests that need to use {@link #languages()} without
- * using {@link #add(DefaultInputFile)}.
+ * using {@link #add(InputFile)}.
*/
public DefaultFileSystem addLanguages(String language, String... others) {
languages.add(language);
--- /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.batch.fs.internal;
+
+import java.io.File;
+import java.nio.file.Path;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+import org.sonar.api.batch.fs.IndexedFile;
+import org.sonar.api.batch.fs.InputFile.Type;
+import org.sonar.api.utils.PathUtils;
+
+public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile {
+ private final String relativePath;
+ private final String moduleKey;
+ private final Path moduleBaseDir;
+ private String language;
+ private final Type type;
+
+ public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath) {
+ this(moduleKey, moduleBaseDir, relativePath, Type.MAIN);
+ }
+
+ public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type) {
+ this.moduleKey = moduleKey;
+ this.relativePath = PathUtils.sanitize(relativePath);
+ this.moduleBaseDir = moduleBaseDir.normalize();
+ this.type = type;
+ }
+
+ public DefaultIndexedFile setLanguage(@Nullable String language) {
+ this.language = language;
+ return this;
+ }
+
+ @Override
+ public String relativePath() {
+ return relativePath;
+ }
+
+ @Override
+ public String absolutePath() {
+ return PathUtils.sanitize(path().toString());
+ }
+
+ @Override
+ public File file() {
+ return path().toFile();
+ }
+
+ @Override
+ public Path path() {
+ return moduleBaseDir.resolve(relativePath);
+ }
+
+ @CheckForNull
+ @Override
+ public String language() {
+ return language;
+ }
+
+ @Override
+ public Type type() {
+ return type;
+ }
+
+ /**
+ * Component key.
+ */
+ @Override
+ public String key() {
+ return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString();
+ }
+
+ public String moduleKey() {
+ return moduleKey;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+
+ if (!(o instanceof DefaultIndexedFile)) {
+ return false;
+ }
+
+ DefaultIndexedFile that = (DefaultIndexedFile) o;
+ return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath);
+ }
+
+ @Override
+ public int hashCode() {
+ return moduleKey.hashCode() + relativePath.hashCode() * 13;
+ }
+
+ @Override
+ public String toString() {
+ return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
+ }
+
+ @Override
+ public boolean isFile() {
+ return true;
+ }
+}
import com.google.common.base.Preconditions;
import java.io.File;
-import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Arrays;
+import java.util.function.Function;
+
import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextPointer;
import org.sonar.api.batch.fs.TextRange;
-import org.sonar.api.batch.fs.internal.FileMetadata.Metadata;
-import org.sonar.api.utils.PathUtils;
/**
* @since 4.2
*/
public class DefaultInputFile extends DefaultInputComponent implements InputFile {
-
- private final String relativePath;
- private final String moduleKey;
- private Path moduleBaseDir;
- private String language;
- private Type type = Type.MAIN;
+ private final DefaultIndexedFile indexedFile;
+ private final Function<DefaultInputFile, Metadata> metadataGenerator;
private Status status;
- private int lines = -1;
private Charset charset;
- private int lastValidOffset = -1;
- private String hash;
- private int nonBlankLines;
- private int[] originalLineOffsets;
+ private Metadata metadata;
- public DefaultInputFile(String moduleKey, String relativePath) {
- this.moduleKey = moduleKey;
- this.relativePath = PathUtils.sanitize(relativePath);
+ public DefaultInputFile(DefaultIndexedFile indexedFile, Function<DefaultInputFile, Metadata> metadataGenerator) {
+ this.indexedFile = indexedFile;
+ this.metadataGenerator = metadataGenerator;
+ this.metadata = null;
+ }
+
+ private void checkMetadata() {
+ if (metadata == null) {
+ setMetadata(metadataGenerator.apply(this));
+ }
}
@Override
public String relativePath() {
- return relativePath;
+ return indexedFile.relativePath();
}
@Override
public String absolutePath() {
- return PathUtils.sanitize(path().toString());
+ return indexedFile.absolutePath();
}
@Override
public File file() {
- return path().toFile();
+ return indexedFile.file();
}
@Override
public Path path() {
- if (moduleBaseDir == null) {
- throw new IllegalStateException("Can not return the java.nio.file.Path because module baseDir is not set (see method setModuleBaseDir(java.io.File))");
- }
- return moduleBaseDir.resolve(relativePath);
+ return indexedFile.path();
}
@CheckForNull
@Override
public String language() {
- return language;
+ return indexedFile.language();
}
@Override
public Type type() {
- return type;
- }
-
- /**
- * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)}
- */
- @Override
- public Status status() {
- return status;
- }
-
- @Override
- public int lines() {
- return lines;
- }
-
- @Override
- public boolean isEmpty() {
- return lastValidOffset == 0;
+ return indexedFile.type();
}
/**
*/
@Override
public String key() {
- return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString();
+ return indexedFile.key();
}
public String moduleKey() {
- return moduleKey;
+ return indexedFile.moduleKey();
}
@Override
- public Charset charset() {
- return charset;
- }
-
- /**
- * For testing purpose. Will be automaticall set when file is added to {@link DefaultFileSystem}
- */
- public DefaultInputFile setModuleBaseDir(Path moduleBaseDir) {
- this.moduleBaseDir = moduleBaseDir.normalize();
- return this;
+ public int hashCode() {
+ return indexedFile.hashCode();
}
- public DefaultInputFile setLanguage(@Nullable String language) {
- this.language = language;
- return this;
+ @Override
+ public String toString() {
+ return indexedFile.toString();
}
- public DefaultInputFile setType(Type type) {
- this.type = type;
- return this;
+ /**
+ * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)}
+ */
+ @Override
+ public Status status() {
+ checkMetadata();
+ return status;
}
- public DefaultInputFile setStatus(Status status) {
- this.status = status;
- return this;
+ @Override
+ public int lines() {
+ checkMetadata();
+ return metadata.lines();
}
- public DefaultInputFile setLines(int lines) {
- this.lines = lines;
- return this;
+ @Override
+ public boolean isEmpty() {
+ checkMetadata();
+ return metadata.lastValidOffset() == 0;
}
- public DefaultInputFile setCharset(Charset charset) {
- this.charset = charset;
- return this;
+ @Override
+ public Charset charset() {
+ checkMetadata();
+ return charset;
}
public int lastValidOffset() {
- Preconditions.checkState(lastValidOffset >= 0, "InputFile is not properly initialized. Please set 'lastValidOffset' property.");
- return lastValidOffset;
- }
-
- public DefaultInputFile setLastValidOffset(int lastValidOffset) {
- this.lastValidOffset = lastValidOffset;
- return this;
+ checkMetadata();
+ Preconditions.checkState(metadata.lastValidOffset() >= 0, "InputFile is not properly initialized.");
+ return metadata.lastValidOffset();
}
/**
* Digest hash of the file.
*/
public String hash() {
- return hash;
+ checkMetadata();
+ return metadata.hash();
}
public int nonBlankLines() {
- return nonBlankLines;
+ checkMetadata();
+ return metadata.nonBlankLines();
}
public int[] originalLineOffsets() {
- Preconditions.checkState(originalLineOffsets != null, "InputFile is not properly initialized. Please set 'originalLineOffsets' property.");
- Preconditions.checkState(originalLineOffsets.length == lines, "InputFile is not properly initialized. 'originalLineOffsets' property length should be equal to 'lines'");
- return originalLineOffsets;
- }
-
- public DefaultInputFile setHash(String hash) {
- this.hash = hash;
- return this;
- }
-
- public DefaultInputFile setNonBlankLines(int nonBlankLines) {
- this.nonBlankLines = nonBlankLines;
- return this;
- }
-
- public DefaultInputFile setOriginalLineOffsets(int[] originalLineOffsets) {
- this.originalLineOffsets = originalLineOffsets;
- return this;
+ checkMetadata();
+ Preconditions.checkState(metadata.originalLineOffsets() != null, "InputFile is not properly initialized.");
+ Preconditions.checkState(metadata.originalLineOffsets().length == metadata.lines(),
+ "InputFile is not properly initialized. 'originalLineOffsets' property length should be equal to 'lines'");
+ return metadata.originalLineOffsets();
}
@Override
public TextPointer newPointer(int line, int lineOffset) {
+ checkMetadata();
DefaultTextPointer textPointer = new DefaultTextPointer(line, lineOffset);
checkValid(textPointer, "pointer");
return textPointer;
}
- private void checkValid(TextPointer pointer, String owner) {
- Preconditions.checkArgument(pointer.line() >= 1, "%s is not a valid line for a file", pointer.line());
- Preconditions.checkArgument(pointer.line() <= this.lines, "%s is not a valid line for %s. File %s has %s line(s)", pointer.line(), owner, this, lines);
- Preconditions.checkArgument(pointer.lineOffset() >= 0, "%s is not a valid line offset for a file", pointer.lineOffset());
- int lineLength = lineLength(pointer.line());
- Preconditions.checkArgument(pointer.lineOffset() <= lineLength,
- "%s is not a valid line offset for %s. File %s has %s character(s) at line %s", pointer.lineOffset(), owner, this, lineLength, pointer.line());
- }
-
- private int lineLength(int line) {
- return lastValidGlobalOffsetForLine(line) - originalLineOffsets()[line - 1];
- }
-
- private int lastValidGlobalOffsetForLine(int line) {
- return line < this.lines ? (originalLineOffsets()[line] - 1) : lastValidOffset();
- }
-
@Override
public TextRange newRange(TextPointer start, TextPointer end) {
+ checkMetadata();
checkValid(start, "start pointer");
checkValid(end, "end pointer");
return newRangeValidPointers(start, end, false);
@Override
public TextRange newRange(int startLine, int startLineOffset, int endLine, int endLineOffset) {
+ checkMetadata();
TextPointer start = newPointer(startLine, startLineOffset);
TextPointer end = newPointer(endLine, endLineOffset);
return newRangeValidPointers(start, end, false);
@Override
public TextRange selectLine(int line) {
+ checkMetadata();
TextPointer startPointer = newPointer(line, 0);
TextPointer endPointer = newPointer(line, lineLength(line));
return newRangeValidPointers(startPointer, endPointer, true);
}
public void validate(TextRange range) {
+ checkMetadata();
checkValid(range.start(), "start pointer");
checkValid(range.end(), "end pointer");
}
- private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) {
- Preconditions.checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0),
- "Start pointer %s should be before end pointer %s", start, end);
- return new DefaultTextRange(start, end);
- }
-
/**
* Create Range from global offsets. Used for backward compatibility with older API.
*/
public TextRange newRange(int startOffset, int endOffset) {
+ checkMetadata();
return newRangeValidPointers(newPointer(startOffset), newPointer(endOffset), false);
}
public TextPointer newPointer(int globalOffset) {
+ checkMetadata();
Preconditions.checkArgument(globalOffset >= 0, "%s is not a valid offset for a file", globalOffset);
Preconditions.checkArgument(globalOffset <= lastValidOffset(), "%s is not a valid offset for file %s. Max offset is %s", globalOffset, this, lastValidOffset());
int line = findLine(globalOffset);
return new DefaultTextPointer(line, globalOffset - startLineOffset);
}
- private int findLine(int globalOffset) {
- return Math.abs(Arrays.binarySearch(originalLineOffsets(), globalOffset) + 1);
+ public DefaultInputFile setStatus(Status status) {
+ this.status = status;
+ return this;
}
- public DefaultInputFile initMetadata(Metadata metadata) {
- this.setLines(metadata.lines);
- this.setLastValidOffset(metadata.lastValidOffset);
- this.setNonBlankLines(metadata.nonBlankLines);
- this.setHash(metadata.hash);
- this.setOriginalLineOffsets(metadata.originalLineOffsets);
+ public DefaultInputFile setCharset(Charset charset) {
+ this.charset = charset;
return this;
}
- /**
- * For testing purpose
- */
- public DefaultInputFile initMetadata(String content) {
- return initMetadata(new FileMetadata().readMetadata(new StringReader(content)));
+ private void checkValid(TextPointer pointer, String owner) {
+ Preconditions.checkArgument(pointer.line() >= 1, "%s is not a valid line for a file", pointer.line());
+ Preconditions.checkArgument(pointer.line() <= this.metadata.lines(), "%s is not a valid line for %s. File %s has %s line(s)", pointer.line(), owner, this, metadata.lines());
+ Preconditions.checkArgument(pointer.lineOffset() >= 0, "%s is not a valid line offset for a file", pointer.lineOffset());
+ int lineLength = lineLength(pointer.line());
+ Preconditions.checkArgument(pointer.lineOffset() <= lineLength,
+ "%s is not a valid line offset for %s. File %s has %s character(s) at line %s", pointer.lineOffset(), owner, this, lineLength, pointer.line());
+ }
+
+ private int lineLength(int line) {
+ return lastValidGlobalOffsetForLine(line) - originalLineOffsets()[line - 1];
+ }
+
+ private int lastValidGlobalOffsetForLine(int line) {
+ return line < this.metadata.lines() ? (originalLineOffsets()[line] - 1) : lastValidOffset();
+ }
+
+ private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) {
+ Preconditions.checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0),
+ "Start pointer %s should be before end pointer %s", start, end);
+ return new DefaultTextRange(start, end);
+ }
+
+ private int findLine(int globalOffset) {
+ return Math.abs(Arrays.binarySearch(originalLineOffsets(), globalOffset) + 1);
+ }
+
+ private DefaultInputFile setMetadata(Metadata metadata) {
+ this.metadata = metadata;
+ return this;
}
@Override
}
DefaultInputFile that = (DefaultInputFile) o;
- return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath);
- }
-
- @Override
- public int hashCode() {
- return moduleKey.hashCode() + relativePath.hashCode() * 13;
- }
-
- @Override
- public String toString() {
- return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
+ return this.moduleKey().equals(that.moduleKey()) && this.relativePath().equals(that.relativePath());
}
@Override
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem.Index;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
import java.util.Collections;
static final FilePredicate FALSE = new FalsePredicate();
@Override
- public boolean apply(InputFile inputFile) {
+ public boolean apply(IndexedFile inputFile) {
return false;
}
import org.apache.commons.io.input.BOMInputStream;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.ScannerSide;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
}
}
- public static class Metadata {
- final int lines;
- final int nonBlankLines;
- final String hash;
- final int[] originalLineOffsets;
- final int lastValidOffset;
-
- private Metadata(int lines, int nonBlankLines, String hash, int[] originalLineOffsets, int lastValidOffset) {
- this.lines = lines;
- this.nonBlankLines = nonBlankLines;
- this.hash = hash;
- this.originalLineOffsets = originalLineOffsets;
- this.lastValidOffset = lastValidOffset;
- }
- }
-
@FunctionalInterface
public interface LineHashConsumer {
/**
* Compute a MD5 hash of each line of the file after removing of all blank chars
*/
- public static void computeLineHashesForIssueTracking(DefaultInputFile f, LineHashConsumer consumer) {
+ public static void computeLineHashesForIssueTracking(InputFile f, LineHashConsumer consumer) {
readFile(f.file(), f.charset(), new LineHashComputer(consumer, f.file()));
}
}
--- /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.batch.fs.internal;
+
+import org.sonar.api.batch.fs.InputFile;
+
+@FunctionalInterface
+public interface InputFilePredicate {
+ boolean apply(InputFile inputFile);
+}
*/
package org.sonar.api.batch.fs.internal;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.IndexedFile;
/**
* @since 4.2
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
return language.equals(f.language());
}
}
--- /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.batch.fs.internal;
+
+public class Metadata {
+ private final int lines;
+ private final int nonBlankLines;
+ private final String hash;
+ private final int[] originalLineOffsets;
+ private final int lastValidOffset;
+
+ public Metadata(int lines, int nonBlankLines, String hash, int[] originalLineOffsets, int lastValidOffset) {
+ this.lines = lines;
+ this.nonBlankLines = nonBlankLines;
+ this.hash = hash;
+ this.originalLineOffsets = originalLineOffsets;
+ this.lastValidOffset = lastValidOffset;
+ }
+
+ public int lines() {
+ return lines;
+ }
+
+ public int nonBlankLines() {
+ return nonBlankLines;
+ }
+
+ public String hash() {
+ return hash;
+ }
+
+ public int[] originalLineOffsets() {
+ return originalLineOffsets;
+ }
+
+ public int lastValidOffset() {
+ return lastValidOffset;
+ }
+
+}
package org.sonar.api.batch.fs.internal;
import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.IndexedFile;
/**
* @since 4.2
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
return !predicate.apply(f);
}
package org.sonar.api.batch.fs.internal;
import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.IndexedFile;
class OptimizedFilePredicateAdapter extends AbstractFilePredicate {
}
@Override
- public boolean apply(InputFile inputFile) {
+ public boolean apply(IndexedFile inputFile) {
return unoptimizedPredicate.apply(inputFile);
}
import com.google.common.annotations.VisibleForTesting;
import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.IndexedFile;
import java.util.ArrayList;
import java.util.Collection;
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
for (FilePredicate predicate : predicates) {
if (predicate.apply(f)) {
return true;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.utils.WildcardPattern;
public abstract class PathPattern {
this.pattern = WildcardPattern.create(pattern);
}
- public abstract boolean match(InputFile inputFile);
+ public abstract boolean match(IndexedFile inputFile);
- public abstract boolean match(InputFile inputFile, boolean caseSensitiveFileExtension);
+ public abstract boolean match(IndexedFile inputFile, boolean caseSensitiveFileExtension);
public static PathPattern create(String s) {
String trimmed = StringUtils.trim(s);
}
@Override
- public boolean match(InputFile inputFile) {
+ public boolean match(IndexedFile inputFile) {
return match(inputFile, true);
}
@Override
- public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) {
+ public boolean match(IndexedFile inputFile, boolean caseSensitiveFileExtension) {
String path = inputFile.absolutePath();
if (!caseSensitiveFileExtension) {
String extension = sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName()));
}
@Override
- public boolean match(InputFile inputFile) {
+ public boolean match(IndexedFile inputFile) {
return match(inputFile, true);
}
@Override
- public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) {
+ public boolean match(IndexedFile inputFile, boolean caseSensitiveFileExtension) {
String path = inputFile.relativePath();
if (!caseSensitiveFileExtension) {
String extension = sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName()));
*/
package org.sonar.api.batch.fs.internal;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
/**
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
return pattern.match(f);
}
package org.sonar.api.batch.fs.internal;
import org.sonar.api.batch.fs.FileSystem.Index;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.utils.PathUtils;
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
return path.equals(f.relativePath());
}
+++ /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.batch.fs.internal;
-
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * @since 4.2
- */
-class StatusPredicate extends AbstractFilePredicate {
-
- private final InputFile.Status status;
-
- StatusPredicate(InputFile.Status status) {
- this.status = status;
- }
-
- @Override
- public boolean apply(InputFile f) {
- return status == f.status();
- }
-
-}
--- /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.batch.fs.internal;
+
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import javax.annotation.Nullable;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.utils.PathUtils;
+
+/**
+ * @since 4.2
+ */
+public class TestInputFileBuilder {
+ private final String relativePath;
+ private final String moduleKey;
+ private Path moduleBaseDir;
+ private String language;
+ private InputFile.Type type = InputFile.Type.MAIN;
+ private InputFile.Status status;
+ private int lines = -1;
+ private Charset charset;
+ private int lastValidOffset = -1;
+ private String hash;
+ private int nonBlankLines;
+ private int[] originalLineOffsets;
+
+ public TestInputFileBuilder(String moduleKey, String relativePath) {
+ this.moduleKey = moduleKey;
+ this.moduleBaseDir = Paths.get(moduleKey);
+ this.relativePath = PathUtils.sanitize(relativePath);
+ }
+
+ public TestInputFileBuilder setModuleBaseDir(Path moduleBaseDir) {
+ this.moduleBaseDir = moduleBaseDir.normalize();
+ return this;
+ }
+
+ public TestInputFileBuilder setLanguage(@Nullable String language) {
+ this.language = language;
+ return this;
+ }
+
+ public TestInputFileBuilder setType(InputFile.Type type) {
+ this.type = type;
+ return this;
+ }
+
+ public TestInputFileBuilder setStatus(InputFile.Status status) {
+ this.status = status;
+ return this;
+ }
+
+ public TestInputFileBuilder setLines(int lines) {
+ this.lines = lines;
+ return this;
+ }
+
+ public TestInputFileBuilder setCharset(Charset charset) {
+ this.charset = charset;
+ return this;
+ }
+
+ public TestInputFileBuilder setLastValidOffset(int lastValidOffset) {
+ this.lastValidOffset = lastValidOffset;
+ return this;
+ }
+
+ public TestInputFileBuilder setHash(String hash) {
+ this.hash = hash;
+ return this;
+ }
+
+ public TestInputFileBuilder setNonBlankLines(int nonBlankLines) {
+ this.nonBlankLines = nonBlankLines;
+ return this;
+ }
+
+ public TestInputFileBuilder setOriginalLineOffsets(int[] originalLineOffsets) {
+ this.originalLineOffsets = originalLineOffsets;
+ return this;
+ }
+
+ public TestInputFileBuilder setMetadata(Metadata metadata) {
+ this.setLines(metadata.lines());
+ this.setLastValidOffset(metadata.lastValidOffset());
+ this.setNonBlankLines(metadata.nonBlankLines());
+ this.setHash(metadata.hash());
+ this.setOriginalLineOffsets(metadata.originalLineOffsets());
+ return this;
+ }
+
+ public TestInputFileBuilder initMetadata(String content) {
+ return setMetadata(new FileMetadata().readMetadata(new StringReader(content)));
+ }
+
+ public DefaultInputFile build() {
+ DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type);
+ indexedFile.setLanguage(language);
+ DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> new Metadata(lines, nonBlankLines, hash, originalLineOffsets, lastValidOffset));
+ inputFile.setStatus(status);
+ inputFile.setCharset(charset);
+ return inputFile;
+ }
+}
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem.Index;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
class TruePredicate extends AbstractFilePredicate {
static final FilePredicate TRUE = new TruePredicate();
@Override
- public boolean apply(InputFile inputFile) {
+ public boolean apply(IndexedFile inputFile) {
return true;
}
*/
package org.sonar.api.batch.fs.internal;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
/**
}
@Override
- public boolean apply(InputFile f) {
+ public boolean apply(IndexedFile f) {
return type == f.type();
}
import java.util.SortedMap;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.sensor.coverage.NewCoverage;
import org.sonar.api.batch.sensor.internal.DefaultStorable;
public class DefaultCoverage extends DefaultStorable implements NewCoverage {
- private DefaultInputFile inputFile;
+ private InputFile inputFile;
private CoverageType type;
private int totalCoveredLines = 0;
private int totalConditions = 0;
@Override
public DefaultCoverage onFile(InputFile inputFile) {
- this.inputFile = (DefaultInputFile) inputFile;
+ this.inputFile = inputFile;
return this;
}
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.PathPattern;
import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
import org.sonar.api.batch.sensor.internal.DefaultStorable;
private final Settings settings;
private final ImmutableList.Builder<TokensLine> result = ImmutableList.builder();
- private DefaultInputFile inputFile;
+ private InputFile inputFile;
private int startLine = Integer.MIN_VALUE;
private int startIndex = 0;
private int currentIndex = 0;
@Override
public DefaultCpdTokens onFile(InputFile inputFile) {
Preconditions.checkNotNull(inputFile, "file can't be null");
- this.inputFile = (DefaultInputFile) inputFile;
+ this.inputFile = inputFile;
String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) {
if (cpdExclusion.match(inputFile)) {
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FilePredicates;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- DefaultInputFile javaFile;
+ private Path moduleBasePath;
+
+ @Before
+ public void setUp() throws IOException {
+ moduleBasePath = temp.newFolder().toPath();
+ }
+
+ IndexedFile javaFile;
FilePredicates predicates;
@Before
public void before() throws IOException {
predicates = new DefaultFilePredicates(temp.newFolder().toPath());
- javaFile = new DefaultInputFile("foo", "src/main/java/struts/Action.java")
- .setModuleBaseDir(temp.newFolder().toPath())
- .setLanguage("java")
- .setStatus(InputFile.Status.ADDED);
+ javaFile = new DefaultIndexedFile("foo", moduleBasePath, "src/main/java/struts/Action.java")
+ .setLanguage("java");
}
@Test
assertThat(predicates.hasLanguages(Collections.<String>emptyList()).apply(javaFile)).isTrue();
}
- @Test
- public void has_status() {
- assertThat(predicates.hasStatus(InputFile.Status.ADDED).apply(javaFile)).isTrue();
- assertThat(predicates.hasStatus(InputFile.Status.CHANGED).apply(javaFile)).isFalse();
- }
-
@Test
public void has_type() {
assertThat(predicates.hasType(InputFile.Type.MAIN).apply(javaFile)).isTrue();
public void files() {
assertThat(fs.inputFiles(fs.predicates().all())).isEmpty();
- fs.add(new DefaultInputFile("foo", "src/Foo.php").setLanguage("php"));
- fs.add(new DefaultInputFile("foo", "src/Bar.java").setLanguage("java"));
- fs.add(new DefaultInputFile("foo", "src/Baz.java").setLanguage("java"));
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build());
// no language
- fs.add(new DefaultInputFile("foo", "src/readme.txt"));
+ fs.add(new TestInputFileBuilder("foo", "src/readme.txt").build());
assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNotNull();
assertThat(fs.inputFile(fs.predicates().hasRelativePath("does/not/exist"))).isNull();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("expected one element");
- fs.add(new DefaultInputFile("foo", "src/Bar.java").setLanguage("java"));
- fs.add(new DefaultInputFile("foo", "src/Baz.java").setLanguage("java"));
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build());
fs.inputFile(fs.predicates().all());
}
@Test
public void input_file_supports_non_indexed_predicates() {
- fs.add(new DefaultInputFile("foo", "src/Bar.java").setLanguage("java"));
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
// it would fail if more than one java file
assertThat(fs.inputFile(fs.predicates().hasLanguage("java"))).isNotNull();
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.stream.Collectors;
import org.junit.Rule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
public class DefaultInputFileTest {
@Test
public void test() throws Exception {
Path baseDir = temp.newFolder().toPath();
- DefaultInputFile inputFile = new DefaultInputFile("ABCDE", "src/Foo.php")
- .setModuleBaseDir(baseDir)
- .setLines(42)
- .setLanguage("php")
+
+ Metadata metadata = new Metadata(42, 42, "", new int[0], 0);
+ DefaultIndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST).setLanguage("php");
+ DefaultInputFile inputFile = new DefaultInputFile(indexedFile, (f) -> metadata)
.setStatus(InputFile.Status.ADDED)
- .setType(InputFile.Type.TEST)
.setCharset(StandardCharsets.ISO_8859_1);
assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php");
Path testFile = baseDir.resolve("src").resolve("Foo.php");
Files.createDirectories(testFile.getParent());
Files.write(testFile, "test string".getBytes(StandardCharsets.UTF_8));
- DefaultInputFile inputFile = new DefaultInputFile("ABCDE", "src/Foo.php")
- .setModuleBaseDir(baseDir)
- .setLines(42)
- .setLanguage("php")
- .setStatus(InputFile.Status.ADDED)
- .setType(InputFile.Type.TEST)
- .setCharset(StandardCharsets.ISO_8859_1);
+ Metadata metadata = new Metadata(42, 30, "", new int[0], 0);
+
+ DefaultInputFile inputFile = new DefaultInputFile(new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST)
+ .setLanguage("php"), f -> metadata)
+ .setStatus(InputFile.Status.ADDED)
+ .setCharset(StandardCharsets.ISO_8859_1);
assertThat(inputFile.contents()).isEqualTo("test string");
try (InputStream inputStream = inputFile.inputStream()) {
@Test
public void test_equals_and_hashcode() throws Exception {
- DefaultInputFile f1 = new DefaultInputFile("ABCDE", "src/Foo.php");
- DefaultInputFile f1a = new DefaultInputFile("ABCDE", "src/Foo.php");
- DefaultInputFile f2 = new DefaultInputFile("ABCDE", "src/Bar.php");
+ DefaultInputFile f1 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> mock(Metadata.class));
+ DefaultInputFile f1a = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> mock(Metadata.class));
+ DefaultInputFile f2 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Bar.php"), (f) -> mock(Metadata.class));
assertThat(f1).isEqualTo(f1);
assertThat(f1).isEqualTo(f1a);
@Test
public void test_toString() throws Exception {
- DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- assertThat(file.toString()).isEqualTo("[moduleKey=ABCDE, relative=src/Foo.php, basedir=null]");
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> mock(Metadata.class));
+ assertThat(file.toString()).isEqualTo("[moduleKey=ABCDE, relative=src/Foo.php, basedir=module]");
}
@Test
public void checkValidPointer() {
- DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- file.setLines(2);
- file.setOriginalLineOffsets(new int[] {0, 10});
- file.setLastValidOffset(15);
+ Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> metadata);
assertThat(file.newPointer(1, 0).line()).isEqualTo(1);
assertThat(file.newPointer(1, 0).lineOffset()).isEqualTo(0);
// Don't fail
file.newPointer(3, 1);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("3 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 2 line(s)");
+ assertThat(e).hasMessage("3 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 2 line(s)");
}
try {
file.newPointer(1, -1);
file.newPointer(1, 10);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("10 is not a valid line offset for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 9 character(s) at line 1");
+ assertThat(e).hasMessage("10 is not a valid line offset for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 9 character(s) at line 1");
}
}
@Test
public void checkValidPointerUsingGlobalOffset() {
- DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- file.setLines(2);
- file.setOriginalLineOffsets(new int[] {0, 10});
- file.setLastValidOffset(15);
+ Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> metadata);
assertThat(file.newPointer(0).line()).isEqualTo(1);
assertThat(file.newPointer(0).lineOffset()).isEqualTo(0);
file.newPointer(16);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("16 is not a valid offset for file [moduleKey=ABCDE, relative=src/Foo.php, basedir=null]. Max offset is 15");
+ assertThat(e).hasMessage("16 is not a valid offset for file [moduleKey=ABCDE, relative=src/Foo.php, basedir=module]. Max offset is 15");
}
}
@Test
public void checkValidRange() {
- DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- file.initMetadata("bla bla a\nabcde");
+ Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde"));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> metadata);
+
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isEqualTo(1);
// Don't fail
file.newRange(file.newPointer(1, 0), file.newPointer(1, 1));
file.newRange(file.newPointer(1, 0), file.newPointer(1, 10));
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("10 is not a valid line offset for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 9 character(s) at line 1");
+ assertThat(e).hasMessage("10 is not a valid line offset for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 9 character(s) at line 1");
}
}
@Test
public void selectLine() {
- DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- file.initMetadata("bla bla a\nabcde\n\nabc");
+ Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde\n\nabc"));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> metadata);
+
assertThat(file.selectLine(1).start().line()).isEqualTo(1);
assertThat(file.selectLine(1).start().lineOffset()).isEqualTo(0);
assertThat(file.selectLine(1).end().line()).isEqualTo(1);
file.selectLine(5);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("5 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 4 line(s)");
+ assertThat(e).hasMessage("5 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 4 line(s)");
}
}
@Test
public void checkValidRangeUsingGlobalOffset() {
- DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- file.setLines(2);
- file.setOriginalLineOffsets(new int[] {0, 10});
- file.setLastValidOffset(15);
+ Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> metadata);
TextRange newRange = file.newRange(10, 13);
assertThat(newRange.start().line()).isEqualTo(2);
assertThat(newRange.start().lineOffset()).isEqualTo(0);
@Test
public void testRangeOverlap() {
- DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
- file.setLines(2);
- file.setOriginalLineOffsets(new int[] {0, 10});
- file.setLastValidOffset(15);
+ Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php"), (f) -> metadata);
// Don't fail
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)))).isTrue();
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isTrue();
File tempFile = temp.newFile();
FileUtils.touch(tempFile);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(1);
- assertThat(metadata.nonBlankLines).isEqualTo(0);
- assertThat(metadata.hash).isNotEmpty();
- assertThat(metadata.originalLineOffsets).containsOnly(0);
- assertThat(metadata.lastValidOffset).isEqualTo(0);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(1);
+ assertThat(metadata.nonBlankLines()).isEqualTo(0);
+ assertThat(metadata.hash()).isNotEmpty();
+ assertThat(metadata.originalLineOffsets()).containsOnly(0);
+ assertThat(metadata.lastValidOffset()).isEqualTo(0);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\r\nbar\r\nbaz", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(3);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10);
- assertThat(metadata.lastValidOffset).isEqualTo(13);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 5, 10);
+ assertThat(metadata.lastValidOffset()).isEqualTo(13);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "marker´s\n", Charset.forName("cp1252"));
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(2);
- assertThat(metadata.hash).isEqualTo(md5Hex("marker\ufffds\n"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 9);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(2);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("marker\ufffds\n"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 9);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "föo\r\nbà r\r\n\u1D11Ebaßz\r\n", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(4);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("föo\nbà r\n\u1D11Ebaßz\n"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10, 18);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("föo\nbà r\n\u1D11Ebaßz\n"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 5, 10, 18);
}
@Test
public void non_ascii_utf_16() throws Exception {
File tempFile = temp.newFile();
FileUtils.write(tempFile, "föo\r\nbà r\r\n\u1D11Ebaßz\r\n", StandardCharsets.UTF_16, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_16);
- assertThat(metadata.lines).isEqualTo(4);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("föo\nbà r\n\u1D11Ebaßz\n".getBytes(StandardCharsets.UTF_8)));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10, 18);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_16);
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("föo\nbà r\n\u1D11Ebaßz\n".getBytes(StandardCharsets.UTF_8)));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 5, 10, 18);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\nbar\nbaz", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(3);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8);
- assertThat(metadata.lastValidOffset).isEqualTo(11);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 8);
+ assertThat(metadata.lastValidOffset()).isEqualTo(11);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\nbar\nbaz\n", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(4);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8, 12);
- assertThat(metadata.lastValidOffset).isEqualTo(12);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 8, 12);
+ assertThat(metadata.lastValidOffset()).isEqualTo(12);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\rbar\rbaz", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(3);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8);
- assertThat(metadata.lastValidOffset).isEqualTo(11);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 8);
+ assertThat(metadata.lastValidOffset()).isEqualTo(11);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\rbar\rbaz\r", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(4);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8, 12);
- assertThat(metadata.lastValidOffset).isEqualTo(12);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 8, 12);
+ assertThat(metadata.lastValidOffset()).isEqualTo(12);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\nbar\r\nbaz\n", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(4);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9, 13);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 9, 13);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\n\n\nbar", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(4);
- assertThat(metadata.nonBlankLines).isEqualTo(2);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\n\n\nbar"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 5, 6);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(2);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\n\n\nbar"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 5, 6);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\nbar\r\nbaz", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(3);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 9);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "\nfoo\nbar\r\nbaz", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(4);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("\nfoo\nbar\nbaz"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 1, 5, 10);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("\nfoo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 1, 5, 10);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, "\uFEFFfoo\nbar\r\nbaz", StandardCharsets.UTF_8, true);
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(3);
- assertThat(metadata.nonBlankLines).isEqualTo(3);
- assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
- assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9);
+ Metadata metadata = new FileMetadata().readMetadata(tempFile, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets()).containsOnly(0, 4, 9);
}
@Test
File tempFile = temp.newFile();
FileUtils.write(tempFile, " foo\nb ar\r\nbaz \t", StandardCharsets.UTF_8, true);
- DefaultInputFile f = new DefaultInputFile("foo", tempFile.getName());
- f.setModuleBaseDir(tempFile.getParentFile().toPath());
- f.setCharset(StandardCharsets.UTF_8);
+ DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName())
+ .setModuleBaseDir(tempFile.getParentFile().toPath())
+ .setCharset(StandardCharsets.UTF_8)
+ .build();
FileMetadata.computeLineHashesForIssueTracking(f, new LineHashConsumer() {
@Override
File tempFile = temp.newFile();
FileUtils.write(tempFile, "", StandardCharsets.UTF_8, true);
- DefaultInputFile f = new DefaultInputFile("foo", tempFile.getName());
- f.setModuleBaseDir(tempFile.getParentFile().toPath());
- f.setCharset(StandardCharsets.UTF_8);
+ DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName())
+ .setModuleBaseDir(tempFile.getParentFile().toPath())
+ .setCharset(StandardCharsets.UTF_8)
+ .build();
FileMetadata.computeLineHashesForIssueTracking(f, new LineHashConsumer() {
@Override
File file2 = temp.newFile();
FileUtils.write(file2, "foo\nbar", StandardCharsets.UTF_8, true);
- String hash1 = new FileMetadata().readMetadata(file1, StandardCharsets.UTF_8).hash;
- String hash1a = new FileMetadata().readMetadata(file1a, StandardCharsets.UTF_8).hash;
- String hash2 = new FileMetadata().readMetadata(file2, StandardCharsets.UTF_8).hash;
+ String hash1 = new FileMetadata().readMetadata(file1, StandardCharsets.UTF_8).hash();
+ String hash1a = new FileMetadata().readMetadata(file1a, StandardCharsets.UTF_8).hash();
+ String hash2 = new FileMetadata().readMetadata(file2, StandardCharsets.UTF_8).hash();
assertThat(hash1).isEqualTo(hash1a);
assertThat(hash1).isNotEqualTo(hash2);
}
public void binary_file_with_unmappable_character() throws Exception {
File woff = new File(this.getClass().getResource("glyphicons-halflings-regular.woff").toURI());
- FileMetadata.Metadata metadata = new FileMetadata().readMetadata(woff, StandardCharsets.UTF_8);
- assertThat(metadata.lines).isEqualTo(135);
- assertThat(metadata.nonBlankLines).isEqualTo(133);
- assertThat(metadata.hash).isNotEmpty();
+ Metadata metadata = new FileMetadata().readMetadata(woff, StandardCharsets.UTF_8);
+ assertThat(metadata.lines()).isEqualTo(135);
+ assertThat(metadata.nonBlankLines()).isEqualTo(133);
+ assertThat(metadata.hash()).isNotEmpty();
assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains("Invalid character encountered in file");
assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains(
*/
package org.sonar.api.batch.fs.internal;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.IndexedFile;
+import java.io.IOException;
import java.nio.file.Path;
import static org.assertj.core.api.Assertions.assertThat;
public class PathPatternTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ private Path moduleBasePath;
+
+ @Before
+ public void setUp() throws IOException {
+ moduleBasePath = temp.newFolder().toPath();
+ }
@Test
public void match_relative_path() {
PathPattern pattern = PathPattern.create("**/*Foo.java");
assertThat(pattern.toString()).isEqualTo("**/*Foo.java");
- InputFile inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/MyFoo.java");
- assertThat(pattern.match(inputFile)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.java");
+ assertThat(pattern.match(indexedFile)).isTrue();
// case sensitive by default
- inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/MyFoo.JAVA");
- assertThat(pattern.match(inputFile)).isFalse();
+ indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA");
+ assertThat(pattern.match(indexedFile)).isFalse();
- inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/Other.java");
- assertThat(pattern.match(inputFile)).isFalse();
+ indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.java");
+ assertThat(pattern.match(indexedFile)).isFalse();
}
@Test
public void match_relative_path_and_insensitive_file_extension() throws Exception {
PathPattern pattern = PathPattern.create("**/*Foo.java");
- Path moduleBaseDir = temp.newFolder().toPath();
- InputFile inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/MyFoo.JAVA").setModuleBaseDir(moduleBaseDir);
- assertThat(pattern.match(inputFile, false)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA");
+ assertThat(pattern.match(indexedFile, false)).isTrue();
- inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/Other.java").setModuleBaseDir(moduleBaseDir);
- assertThat(pattern.match(inputFile, false)).isFalse();
+ indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.java");
+ assertThat(pattern.match(indexedFile, false)).isFalse();
}
@Test
PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
- Path moduleBaseDir = temp.newFolder().toPath();
- InputFile inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/MyFoo.java").setModuleBaseDir(moduleBaseDir);
- assertThat(pattern.match(inputFile)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.java");
+ assertThat(pattern.match(indexedFile)).isTrue();
// case sensitive by default
- inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/MyFoo.JAVA").setModuleBaseDir(moduleBaseDir);
- assertThat(pattern.match(inputFile)).isFalse();
+ indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA");
+ assertThat(pattern.match(indexedFile)).isFalse();
- inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/Other.java").setModuleBaseDir(moduleBaseDir);
- assertThat(pattern.match(inputFile)).isFalse();
+ indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.java");
+ assertThat(pattern.match(indexedFile)).isFalse();
}
@Test
PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
- Path moduleBaseDir = temp.newFolder().toPath();
- InputFile inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/MyFoo.JAVA").setModuleBaseDir(moduleBaseDir);
- assertThat(pattern.match(inputFile, false)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/MyFoo.JAVA");
+ assertThat(pattern.match(indexedFile, false)).isTrue();
- inputFile = new DefaultInputFile("ABCDE", "src/main/java/org/Other.JAVA").setModuleBaseDir(moduleBaseDir);
- assertThat(pattern.match(inputFile, false)).isFalse();
+ indexedFile = new DefaultIndexedFile("ABCDE", moduleBasePath, "src/main/java/org/Other.JAVA");
+ assertThat(pattern.match(indexedFile, false)).isFalse();
}
@Test
package org.sonar.api.batch.sensor.cpd.internal;
import org.junit.Test;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
public class DefaultCpdTokensTest {
- private static final DefaultInputFile INPUT_FILE = new DefaultInputFile("foo", "src/Foo.java")
+ private static final InputFile INPUT_FILE = new TestInputFileBuilder("foo", "src/Foo.java")
.setLines(2)
- .setLanguage("java")
.setOriginalLineOffsets(new int[] {0, 50})
- .setLastValidOffset(100);
+ .setLastValidOffset(100)
+ .build();
@Test
public void save_no_tokens() {
tokens.addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo");
fail("Expected exception");
} catch (Exception e) {
- assertThat(e).hasMessage("Tokens of file [moduleKey=foo, relative=src/Foo.java, basedir=null] should be provided in order.\n" +
+ assertThat(e).hasMessage("Tokens of file [moduleKey=foo, relative=src/Foo.java, basedir=foo] should be provided in order.\n" +
"Previous token: Range[from [line=1, lineOffset=6] to [line=1, lineOffset=10]]\n" +
"Last token: Range[from [line=1, lineOffset=2] to [line=1, lineOffset=5]]");
}
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextPointer;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultTextPointer;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.error.NewAnalysisError;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import static org.assertj.core.api.Assertions.assertThat;
@Before
public void setUp() {
- inputFile = new DefaultInputFile("module1", "src/File.java");
+ inputFile = new TestInputFileBuilder("module1", "src/File.java").build();
textPointer = new DefaultTextPointer(5, 2);
storage = mock(SensorStorage.class);
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultTextPointer;
import org.sonar.api.batch.fs.internal.DefaultTextRange;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import static org.assertj.core.api.Assertions.assertThat;
public class DefaultHighlightingTest {
- private static final DefaultInputFile INPUT_FILE = new DefaultInputFile("foo", "src/Foo.java")
+ private static final InputFile INPUT_FILE = new TestInputFileBuilder("foo", "src/Foo.java")
.setLines(2)
.setOriginalLineOffsets(new int[] {0, 50})
- .setLastValidOffset(100);
+ .setLastValidOffset(100)
+ .build();
private Collection<SyntaxHighlightingRule> highlightingRules;
package org.sonar.api.batch.sensor.internal;
import java.io.File;
-import java.io.StringReader;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.DefaultTextPointer;
-import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.error.AnalysisError;
assertThat(tester.allIssues()).isEmpty();
NewIssue newIssue = tester.newIssue();
newIssue
- .at(newIssue.newLocation().on(new DefaultInputFile("foo", "src/Foo.java")))
+ .at(newIssue.newLocation().on(new TestInputFileBuilder("foo", "src/Foo.java").build()))
.forRule(RuleKey.of("repo", "rule"))
.save();
newIssue = tester.newIssue();
newIssue
- .at(newIssue.newLocation().on(new DefaultInputFile("foo", "src/Foo.java")))
+ .at(newIssue.newLocation().on(new TestInputFileBuilder("foo", "src/Foo.java").build()))
.forRule(RuleKey.of("repo", "rule"))
.save();
assertThat(tester.allIssues()).hasSize(2);
assertThat(tester.allAnalysisErrors()).isEmpty();
NewAnalysisError newAnalysisError = tester.newAnalysisError();
- InputFile file = new DefaultInputFile("foo", "src/Foo.java");
+ InputFile file = new TestInputFileBuilder("foo", "src/Foo.java").build();
newAnalysisError.onFile(file)
.message("error")
.at(new DefaultTextPointer(5, 2))
assertThat(tester.measures("foo:src/Foo.java")).isEmpty();
assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNull();
tester.<Integer>newMeasure()
- .on(new DefaultInputFile("foo", "src/Foo.java"))
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
.forMetric(CoreMetrics.NCLOC)
.withValue(2)
.save();
assertThat(tester.measures("foo:src/Foo.java")).hasSize(1);
assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNotNull();
tester.<Integer>newMeasure()
- .on(new DefaultInputFile("foo", "src/Foo.java"))
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
.forMetric(CoreMetrics.LINES)
.withValue(4)
.save();
@Test(expected = SonarException.class)
public void duplicateMeasures() {
tester.<Integer>newMeasure()
- .on(new DefaultInputFile("foo", "src/Foo.java"))
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
.forMetric(CoreMetrics.NCLOC)
.withValue(2)
.save();
tester.<Integer>newMeasure()
- .on(new DefaultInputFile("foo", "src/Foo.java"))
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
.forMetric(CoreMetrics.NCLOC)
.withValue(2)
.save();
public void testHighlighting() {
assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).isEmpty();
tester.newHighlighting()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
.highlight(1, 0, 1, 5, TypeOfText.ANNOTATION)
.highlight(8, 10, TypeOfText.CONSTANT)
.highlight(9, 10, TypeOfText.COMMENT)
@Test(expected = UnsupportedOperationException.class)
public void duplicateHighlighting() {
tester.newHighlighting()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
.highlight(1, 0, 1, 5, TypeOfText.ANNOTATION)
.save();
tester.newHighlighting()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
.highlight(1, 0, 1, 5, TypeOfText.ANNOTATION)
.save();
}
assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 0)).isNull();
NewSymbolTable symbolTable = tester.newSymbolTable()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))));
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build());
symbolTable
.newSymbol(1, 8, 1, 10);
@Test(expected = UnsupportedOperationException.class)
public void duplicateSymbolReferences() {
NewSymbolTable symbolTable = tester.newSymbolTable()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))));
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build());
symbolTable
.newSymbol(1, 8, 1, 10);
symbolTable.save();
symbolTable = tester.newSymbolTable()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))));
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build());
symbolTable
.newSymbol(1, 8, 1, 10);
exception.expect(IllegalStateException.class);
tester.newCoverage()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
.lineHits(0, 3);
}
exception.expect(IllegalStateException.class);
tester.newCoverage()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
.lineHits(4, 3);
}
assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
tester.newCoverage()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasdas"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar\nasdas").build())
.lineHits(1, 2)
.lineHits(2, 3)
.save();
public void multipleCoverage() {
tester.newCoverage()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasdas"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar\nasdas").build())
.lineHits(1, 2)
.conditions(3, 4, 2)
.save();
tester.newCoverage()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasdas"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar\nasdas").build())
.lineHits(1, 2)
.conditions(3, 4, 3)
.save();
assertThat(tester.conditions("foo:src/Foo.java", 1)).isNull();
assertThat(tester.coveredConditions("foo:src/Foo.java", 1)).isNull();
tester.newCoverage()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasd\nasdas\nasdfas"))))
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java")
+ .initMetadata("annot dsf fds foo bar\nasd\nasdas\nasdfas")
+ .build())
.conditions(1, 4, 2)
.save();
assertThat(tester.conditions("foo:src/Foo.java", 1)).isEqualTo(4);
@Test
public void testCpdTokens() {
assertThat(tester.cpdTokens("foo:src/Foo.java")).isNull();
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("public class Foo {\n\n}")));
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
+ .initMetadata("public class Foo {\n\n}")
+ .build();
tester.newCpdTokens()
.onFile(inputFile)
.addToken(inputFile.newRange(0, 6), "public")
@Test(expected = UnsupportedOperationException.class)
public void duplicateCpdTokens() {
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("public class Foo {\n\n}")));
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
+ .initMetadata("public class Foo {\n\n}")
+ .build();
tester.newCpdTokens()
.onFile(inputFile)
.addToken(inputFile.newRange(0, 6), "public")
*/
package org.sonar.api.batch.sensor.issue.internal;
-import java.io.StringReader;
import org.apache.commons.lang.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import static org.assertj.core.api.Assertions.assertThat;
@Rule
public ExpectedException thrown = ExpectedException.none();
- private DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.php").initMetadata(new FileMetadata().readMetadata(new StringReader("Foo\nBar\n")));
+ private InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata("Foo\nBar\n")
+ .build();
@Test
public void not_allowed_to_call_on_twice() {
*/
package org.sonar.api.batch.sensor.issue.internal;
-import java.io.StringReader;
import org.junit.Test;
import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
-import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.rule.RuleKey;
public class DefaultIssueTest {
- private DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.php").initMetadata(new FileMetadata().readMetadata(new StringReader("Foo\nBar\n")));
+ private DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata("Foo\nBar\n")
+ .build();
@Test
public void build_file_issue() {
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.measures.CoreMetrics;
SensorStorage storage = mock(SensorStorage.class);
DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(storage)
.forMetric(CoreMetrics.LINES)
- .on(new DefaultInputFile("foo", "src/Foo.php"))
+ .on(new TestInputFileBuilder("foo", "src/Foo.php").build())
.withValue(3);
- assertThat(newMeasure.inputComponent()).isEqualTo(new DefaultInputFile("foo", "src/Foo.php"));
+ assertThat(newMeasure.inputComponent()).isEqualTo(new TestInputFileBuilder("foo", "src/Foo.php").build());
assertThat(newMeasure.metric()).isEqualTo(CoreMetrics.LINES);
assertThat(newMeasure.value()).isEqualTo(3);
thrown.expectMessage("on() already called");
new DefaultMeasure<Integer>()
.on(new DefaultInputModule("foo"))
- .on(new DefaultInputFile("foo", "src/Foo.php"))
+ .on(new TestInputFileBuilder("foo", "src/Foo.php").build())
.withValue(3)
.save();
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import static org.assertj.core.api.Assertions.assertThat;
public class DefaultSymbolTableTest {
- private static final DefaultInputFile INPUT_FILE = new DefaultInputFile("foo", "src/Foo.java")
+ private static final InputFile INPUT_FILE = new TestInputFileBuilder("foo", "src/Foo.java")
.setLines(2)
.setOriginalLineOffsets(new int[] {0, 50})
- .setLastValidOffset(100);
+ .setLastValidOffset(100)
+ .build();
private Map<TextRange, Set<TextRange>> referencesPerSymbol;
import java.util.Collection;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.ObjectUtils;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.FileMetadata;
/**
return new FileHashes(hashes, linesByHash);
}
- public static FileHashes create(DefaultInputFile f) {
+ public static FileHashes create(InputFile f) {
final byte[][] hashes = new byte[f.lines()][];
FileMetadata.computeLineHashesForIssueTracking(f,
(lineIdx, hash) -> hashes[lineIdx - 1] = hash);
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.batch.ScannerSide;
+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.batch.rule.ActiveRule;
private boolean shouldCopyServerIssues(BatchComponent component) {
if (!mode.scanAllFiles() && component.isFile()) {
- DefaultInputFile inputFile = (DefaultInputFile) component.inputComponent();
+ InputFile inputFile = (InputFile) component.inputComponent();
if (inputFile.status() == Status.SAME) {
return true;
}
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.scanner.index.BatchComponent;
import org.sonar.scanner.index.BatchComponentCache;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
continue;
}
- DefaultInputFile inputFile = (DefaultInputFile) resource.inputComponent();
+ InputFile inputFile = (InputFile) resource.inputComponent();
File iofile = writer.getSourceFile(resource.batchId());
int line = 0;
try (FileOutputStream output = new FileOutputStream(iofile); BOMInputStream bomIn = new BOMInputStream(new FileInputStream(inputFile.file()),
import org.sonar.scanner.scan.filesystem.ExclusionFilters;
import org.sonar.scanner.scan.filesystem.FileIndexer;
import org.sonar.scanner.scan.filesystem.FileSystemLogger;
-import org.sonar.scanner.scan.filesystem.InputFileBuilderFactory;
+import org.sonar.scanner.scan.filesystem.IndexedFileBuilderProvider;
+import org.sonar.scanner.scan.filesystem.MetadataGeneratorProvider;
import org.sonar.scanner.scan.filesystem.LanguageDetectionFactory;
import org.sonar.scanner.scan.filesystem.ModuleFileSystemInitializer;
import org.sonar.scanner.scan.filesystem.ModuleInputFileCache;
ModuleInputFileCache.class,
FileExclusions.class,
ExclusionFilters.class,
- InputFileBuilderFactory.class,
+ new MetadataGeneratorProvider(),
FileMetadata.class,
StatusDetectionFactory.class,
LanguageDetectionFactory.class,
FileIndexer.class,
+ new IndexedFileBuilderProvider(),
ComponentIndexer.class,
LanguageVerifier.class,
FileSystemLogger.class,
*/
package org.sonar.scanner.scan.filesystem;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.internal.AbstractFilePredicate;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
/**
* Additional {@link org.sonar.api.batch.fs.FilePredicate}s that are
}
@Override
- public boolean apply(InputFile f) {
- return key.equals(((DefaultInputFile) f).key());
+ public boolean apply(IndexedFile f) {
+ return key.equals(f.key());
}
}
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
-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.utils.MessageException;
import org.sonar.scanner.analysis.DefaultAnalysisMode;
+import org.sonar.scanner.repository.ProjectRepositories;
/**
* @since 3.5
private List<File> testDirsOrFiles = Lists.newArrayList();
private ComponentIndexer componentIndexer;
private boolean initialized;
+ private Charset charset = null;
public DefaultModuleFileSystem(ModuleInputFileCache moduleInputFileCache, Project project,
- Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode) {
+ Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode,
+ ProjectRepositories projectRepositories) {
super(initializer.baseDir(), moduleInputFileCache);
- setFields(project, settings, indexer, initializer, componentIndexer, mode);
+ setFields(project, settings, indexer, initializer, componentIndexer, mode, projectRepositories);
}
@VisibleForTesting
public DefaultModuleFileSystem(Project project,
- Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode) {
+ Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode,
+ ProjectRepositories projectRepositories) {
super(initializer.baseDir().toPath());
- setFields(project, settings, indexer, initializer, componentIndexer, mode);
+ setFields(project, settings, indexer, initializer, componentIndexer, mode, projectRepositories);
}
private void setFields(Project project,
- Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode) {
+ Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode,
+ ProjectRepositories projectRepositories) {
this.componentIndexer = componentIndexer;
this.moduleKey = project.getKey();
this.settings = settings;
// filter the files sensors have access to
if (!mode.scanAllFiles()) {
- setDefaultPredicate(predicates.not(predicates.hasStatus(Status.SAME)));
+ setDefaultPredicate(new SameInputFilePredicate(projectRepositories, moduleKey));
}
}
@Override
public Charset encoding() {
- final Charset charset;
- String encoding = settings.getString(CoreProperties.ENCODING_PROPERTY);
- if (StringUtils.isNotEmpty(encoding)) {
- charset = Charset.forName(StringUtils.trim(encoding));
- } else {
- charset = Charset.defaultCharset();
+ if (charset == null) {
+ String encoding = settings.getString(CoreProperties.ENCODING_PROPERTY);
+ if (StringUtils.isNotEmpty(encoding)) {
+ charset = Charset.forName(StringUtils.trim(encoding));
+ } else {
+ charset = Charset.defaultCharset();
+ }
}
return charset;
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.ScannerSide;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.PathPattern;
import org.sonar.api.scan.filesystem.FileExclusions;
}
}
- public boolean accept(InputFile inputFile, InputFile.Type type) {
+ public boolean accept(IndexedFile indexedFile, InputFile.Type type) {
PathPattern[] inclusionPatterns;
PathPattern[] exclusionPatterns;
if (InputFile.Type.MAIN == type) {
if (inclusionPatterns.length > 0) {
boolean matchInclusion = false;
for (PathPattern pattern : inclusionPatterns) {
- matchInclusion |= pattern.match(inputFile);
+ matchInclusion |= pattern.match(indexedFile);
}
if (!matchInclusion) {
return false;
}
if (exclusionPatterns.length > 0) {
for (PathPattern pattern : exclusionPatterns) {
- if (pattern.match(inputFile)) {
+ if (pattern.match(indexedFile)) {
return false;
}
}
*/
package org.sonar.scanner.scan.filesystem;
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystemLoopException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-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.IndexedFile;
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.DefaultIndexedFile;
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.batch.fs.InputFileFilter;
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.util.ProgressReport;
private final InputFileFilter[] filters;
private final boolean isAggregator;
private final ExclusionFilters exclusionFilters;
- private final InputFileBuilderFactory inputFileBuilderFactory;
private ProgressReport progressReport;
- private ExecutorService executorService;
- private List<Future<Void>> tasks;
+ private IndexedFileBuilder indexedFileBuilder;
+ private MetadataGenerator metadataGenerator;
- public FileIndexer(ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, ProjectDefinition def, InputFileFilter[] filters) {
+ public FileIndexer(ExclusionFilters exclusionFilters, IndexedFileBuilder indexedFileBuilder, MetadataGenerator inputFileBuilder, ProjectDefinition def,
+ InputFileFilter[] filters) {
+ this.indexedFileBuilder = indexedFileBuilder;
+ this.metadataGenerator = inputFileBuilder;
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]);
+ public FileIndexer(ExclusionFilters exclusionFilters, IndexedFileBuilder indexedFileBuilder, MetadataGenerator inputFileBuilder, ProjectDefinition def) {
+ this(exclusionFilters, indexedFileBuilder, inputFileBuilder, def, new InputFileFilter[0]);
}
void index(DefaultModuleFileSystem fileSystem) {
Progress progress = new Progress();
- InputFileBuilder inputFileBuilder = inputFileBuilderFactory.create(fileSystem);
- int threads = Math.max(1, Runtime.getRuntime().availableProcessors() - 1);
- executorService = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder().setNameFormat("FileIndexer-%d").build());
- tasks = new ArrayList<>();
- indexFiles(fileSystem, progress, inputFileBuilder, fileSystem.sources(), InputFile.Type.MAIN);
- indexFiles(fileSystem, progress, inputFileBuilder, fileSystem.tests(), InputFile.Type.TEST);
-
- waitForTasksToComplete();
+ indexFiles(fileSystem, progress, fileSystem.sources(), InputFile.Type.MAIN);
+ indexFiles(fileSystem, progress, fileSystem.tests(), InputFile.Type.TEST);
progressReport.stop(progress.count() + " files indexed");
}
}
- private void waitForTasksToComplete() {
- executorService.shutdown();
- for (Future<Void> task : tasks) {
- try {
- task.get();
- } catch (ExecutionException e) {
- // Unwrap ExecutionException
- throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause());
- } catch (InterruptedException e) {
- throw new IllegalStateException(e);
- }
- }
- }
-
- private void indexFiles(DefaultModuleFileSystem fileSystem, Progress progress, InputFileBuilder inputFileBuilder, List<File> sources, InputFile.Type type) {
+ private void indexFiles(DefaultModuleFileSystem fileSystem, Progress progress, List<File> sources, InputFile.Type type) {
try {
for (File dirOrFile : sources) {
if (dirOrFile.isDirectory()) {
- indexDirectory(inputFileBuilder, fileSystem, progress, dirOrFile, type);
+ indexDirectory(fileSystem, progress, dirOrFile, type);
} else {
- indexFile(inputFileBuilder, fileSystem, progress, dirOrFile.toPath(), type);
+ indexFile(fileSystem, progress, dirOrFile.toPath(), type);
}
}
} catch (IOException e) {
}
}
- private void indexDirectory(final InputFileBuilder inputFileBuilder, final DefaultModuleFileSystem fileSystem, final Progress status,
- final File dirToIndex, final InputFile.Type type) throws IOException {
+ private void indexDirectory(final DefaultModuleFileSystem fileSystem, final Progress status, final File dirToIndex, final InputFile.Type type) throws IOException {
Files.walkFileTree(dirToIndex.toPath().normalize(), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
- new IndexFileVisitor(inputFileBuilder, fileSystem, status, type));
+ new IndexFileVisitor(fileSystem, status, type));
}
- private void indexFile(InputFileBuilder inputFileBuilder, DefaultModuleFileSystem fileSystem, Progress progress, Path sourceFile, InputFile.Type type) throws IOException {
+ private void indexFile(DefaultModuleFileSystem fileSystem, Progress progress, Path sourceFile, InputFile.Type type) throws IOException {
// get case of real file without resolving link
Path realFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS);
- DefaultInputFile inputFile = inputFileBuilder.create(realFile.toFile());
- if (inputFile != null) {
- // Set basedir on input file prior to adding it to the FS since exclusions filters may require the absolute path
- inputFile.setModuleBaseDir(fileSystem.baseDirPath());
- if (exclusionFilters.accept(inputFile, type)) {
- indexFile(inputFileBuilder, fileSystem, progress, inputFile, type);
+ DefaultIndexedFile indexedFile = indexedFileBuilder.create(realFile, type, fileSystem.baseDirPath());
+ if (indexedFile != null) {
+ if (exclusionFilters.accept(indexedFile, type)) {
+ InputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.readMetadata(f, fileSystem.encoding()));
+ if (accept(inputFile)) {
+ fileSystem.add(inputFile);
+ }
+ indexParentDir(fileSystem, indexedFile);
+ progress.markAsIndexed(indexedFile);
+ LOG.debug("'{}' indexed {} with language '{}'", indexedFile.relativePath(), type == Type.TEST ? "as test " : "", indexedFile.language());
} else {
progress.increaseExcludedByPatternsCount();
}
}
}
- private void indexFile(final InputFileBuilder inputFileBuilder, final DefaultModuleFileSystem fs,
- final Progress status, final DefaultInputFile inputFile, final InputFile.Type type) {
-
- tasks.add(executorService.submit(() -> {
- DefaultInputFile completedInputFile = inputFileBuilder.completeAndComputeMetadata(inputFile, type);
- if (completedInputFile != null && accept(completedInputFile)) {
- LOG.debug("'{}' indexed {}with language '{}' and charset '{}'",
- inputFile.relativePath(),
- type == Type.TEST ? "as test " : "",
- inputFile.language(),
- inputFile.charset());
- fs.add(completedInputFile);
- status.markAsIndexed(completedInputFile);
- File parentDir = completedInputFile.file().getParentFile();
- String relativePath = new PathResolver().relativePath(fs.baseDir(), parentDir);
- if (relativePath != null) {
- DefaultInputDir inputDir = new DefaultInputDir(fs.moduleKey(), relativePath);
- fs.add(inputDir);
- }
- }
- return null;
- }));
-
+ private void indexParentDir(DefaultModuleFileSystem fileSystem, IndexedFile indexedFile) {
+ File parentDir = indexedFile.file().getParentFile();
+ String relativePath = new PathResolver().relativePath(fileSystem.baseDir(), parentDir);
+ if (relativePath != null) {
+ DefaultInputDir inputDir = new DefaultInputDir(fileSystem.moduleKey(), relativePath);
+ inputDir.setModuleBaseDir(fileSystem.baseDirPath());
+ fileSystem.add(inputDir);
+ }
}
- private boolean accept(InputFile inputFile) {
+ private boolean accept(InputFile indexedFile) {
// InputFileFilter extensions
for (InputFileFilter filter : filters) {
- if (!filter.accept(inputFile)) {
- LOG.debug("'{}' excluded by {}", inputFile.relativePath(), filter.getClass().getName());
+ if (!filter.accept(indexedFile)) {
+ LOG.debug("'{}' excluded by {}", indexedFile.relativePath(), filter.getClass().getName());
return false;
}
}
}
private class IndexFileVisitor implements FileVisitor<Path> {
- private InputFileBuilder inputFileBuilder;
private DefaultModuleFileSystem fileSystem;
private Progress status;
private Type type;
- IndexFileVisitor(InputFileBuilder inputFileBuilder, DefaultModuleFileSystem fileSystem, Progress status, InputFile.Type type) {
- this.inputFileBuilder = inputFileBuilder;
+ IndexFileVisitor(DefaultModuleFileSystem fileSystem, Progress status, InputFile.Type type) {
this.fileSystem = fileSystem;
this.status = status;
this.type = type;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!Files.isHidden(file)) {
- indexFile(inputFileBuilder, fileSystem, status, file, type);
+ indexFile(fileSystem, status, file, type);
}
return FileVisitResult.CONTINUE;
}
private final Set<Path> indexed = new HashSet<>();
private int excludedByPatternsCount = 0;
- synchronized void markAsIndexed(InputFile inputFile) {
+ synchronized void markAsIndexed(IndexedFile inputFile) {
if (indexed.contains(inputFile.path())) {
throw MessageException.of("File " + inputFile + " can't be indexed twice. Please check that inclusion/exclusion patterns produce "
+ "disjoint sets for main and test files");
--- /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 java.nio.file.Path;
+
+import javax.annotation.CheckForNull;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
+import org.sonar.api.config.Settings;
+import org.sonar.api.scan.filesystem.PathResolver;
+
+public class IndexedFileBuilder {
+ private static final Logger LOG = LoggerFactory.getLogger(IndexedFileBuilder.class);
+ private final String moduleKey;
+ private final PathResolver pathResolver;
+ private final LanguageDetection langDetection;
+ private final Settings settings;
+
+ IndexedFileBuilder(String moduleKey, PathResolver pathResolver, Settings settings, LanguageDetection langDetection) {
+ this.moduleKey = moduleKey;
+ this.pathResolver = pathResolver;
+ this.settings = settings;
+ this.langDetection = langDetection;
+ }
+
+ @CheckForNull
+ DefaultIndexedFile create(Path file, InputFile.Type type, Path moduleBaseDir) {
+ String relativePath = pathResolver.relativePath(moduleBaseDir, file);
+ if (relativePath == null) {
+ LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", file.toAbsolutePath(), moduleBaseDir);
+ return null;
+ }
+ DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type);
+ String language = langDetection.language(indexedFile);
+ if (language == null && !settings.getBoolean(CoreProperties.IMPORT_UNKNOWN_FILES_KEY)) {
+ LOG.debug("'{}' language is not supported by any analyzer. Skipping it.", relativePath);
+ return null;
+ }
+
+ indexedFile.setLanguage(language);
+ return indexedFile;
+ }
+}
--- /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.picocontainer.injectors.ProviderAdapter;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.config.Settings;
+import org.sonar.api.scan.filesystem.PathResolver;
+
+public class IndexedFileBuilderProvider extends ProviderAdapter {
+
+ public IndexedFileBuilder provide(ProjectDefinition def, PathResolver pathResolver, Settings settings, LanguageDetectionFactory langDetectionFactory) {
+ return new IndexedFileBuilder(def.getKeyWithBranch(), pathResolver, settings, langDetectionFactory.create());
+ }
+
+}
+++ /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.annotations.VisibleForTesting;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import javax.annotation.CheckForNull;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
-import org.sonar.api.config.Settings;
-import org.sonar.api.scan.filesystem.PathResolver;
-
-class InputFileBuilder {
-
- private static final Logger LOG = LoggerFactory.getLogger(InputFileBuilder.class);
-
- @VisibleForTesting
- static final Charset UTF_32BE = Charset.forName("UTF-32BE");
-
- @VisibleForTesting
- static final Charset UTF_32LE = Charset.forName("UTF-32LE");
-
- private final String moduleKey;
- private final PathResolver pathResolver;
- private final LanguageDetection langDetection;
- private final StatusDetection statusDetection;
- private final DefaultModuleFileSystem fs;
- private final Settings settings;
- private final FileMetadata fileMetadata;
-
- InputFileBuilder(String moduleKey, PathResolver pathResolver, LanguageDetection langDetection,
- StatusDetection statusDetection, DefaultModuleFileSystem fs, Settings settings, FileMetadata fileMetadata) {
- this.moduleKey = moduleKey;
- this.pathResolver = pathResolver;
- this.langDetection = langDetection;
- this.statusDetection = statusDetection;
- this.fs = fs;
- this.settings = settings;
- this.fileMetadata = fileMetadata;
- }
-
- String moduleKey() {
- return moduleKey;
- }
-
- PathResolver pathResolver() {
- return pathResolver;
- }
-
- LanguageDetection langDetection() {
- return langDetection;
- }
-
- StatusDetection statusDetection() {
- return statusDetection;
- }
-
- FileSystem fs() {
- return fs;
- }
-
- @CheckForNull
- DefaultInputFile create(File file) {
- String relativePath = pathResolver.relativePath(fs.baseDir(), file);
- if (relativePath == null) {
- LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", file.getAbsolutePath(), fs.baseDir());
- return null;
- }
- return new DefaultInputFile(moduleKey, relativePath);
- }
-
- /**
- * Optimization to not compute InputFile metadata if the file is excluded from analysis.
- */
- @CheckForNull
- DefaultInputFile completeAndComputeMetadata(DefaultInputFile inputFile, InputFile.Type type) {
- inputFile.setType(type);
- inputFile.setModuleBaseDir(fs.baseDir().toPath());
-
- String lang = langDetection.language(inputFile);
- if (lang == null && !settings.getBoolean(CoreProperties.IMPORT_UNKNOWN_FILES_KEY)) {
- // Return fast to skip costly metadata computation
- LOG.debug("'{}' language is not supported by any analyzer. Skipping it.", inputFile.relativePath());
- return null;
- }
- inputFile.setLanguage(lang);
-
- Charset charset = detectCharset(inputFile.file(), fs.encoding());
- inputFile.setCharset(charset);
-
- inputFile.initMetadata(fileMetadata.readMetadata(inputFile.file(), charset));
-
- inputFile.setStatus(statusDetection.status(inputFile.moduleKey(), inputFile.relativePath(), inputFile.hash()));
-
- return inputFile;
- }
-
- /**
- * @return charset detected from BOM in given file or given defaultCharset
- * @throws IllegalStateException if an I/O error occurs
- */
- private static Charset detectCharset(File file, Charset defaultCharset) {
- try (FileInputStream inputStream = new FileInputStream(file)) {
- byte[] bom = new byte[4];
- int n = inputStream.read(bom, 0, bom.length);
- if ((n >= 3) && (bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
- return StandardCharsets.UTF_8;
- } else if ((n >= 4) && (bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
- return UTF_32BE;
- } else if ((n >= 4) && (bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
- return UTF_32LE;
- } else if ((n >= 2) && (bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
- return StandardCharsets.UTF_16BE;
- } else if ((n >= 2) && (bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
- return StandardCharsets.UTF_16LE;
- } else {
- return defaultCharset;
- }
- } catch (IOException e) {
- throw new IllegalStateException("Unable to read file " + file.getAbsolutePath(), e);
- }
- }
-}
+++ /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.ScannerSide;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.fs.internal.FileMetadata;
-import org.sonar.api.config.Settings;
-import org.sonar.api.scan.filesystem.PathResolver;
-
-@ScannerSide
-public class InputFileBuilderFactory {
-
- private final String moduleKey;
- private final PathResolver pathResolver;
- private final LanguageDetectionFactory langDetectionFactory;
- private final StatusDetectionFactory statusDetectionFactory;
- private final Settings settings;
- private final FileMetadata fileMetadata;
-
- public InputFileBuilderFactory(ProjectDefinition def, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory,
- StatusDetectionFactory statusDetectionFactory, Settings settings, FileMetadata fileMetadata) {
- this.fileMetadata = fileMetadata;
- this.moduleKey = def.getKeyWithBranch();
- this.pathResolver = pathResolver;
- this.langDetectionFactory = langDetectionFactory;
- this.statusDetectionFactory = statusDetectionFactory;
- this.settings = settings;
- }
-
- InputFileBuilder create(DefaultModuleFileSystem fs) {
- return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, settings, fileMetadata);
- }
-}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
import org.sonar.api.batch.fs.internal.PathPattern;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
}
@CheckForNull
- String language(InputFile inputFile) {
+ String language(DefaultIndexedFile inputFile) {
String detectedLanguage = null;
for (String languageKey : languagesToConsider) {
if (isCandidateForLanguage(inputFile, languageKey)) {
return null;
}
- private boolean isCandidateForLanguage(InputFile inputFile, String languageKey) {
+ private boolean isCandidateForLanguage(DefaultIndexedFile inputFile, String languageKey) {
PathPattern[] patterns = patternsByLanguage.get(languageKey);
if (patterns != null) {
for (PathPattern pathPattern : patterns) {
--- /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.annotations.VisibleForTesting;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.fs.InputFile.Type;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.api.batch.fs.internal.Metadata;
+
+class MetadataGenerator {
+ private static final Logger LOG = LoggerFactory.getLogger(MetadataGenerator.class);
+ @VisibleForTesting
+ static final Charset UTF_32BE = Charset.forName("UTF-32BE");
+
+ @VisibleForTesting
+ static final Charset UTF_32LE = Charset.forName("UTF-32LE");
+
+ private final StatusDetection statusDetection;
+ private final FileMetadata fileMetadata;
+
+ MetadataGenerator(StatusDetection statusDetection, FileMetadata fileMetadata) {
+ this.statusDetection = statusDetection;
+ this.fileMetadata = fileMetadata;
+ }
+
+ /**
+ * @return charset detected from BOM in given file or given defaultCharset
+ * @throws IllegalStateException if an I/O error occurs
+ */
+ private static Charset detectCharset(File file, Charset defaultCharset) {
+ try (FileInputStream inputStream = new FileInputStream(file)) {
+ byte[] bom = new byte[4];
+ int n = inputStream.read(bom, 0, bom.length);
+ if ((n >= 3) && (bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
+ return StandardCharsets.UTF_8;
+ } else if ((n >= 4) && (bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
+ return UTF_32BE;
+ } else if ((n >= 4) && (bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
+ return UTF_32LE;
+ } else if ((n >= 2) && (bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
+ return StandardCharsets.UTF_16BE;
+ } else if ((n >= 2) && (bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
+ return StandardCharsets.UTF_16LE;
+ } else {
+ return defaultCharset;
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to read file " + file.getAbsolutePath(), e);
+ }
+ }
+
+ public Metadata readMetadata(final DefaultInputFile inputFile, Charset defaultEncoding) {
+ try {
+ Charset charset = detectCharset(inputFile.file(), defaultEncoding);
+ inputFile.setCharset(charset);
+ Metadata metadata = fileMetadata.readMetadata(inputFile.file(), charset);
+ inputFile.setStatus(statusDetection.status(inputFile.moduleKey(), inputFile.relativePath(), metadata.hash()));
+ LOG.debug("'{}' generated metadata {} with and charset '{}'",
+ inputFile.relativePath(), inputFile.type() == Type.TEST ? "as test " : "", charset);
+ return metadata;
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+}
--- /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.picocontainer.injectors.ProviderAdapter;
+import org.sonar.api.batch.ScannerSide;
+import org.sonar.api.batch.fs.internal.FileMetadata;
+
+@ScannerSide
+public class MetadataGeneratorProvider extends ProviderAdapter {
+ public MetadataGenerator provide(StatusDetectionFactory statusDetectionFactory, FileMetadata fileMetadata) {
+ return new MetadataGenerator(statusDetectionFactory.create(), fileMetadata);
+ }
+}
--- /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.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.InputFilePredicate;
+import org.sonar.scanner.repository.FileData;
+import org.sonar.scanner.repository.ProjectRepositories;
+
+public class SameInputFilePredicate implements InputFilePredicate {
+ private static final Logger LOG = LoggerFactory.getLogger(SameInputFilePredicate.class);
+ private final ProjectRepositories projectRepositories;
+ private final String moduleKey;
+
+ public SameInputFilePredicate(ProjectRepositories projectRepositories, String moduleKey) {
+ this.projectRepositories = projectRepositories;
+ this.moduleKey = moduleKey;
+ }
+
+ @Override
+ public boolean apply(InputFile inputFile) {
+ FileData fileDataPerPath = projectRepositories.fileData(moduleKey, inputFile.relativePath());
+ if (fileDataPerPath == null) {
+ // ADDED
+ return true;
+ }
+ String previousHash = fileDataPerPath.hash();
+ if (StringUtils.isEmpty(previousHash)) {
+ // ADDED
+ return true;
+ }
+
+ // this will trigger computation of metadata
+ String hash = ((DefaultInputFile) inputFile).hash();
+ if (StringUtils.equals(hash, previousHash)) {
+ // SAME
+ LOG.debug("'{}' filtering unmodified file", inputFile.relativePath());
+ return false;
+ }
+
+ // CHANGED
+ return true;
+ }
+
+}
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.measure.Metric;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage;
@Override
public void store(DefaultHighlighting highlighting) {
ScannerReportWriter writer = reportPublisher.getWriter();
- DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile();
+ InputFile inputFile = highlighting.inputFile();
int componentRef = componentCache.get(inputFile).batchId();
if (writer.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef)) {
throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile.absolutePath());
package org.sonar.scanner.source;
import org.sonar.api.batch.AnalysisMode;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.internal.SensorStorage;
public class DefaultHighlightable implements Highlightable {
private static final HighlightingBuilder NO_OP_BUILDER = new NoOpHighlightingBuilder();
- private final DefaultInputFile inputFile;
+ private final InputFile inputFile;
private final SensorStorage sensorStorage;
private final AnalysisMode analysisMode;
- public DefaultHighlightable(DefaultInputFile inputFile, SensorStorage sensorStorage, AnalysisMode analysisMode) {
+ public DefaultHighlightable(InputFile inputFile, SensorStorage sensorStorage, AnalysisMode analysisMode) {
this.inputFile = inputFile;
this.sensorStorage = sensorStorage;
this.analysisMode = analysisMode;
import java.util.Collections;
import java.util.List;
import org.sonar.api.batch.AnalysisMode;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.source.Symbol;
import org.sonar.api.source.Symbolizable;
}
}
- private final DefaultInputFile inputFile;
+ private final InputFile inputFile;
private final DefaultSensorStorage sensorStorage;
private final AnalysisMode analysisMode;
- public DefaultSymbolizable(DefaultInputFile inputFile, DefaultSensorStorage sensorStorage, AnalysisMode analysisMode) {
+ public DefaultSymbolizable(InputFile inputFile, DefaultSensorStorage sensorStorage, AnalysisMode analysisMode) {
this.inputFile = inputFile;
this.sensorStorage = sensorStorage;
this.analysisMode = analysisMode;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.source.Highlightable;
import org.sonar.scanner.deprecated.perspectives.PerspectiveBuilder;
public Highlightable loadPerspective(Class<Highlightable> perspectiveClass, InputComponent component) {
if (component.isFile()) {
InputFile path = (InputFile) component;
- return new DefaultHighlightable((DefaultInputFile) path, sensorStorage, analysisMode);
+ return new DefaultHighlightable(path, sensorStorage, analysisMode);
}
return null;
}
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.measures.CoreMetrics;
-@Phase(name = Phase.Name.PRE)
+@Phase(name = Phase.Name.POST)
public final class LinesSensor implements Sensor {
@Override
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.source.Symbolizable;
import org.sonar.scanner.deprecated.perspectives.PerspectiveBuilder;
import org.sonar.scanner.sensor.DefaultSensorStorage;
public Symbolizable loadPerspective(Class<Symbolizable> perspectiveClass, InputComponent component) {
if (component.isFile()) {
InputFile path = (InputFile) component;
- return new DefaultSymbolizable((DefaultInputFile) path, sensorStorage, analysisMode);
+ return new DefaultSymbolizable(path, sensorStorage, analysisMode);
}
return null;
}
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
when(metricFinder.<String>findByKey(CoreMetrics.EXECUTABLE_LINES_DATA_KEY)).thenReturn(CoreMetrics.EXECUTABLE_LINES_DATA);
when(metricFinder.<String>findByKey(CoreMetrics.COMMENT_LINES_DATA_KEY)).thenReturn(CoreMetrics.COMMENT_LINES_DATA);
measureCache = mock(MeasureCache.class);
- fileLineMeasures = new DefaultFileLinesContext(sensorContextTester, new DefaultInputFile("foo", "src/foo.php").initMetadata("Foo\nbar\nbiz"), metricFinder,
+ fileLineMeasures = new DefaultFileLinesContext(sensorContextTester, new TestInputFileBuilder("foo", "src/foo.php").initMetadata("Foo\nbar\nbiz").build(), metricFinder,
measureCache);
}
@Test
public void validateLineGreaterThanZero() {
- thrown.expectMessage("Line number should be positive for file [moduleKey=foo, relative=src/foo.php, basedir=null].");
+ thrown.expectMessage("Line number should be positive for file [moduleKey=foo, relative=src/foo.php, basedir=foo].");
fileLineMeasures.setIntValue(HITS_METRIC_KEY, 0, 2);
}
@Test
public void validateLineLowerThanLineCount() {
- thrown.expectMessage("Line 4 is out of range for file [moduleKey=foo, relative=src/foo.php, basedir=null]. File has 3 lines");
+ thrown.expectMessage("Line 4 is out of range for file [moduleKey=foo, relative=src/foo.php, basedir=foo]. File has 3 lines");
fileLineMeasures.setIntValue(HITS_METRIC_KEY, 4, 2);
}
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
private BatchComponent createComponent(String relativePath, int lines) {
org.sonar.api.resources.Resource sampleFile = org.sonar.api.resources.File.create("relativePath").setEffectiveKey("foo:" + relativePath);
return componentCache.add(sampleFile, null)
- .setInputComponent(new DefaultInputFile("foo", relativePath)
+ .setInputComponent(new TestInputFileBuilder("foo", relativePath)
.setModuleBaseDir(baseDir.toPath())
- .setLines(lines));
+ .setLines(lines)
+ .build());
}
@Test
import org.mockito.MockitoAnnotations;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
import org.sonar.duplications.block.Block;
private Settings settings;
private JavaCpdBlockIndexer engine;
- private DefaultInputFile file;
+ private InputFile file;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
File baseDir = temp.newFolder();
DefaultFileSystem fs = new DefaultFileSystem(baseDir);
- file = new DefaultInputFile("foo", "src/ManyStatements.java").setLanguage(JAVA);
+ file = new TestInputFileBuilder("foo", "src/ManyStatements.java").setLanguage(JAVA).build();
fs.add(file);
BatchComponentCache batchComponentCache = new BatchComponentCache();
batchComponentCache.add(org.sonar.api.resources.File.create("src/Foo.java").setEffectiveKey("foo:src/ManyStatements.java"), null).setInputComponent(file);
import org.junit.Test;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import static org.assertj.core.api.Assertions.assertThat;
}
private DefaultInputFile setupFile(String path) {
- return new DefaultInputFile(context.module().key(), path)
+ return new TestInputFileBuilder(context.module().key(), path)
.setLanguage("bla")
.setType(InputFile.Type.TEST)
- .initMetadata("1\n2\n3\n4\n5\n6");
+ .initMetadata("1\n2\n3\n4\n5\n6")
+ .build();
}
}
import org.junit.Test;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.test.MutableTestCase;
import org.sonar.api.test.MutableTestPlan;
}
private DefaultInputFile setupFile(String path) {
- return new DefaultInputFile(context.module().key(), path)
+ return new TestInputFileBuilder(context.module().key(), path)
.setLanguage("bla")
.setType(InputFile.Type.TEST)
- .initMetadata("1\n2\n3\n4\n5\n6");
+ .initMetadata("1\n2\n3\n4\n5\n6")
+ .build();
}
private MutableTestPlan mockMutableTestPlan(MutableTestCase testCase) {
package org.sonar.scanner.issue;
import org.junit.Test;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.issue.Issuable;
import org.sonar.scanner.DefaultProjectTree;
import org.sonar.scanner.sensor.DefaultSensorContext;
@Test
public void file_should_be_issuable() {
IssuableFactory factory = new IssuableFactory(mock(DefaultSensorContext.class));
- Issuable issuable = factory.loadPerspective(Issuable.class, new DefaultInputFile("foo", "src/Foo.java"));
+ Issuable issuable = factory.loadPerspective(Issuable.class, new TestInputFileBuilder("foo", "src/Foo.java").build());
assertThat(issuable).isNotNull();
assertThat(issuable.issues()).isEmpty();
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.RulesBuilder;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
ModuleIssues moduleIssues;
BatchComponentCache componentCache = new BatchComponentCache();
- InputFile file = new DefaultInputFile("foo", "src/Foo.php").initMetadata("Foo\nBar\nBiz\n");
+ InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").initMetadata("Foo\nBar\nBiz\n").build();
ReportPublisher reportPublisher = mock(ReportPublisher.class, RETURNS_DEEP_STUBS);
@Before
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
import org.sonar.scanner.issue.ignore.pattern.IssueInclusionPatternInitializer;
import org.sonar.scanner.issue.ignore.pattern.PatternMatcher;
@Test
public void shouldAnalyzeProject() throws IOException {
File javaFile1 = new File(baseDir, "src/main/java/Foo.java");
- fs.add(new DefaultInputFile("polop", "src/main/java/Foo.java")
- .setType(InputFile.Type.MAIN));
+ fs.add(new TestInputFileBuilder("polop", "src/main/java/Foo.java")
+ .setModuleBaseDir(baseDir.toPath())
+ .setType(InputFile.Type.MAIN)
+ .build());
File javaTestFile1 = new File(baseDir, "src/test/java/FooTest.java");
- fs.add(new DefaultInputFile("polop", "src/test/java/FooTest.java")
- .setType(InputFile.Type.TEST));
+ fs.add(new TestInputFileBuilder("polop", "src/test/java/FooTest.java")
+ .setModuleBaseDir(baseDir.toPath())
+ .setType(InputFile.Type.TEST)
+ .build());
when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
@Test
public void shouldAnalyseFilesOnlyWhenRegexConfigured() {
- File javaFile1 = new File(baseDir, "src/main/java/Foo.java");
- fs.add(new DefaultInputFile("polop", "src/main/java/Foo.java")
- .setType(InputFile.Type.MAIN));
- File javaTestFile1 = new File(baseDir, "src/test/java/FooTest.java");
- fs.add(new DefaultInputFile("polop", "src/test/java/FooTest.java")
- .setType(InputFile.Type.TEST));
+ fs.add(new TestInputFileBuilder("polop", "src/main/java/Foo.java")
+ .setType(InputFile.Type.MAIN)
+ .build());
+ fs.add(new TestInputFileBuilder("polop", "src/test/java/FooTest.java")
+ .setType(InputFile.Type.TEST)
+ .build());
when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(false);
scanner.execute();
@Test
public void shouldReportFailure() throws IOException {
File phpFile1 = new File(baseDir, "src/Foo.php");
- fs.add(new DefaultInputFile("polop", "src/Foo.php")
- .setType(InputFile.Type.MAIN));
+ fs.add(new TestInputFileBuilder("polop", "src/Foo.php")
+ .setModuleBaseDir(baseDir.toPath())
+ .setType(InputFile.Type.MAIN)
+ .build());
when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
doThrow(new IOException("BUG")).when(regexpScanner).scan("polop:src/Foo.php", phpFile1, UTF_8);
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputDir;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.DateUtils;
org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", FakeJava.INSTANCE, false);
file.setEffectiveKey("module1:src/Foo.java");
file.setId(4).setUuid("FILE_UUID");
- resourceCache.add(file, dir).setInputComponent(new DefaultInputFile("module1", "src/Foo.java").setLines(2));
+ resourceCache.add(file, dir).setInputComponent(new TestInputFileBuilder("module1", "src/Foo.java").setLines(2).build());
org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", null, false);
fileWithoutLang.setEffectiveKey("module1:src/make");
fileWithoutLang.setId(5).setUuid("FILE_WITHOUT_LANG_UUID");
- resourceCache.add(fileWithoutLang, dir).setInputComponent(new DefaultInputFile("module1", "src/make").setLines(10));
+ resourceCache.add(fileWithoutLang, dir).setInputComponent(new TestInputFileBuilder("module1", "src/make").setLines(10).build());
org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", FakeJava.INSTANCE, true);
testFile.setEffectiveKey("module1:test/FooTest.java");
testFile.setId(6).setUuid("TEST_FILE_UUID");
- resourceCache.add(testFile, dir).setInputComponent(new DefaultInputFile("module1", "test/FooTest.java").setLines(4));
+ resourceCache.add(testFile, dir).setInputComponent(new TestInputFileBuilder("module1", "test/FooTest.java").setLines(4).build());
ImmutableProjectReactor reactor = new ImmutableProjectReactor(rootDef);
assertThat(module1Protobuf.getDescription()).isEqualTo("Module description");
assertThat(module1Protobuf.getVersion()).isEqualTo("1.0");
}
-
+
@Test
public void add_components_without_version_and_name() throws IOException {
ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo");
org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", FakeJava.INSTANCE, false);
file.setEffectiveKey("module1:src/Foo.java");
file.setId(4).setUuid("FILE_UUID");
- resourceCache.add(file, dir).setInputComponent(new DefaultInputFile("module1", "src/Foo.java").setLines(2));
+ resourceCache.add(file, dir).setInputComponent(new TestInputFileBuilder("module1", "src/Foo.java").setLines(2).build());
org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", null, false);
fileWithoutLang.setEffectiveKey("module1:src/make");
fileWithoutLang.setId(5).setUuid("FILE_WITHOUT_LANG_UUID");
- resourceCache.add(fileWithoutLang, dir).setInputComponent(new DefaultInputFile("module1", "src/make").setLines(10));
+ resourceCache.add(fileWithoutLang, dir).setInputComponent(new TestInputFileBuilder("module1", "src/make").setLines(10).build());
org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", FakeJava.INSTANCE, true);
testFile.setEffectiveKey("module1:test/FooTest.java");
testFile.setId(6).setUuid("TEST_FILE_UUID");
- resourceCache.add(testFile, dir).setInputComponent(new DefaultInputFile("module1", "test/FooTest.java").setLines(4));
+ resourceCache.add(testFile, dir).setInputComponent(new TestInputFileBuilder("module1", "test/FooTest.java").setLines(4).build());
ImmutableProjectReactor reactor = new ImmutableProjectReactor(rootDef);
org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", FakeJava.INSTANCE, false);
file.setEffectiveKey("module1:my_branch:my_branch:src/Foo.java");
file.setId(4).setUuid("FILE_UUID");
- resourceCache.add(file, dir).setInputComponent(new DefaultInputFile("module1", "src/Foo.java").setLines(2));
+ resourceCache.add(file, dir).setInputComponent(new TestInputFileBuilder("module1", "src/Foo.java").setLines(2).build());
ImmutableProjectReactor reactor = new ImmutableProjectReactor(rootDef);
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.Project;
BatchComponentCache resourceCache = new BatchComponentCache();
sampleFile = org.sonar.api.resources.File.create("src/Foo.php").setEffectiveKey("foo:src/Foo.php");
resourceCache.add(p, null).setInputComponent(new DefaultInputModule("foo"));
- resourceCache.add(sampleFile, null).setInputComponent(new DefaultInputFile("foo", "src/Foo.php").setLines(5));
+ resourceCache.add(sampleFile, null).setInputComponent(new TestInputFileBuilder("foo", "src/Foo.php").setLines(5).build());
measureCache = mock(MeasureCache.class);
when(measureCache.byMetric(anyString(), anyString())).thenReturn(null);
publisher = new CoveragePublisher(resourceCache, measureCache);
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.Project;
BatchComponentCache resourceCache = new BatchComponentCache();
sampleFile = org.sonar.api.resources.File.create("src/Foo.php").setEffectiveKey(FILE_KEY);
resourceCache.add(p, null).setInputComponent(new DefaultInputModule("foo"));
- resourceCache.add(sampleFile, null).setInputComponent(new DefaultInputFile("foo", "src/Foo.php"));
+ resourceCache.add(sampleFile, null).setInputComponent(new TestInputFileBuilder("foo", "src/Foo.php").build());
measureCache = mock(MeasureCache.class);
when(measureCache.byComponentKey(anyString())).thenReturn(Collections.<DefaultMeasure<?>>emptyList());
publisher = new MeasuresPublisher(resourceCache, measureCache, mock(TestPlanBuilder.class));
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Qualifiers;
import org.sonar.scanner.index.BatchComponentCache;
File baseDir = temp.newFolder();
sourceFile = new File(baseDir, "src/Foo.php");
resourceCache.add(sampleFile, null).setInputComponent(
- new DefaultInputFile("foo", "src/Foo.php").setLines(5).setModuleBaseDir(baseDir.toPath()).setCharset(StandardCharsets.ISO_8859_1));
+ new TestInputFileBuilder("foo", "src/Foo.php")
+ .setLines(5)
+ .setModuleBaseDir(baseDir.toPath())
+ .setCharset(StandardCharsets.ISO_8859_1)
+ .build());
publisher = new SourcePublisher(resourceCache);
File outputDir = temp.newFolder();
writer = new ScannerReportWriter(outputDir);
import org.junit.Test;
import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.IndexedFile;
+import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
import org.sonar.scanner.scan.filesystem.AdditionalFilePredicates;
import static org.assertj.core.api.Assertions.assertThat;
+import java.nio.file.Paths;
+
public class AdditionalFilePredicatesTest {
@Test
public void key() {
FilePredicate predicate = new AdditionalFilePredicates.KeyPredicate("struts:Action.java");
- DefaultInputFile inputFile = new DefaultInputFile("struts", "Action.java");
- assertThat(predicate.apply(inputFile)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("struts", Paths.get("module"), "Action.java");
+ assertThat(predicate.apply(indexedFile)).isTrue();
- inputFile = new DefaultInputFile("struts", "Filter.java");
- assertThat(predicate.apply(inputFile)).isFalse();
+ indexedFile = new DefaultIndexedFile("struts", Paths.get("module"), "Filter.java");
+ assertThat(predicate.apply(indexedFile)).isFalse();
}
}
import org.sonar.api.batch.fs.InputFile.Status;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.resources.AbstractLanguage;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.Languages;
import org.sonar.scanner.index.BatchComponent;
import org.sonar.scanner.index.BatchComponentCache;
import org.sonar.scanner.index.DefaultIndex;
+import org.sonar.scanner.repository.ProjectRepositories;
import org.sonar.scanner.scan.filesystem.ComponentIndexer;
import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem;
import org.sonar.scanner.scan.filesystem.FileIndexer;
public void should_index_java_files() throws IOException {
Languages languages = new Languages(FakeJava.INSTANCE);
ComponentIndexer indexer = createIndexer(languages);
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(project, null, mock(FileIndexer.class), initializer, indexer, mode);
+ DefaultModuleFileSystem fs = new DefaultModuleFileSystem(project, null, mock(FileIndexer.class), initializer, indexer, mode, new ProjectRepositories());
fs.add(newInputFile("src/main/java/foo/bar/Foo.java", "", "foo/bar/Foo.java", "java", false, Status.ADDED));
fs.add(newInputFile("src/main/java2/foo/bar/Foo.java", "", "foo/bar/Foo.java", "java", false, Status.ADDED));
// should index even if filter is applied
public void should_index_cobol_files() throws IOException {
Languages languages = new Languages(cobolLanguage);
ComponentIndexer indexer = createIndexer(languages);
- DefaultModuleFileSystem fs = new DefaultModuleFileSystem(project, null, mock(FileIndexer.class), initializer, indexer, mode);
+ DefaultModuleFileSystem fs = new DefaultModuleFileSystem(project, null, mock(FileIndexer.class), initializer, indexer, mode, new ProjectRepositories());
fs.add(newInputFile("src/foo/bar/Foo.cbl", "", "foo/bar/Foo.cbl", "cobol", false, Status.ADDED));
fs.add(newInputFile("src2/foo/bar/Foo.cbl", "", "foo/bar/Foo.cbl", "cobol", false, Status.ADDED));
fs.add(newInputFile("src/test/foo/bar/FooTest.cbl", "", "foo/bar/FooTest.cbl", "cobol", true, Status.ADDED));
private DefaultInputFile newInputFile(String path, String content, String sourceRelativePath, String languageKey, boolean unitTest, InputFile.Status status) throws IOException {
File file = new File(baseDir, path);
FileUtils.write(file, content);
- return new DefaultInputFile("foo", path)
+ return new TestInputFileBuilder("foo", path)
.setLanguage(languageKey)
.setType(unitTest ? InputFile.Type.TEST : InputFile.Type.MAIN)
- .setStatus(status);
+ .setStatus(status)
+ .build();
}
}
*/
package org.sonar.scanner.scan.filesystem;
+import static org.assertj.core.api.Assertions.assertThat;
+
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
+
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.fs.IndexedFile;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.config.Settings;
+import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
import org.sonar.api.config.MapSettings;
+import org.sonar.api.config.Settings;
import org.sonar.api.scan.filesystem.FileExclusions;
-import static org.assertj.core.api.Assertions.assertThat;
-
public class ExclusionFiltersTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ private Path moduleBaseDir;
+
+ @Before
+ public void setUp() throws IOException {
+ moduleBaseDir = temp.newFolder().toPath();
+ }
@Test
public void no_inclusions_nor_exclusions() throws IOException {
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(new MapSettings()));
filter.prepare();
- java.io.File file = temp.newFile();
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue();
+ assertThat(filter.accept(indexedFile, InputFile.Type.TEST)).isTrue();
}
@Test
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
filter.prepare();
- java.io.File file = temp.newFile();
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue();
- inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
+ indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/Foo.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse();
}
@Test
filter.prepare();
- java.io.File file = temp.newFile();
-
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
+ IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/Foo.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse();
- inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDto.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
+ indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDto.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue();
}
@Test
filter.prepare();
- java.io.File file = temp.newFile();
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
+ IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse();
- inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
+ indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/Foo.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue();
// source exclusions do not apply to tests
- inputFile = new DefaultInputFile("foo", "src/test/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue();
+ indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/test/java/com/mycompany/FooDao.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.TEST)).isTrue();
}
@Test
public void match_exclusion_by_absolute_path() throws IOException {
- File baseDir = temp.newFile();
- File excludedFile = new File(baseDir, "src/main/java/org/bar/Bar.java");
+ File excludedFile = new File(moduleBaseDir.toString(), "src/main/java/org/bar/Bar.java");
Settings settings = new MapSettings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "src/main/java/**/*");
filter.prepare();
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/org/bar/Foo.java").setModuleBaseDir(baseDir.toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
+ IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/org/bar/Foo.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isTrue();
- inputFile = new DefaultInputFile("foo", "src/main/java/org/bar/Bar.java").setModuleBaseDir(baseDir.toPath());
- assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
+ indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/org/bar/Bar.java");
+ assertThat(filter.accept(indexedFile, InputFile.Type.MAIN)).isFalse();
}
@Test
+++ /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.junit.Test;
-import org.mockito.Mockito;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.fs.internal.FileMetadata;
-import org.sonar.api.config.MapSettings;
-import org.sonar.api.scan.filesystem.PathResolver;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
-public class InputFileBuilderFactoryTest {
- @Test
- public void create_builder() {
- PathResolver pathResolver = new PathResolver();
- LanguageDetectionFactory langDetectionFactory = mock(LanguageDetectionFactory.class, Mockito.RETURNS_MOCKS);
- StatusDetectionFactory statusDetectionFactory = mock(StatusDetectionFactory.class, Mockito.RETURNS_MOCKS);
- DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class);
-
- InputFileBuilderFactory factory = new InputFileBuilderFactory(ProjectDefinition.create().setKey("struts"), pathResolver, langDetectionFactory,
- statusDetectionFactory, new MapSettings(), new FileMetadata());
- InputFileBuilder builder = factory.create(fs);
-
- assertThat(builder.langDetection()).isNotNull();
- assertThat(builder.statusDetection()).isNotNull();
- assertThat(builder.pathResolver()).isSameAs(pathResolver);
- assertThat(builder.fs()).isSameAs(fs);
- assertThat(builder.moduleKey()).isEqualTo("struts");
- }
-}
+++ /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 java.io.File;
-import java.io.FileNotFoundException;
-import java.nio.charset.StandardCharsets;
-import org.apache.commons.io.FileUtils;
-import org.junit.Assert;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
-import org.sonar.api.config.MapSettings;
-import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.utils.PathUtils;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class InputFileBuilderTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- LanguageDetection langDetection = mock(LanguageDetection.class);
- StatusDetection statusDetection = mock(StatusDetection.class);
- DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class);
-
- @Test
- public void should_detect_charset_from_BOM() {
- File basedir = new File("src/test/resources/org/sonar/scanner/scan/filesystem/");
- when(fs.baseDir()).thenReturn(basedir);
- when(fs.encoding()).thenReturn(StandardCharsets.US_ASCII);
- when(langDetection.language(any(InputFile.class))).thenReturn("java");
- InputFileBuilder builder = new InputFileBuilder("moduleKey", new PathResolver(), langDetection, statusDetection, fs, new MapSettings(), new FileMetadata());
-
- assertThat(createAndComplete(builder, new File(basedir, "without_BOM.txt")).charset())
- .isEqualTo(StandardCharsets.US_ASCII);
- assertThat(createAndComplete(builder, new File(basedir, "UTF-8.txt")).charset())
- .isEqualTo(StandardCharsets.UTF_8);
- assertThat(createAndComplete(builder, new File(basedir, "UTF-16BE.txt")).charset())
- .isEqualTo(StandardCharsets.UTF_16BE);
- assertThat(createAndComplete(builder, new File(basedir, "UTF-16LE.txt")).charset())
- .isEqualTo(StandardCharsets.UTF_16LE);
- assertThat(createAndComplete(builder, new File(basedir, "UTF-32BE.txt")).charset())
- .isEqualTo(InputFileBuilder.UTF_32BE);
- assertThat(createAndComplete(builder, new File(basedir, "UTF-32LE.txt")).charset())
- .isEqualTo(InputFileBuilder.UTF_32LE);
-
- try {
- createAndComplete(builder, new File(basedir, "non_existing"));
- Assert.fail();
- } catch (IllegalStateException e) {
- assertThat(e.getMessage()).isEqualTo("Unable to read file " + new File(basedir, "non_existing").getAbsolutePath());
- assertThat(e.getCause()).isInstanceOf(FileNotFoundException.class);
- }
- }
-
- private static DefaultInputFile createAndComplete(InputFileBuilder builder, File file) {
- DefaultInputFile inputFile = builder.create(file);
- builder.completeAndComputeMetadata(inputFile, InputFile.Type.MAIN);
- return inputFile;
- }
-
- @Test
- public void complete_input_file() throws Exception {
- // file system
- File basedir = temp.newFolder();
- File srcFile = new File(basedir, "src/main/java/foo/Bar.java");
- FileUtils.touch(srcFile);
- FileUtils.write(srcFile, "single line");
- when(fs.baseDir()).thenReturn(basedir);
- when(fs.encoding()).thenReturn(StandardCharsets.UTF_8);
-
- // lang
- when(langDetection.language(any(InputFile.class))).thenReturn("java");
-
- // status
- when(statusDetection.status("foo", "src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a"))
- .thenReturn(InputFile.Status.ADDED);
-
- InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, new MapSettings(), new FileMetadata());
- DefaultInputFile inputFile = builder.create(srcFile);
- builder.completeAndComputeMetadata(inputFile, InputFile.Type.MAIN);
-
- assertThat(inputFile.type()).isEqualTo(InputFile.Type.MAIN);
- assertThat(inputFile.file()).isEqualTo(srcFile.getAbsoluteFile());
- assertThat(inputFile.absolutePath()).isEqualTo(PathUtils.sanitize(srcFile.getAbsolutePath()));
- assertThat(inputFile.language()).isEqualTo("java");
- assertThat(inputFile.key()).isEqualTo("struts:src/main/java/foo/Bar.java");
- assertThat(inputFile.relativePath()).isEqualTo("src/main/java/foo/Bar.java");
- assertThat(inputFile.lines()).isEqualTo(1);
- }
-
- @Test
- public void return_null_if_file_outside_basedir() throws Exception {
- // file system
- File basedir = temp.newFolder();
- File otherDir = temp.newFolder();
- File srcFile = new File(otherDir, "src/main/java/foo/Bar.java");
- FileUtils.touch(srcFile);
- when(fs.baseDir()).thenReturn(basedir);
-
- InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, new MapSettings(), new FileMetadata());
- DefaultInputFile inputFile = builder.create(srcFile);
-
- assertThat(inputFile).isNull();
- }
-
- @Test
- public void return_null_if_language_not_detected() throws Exception {
- // file system
- File basedir = temp.newFolder();
- File srcFile = new File(basedir, "src/main/java/foo/Bar.java");
- FileUtils.touch(srcFile);
- FileUtils.write(srcFile, "single line");
- when(fs.baseDir()).thenReturn(basedir);
- when(fs.encoding()).thenReturn(StandardCharsets.UTF_8);
-
- // lang
- when(langDetection.language(any(InputFile.class))).thenReturn(null);
-
- InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, new MapSettings(), new FileMetadata());
- DefaultInputFile inputFile = builder.create(srcFile);
- inputFile = builder.completeAndComputeMetadata(inputFile, InputFile.Type.MAIN);
-
- assertThat(inputFile).isNull();
- }
-
-}
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.InputPath;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.scanner.scan.filesystem.InputPathCache;
import java.nio.charset.StandardCharsets;
@Test
public void should_add_input_file() throws Exception {
InputPathCache cache = new InputPathCache();
- DefaultInputFile fooFile = new DefaultInputFile("foo", "src/main/java/Foo.java").setModuleBaseDir(temp.newFolder().toPath());
+ DefaultInputFile fooFile = new TestInputFileBuilder("foo", "src/main/java/Foo.java").setModuleBaseDir(temp.newFolder().toPath()).build();
cache.put("struts", fooFile);
- cache.put("struts-core", new DefaultInputFile("foo", "src/main/java/Bar.java")
+ cache.put("struts-core", new TestInputFileBuilder("foo", "src/main/java/Bar.java")
.setLanguage("bla")
.setType(Type.MAIN)
.setStatus(Status.ADDED)
.setLines(2)
.setCharset(StandardCharsets.UTF_8)
- .setModuleBaseDir(temp.newFolder().toPath()));
+ .setModuleBaseDir(temp.newFolder().toPath())
+ .build());
DefaultInputFile loadedFile = (DefaultInputFile) cache.getFile("struts-core", "src/main/java/Bar.java");
assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java");
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
import org.sonar.api.resources.Language;
LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob")));
LanguageDetection detection = new LanguageDetection(new MapSettings(), languages);
- assertThat(detection.language(newInputFile("Foo.java"))).isEqualTo("java");
- assertThat(detection.language(newInputFile("src/Foo.java"))).isEqualTo("java");
- assertThat(detection.language(newInputFile("Foo.JAVA"))).isEqualTo("java");
- assertThat(detection.language(newInputFile("Foo.jav"))).isEqualTo("java");
- assertThat(detection.language(newInputFile("Foo.Jav"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("Foo.java"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("src/Foo.java"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("Foo.JAVA"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("Foo.jav"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("Foo.Jav"))).isEqualTo("java");
- assertThat(detection.language(newInputFile("abc.cbl"))).isEqualTo("cobol");
- assertThat(detection.language(newInputFile("abc.CBL"))).isEqualTo("cobol");
+ assertThat(detection.language(newIndexedFile("abc.cbl"))).isEqualTo("cobol");
+ assertThat(detection.language(newIndexedFile("abc.CBL"))).isEqualTo("cobol");
- assertThat(detection.language(newInputFile("abc.php"))).isNull();
- assertThat(detection.language(newInputFile("abc"))).isNull();
+ assertThat(detection.language(newIndexedFile("abc.php"))).isNull();
+ assertThat(detection.language(newIndexedFile("abc"))).isNull();
}
@Test
public void should_not_fail_if_no_language() throws Exception {
LanguageDetection detection = spy(new LanguageDetection(new MapSettings(), new DefaultLanguagesRepository(new Languages())));
- assertThat(detection.language(newInputFile("Foo.java"))).isNull();
+ assertThat(detection.language(newIndexedFile("Foo.java"))).isNull();
}
@Test
LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("abap", "abap", "ABAP")));
LanguageDetection detection = new LanguageDetection(new MapSettings(), languages);
- assertThat(detection.language(newInputFile("abc.abap"))).isEqualTo("abap");
+ assertThat(detection.language(newIndexedFile("abc.abap"))).isEqualTo("abap");
}
@Test
// No side-effect on non-ABAP projects
LanguageDetection detection = new LanguageDetection(new MapSettings(), languages);
- assertThat(detection.language(newInputFile("abc"))).isNull();
- assertThat(detection.language(newInputFile("abc.abap"))).isNull();
- assertThat(detection.language(newInputFile("abc.java"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("abc"))).isNull();
+ assertThat(detection.language(newIndexedFile("abc.abap"))).isNull();
+ assertThat(detection.language(newIndexedFile("abc.java"))).isEqualTo("java");
Settings settings = new MapSettings();
settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "abap");
detection = new LanguageDetection(settings, languages);
- assertThat(detection.language(newInputFile("abc"))).isEqualTo("abap");
- assertThat(detection.language(newInputFile("abc.txt"))).isEqualTo("abap");
- assertThat(detection.language(newInputFile("abc.java"))).isEqualTo("abap");
+ assertThat(detection.language(newIndexedFile("abc"))).isEqualTo("abap");
+ assertThat(detection.language(newIndexedFile("abc.txt"))).isEqualTo("abap");
+ assertThat(detection.language(newIndexedFile("abc.java"))).isEqualTo("abap");
}
@Test
Settings settings = new MapSettings();
settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "java");
LanguageDetection detection = new LanguageDetection(settings, languages);
- assertThat(detection.language(newInputFile("abc"))).isNull();
- assertThat(detection.language(newInputFile("abc.php"))).isNull();
- assertThat(detection.language(newInputFile("abc.java"))).isEqualTo("java");
- assertThat(detection.language(newInputFile("src/abc.java"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("abc"))).isNull();
+ assertThat(detection.language(newIndexedFile("abc.php"))).isNull();
+ assertThat(detection.language(newIndexedFile("abc.java"))).isEqualTo("java");
+ assertThat(detection.language(newIndexedFile("src/abc.java"))).isEqualTo("java");
}
@Test
LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml")));
LanguageDetection detection = new LanguageDetection(new MapSettings(), languages);
try {
- detection.language(newInputFile("abc.xhtml"));
+ detection.language(newIndexedFile("abc.xhtml"));
fail();
} catch (MessageException e) {
assertThat(e.getMessage())
settings.setProperty("sonar.lang.patterns.xml", "xml/**");
settings.setProperty("sonar.lang.patterns.web", "web/**");
LanguageDetection detection = new LanguageDetection(settings, languages);
- assertThat(detection.language(newInputFile("xml/abc.xhtml"))).isEqualTo("xml");
- assertThat(detection.language(newInputFile("web/abc.xhtml"))).isEqualTo("web");
+ assertThat(detection.language(newIndexedFile("xml/abc.xhtml"))).isEqualTo("xml");
+ assertThat(detection.language(newIndexedFile("web/abc.xhtml"))).isEqualTo("web");
}
@Test
LanguageDetection detection = new LanguageDetection(settings, languages);
- assertThat(detection.language(newInputFile("abc.abap"))).isEqualTo("abap");
- assertThat(detection.language(newInputFile("abc.cobol"))).isEqualTo("cobol");
+ assertThat(detection.language(newIndexedFile("abc.abap"))).isEqualTo("abap");
+ assertThat(detection.language(newIndexedFile("abc.cobol"))).isEqualTo("cobol");
try {
- detection.language(newInputFile("abc.txt"));
+ detection.language(newIndexedFile("abc.txt"));
fail();
} catch (MessageException e) {
assertThat(e.getMessage())
}
}
- private InputFile newInputFile(String path) throws IOException {
+ private DefaultIndexedFile newIndexedFile(String path) throws IOException {
File basedir = temp.newFolder();
- return new DefaultInputFile("foo", path).setModuleBaseDir(basedir.toPath());
+ return new DefaultIndexedFile("foo", basedir.toPath(), path);
}
static class MockLanguage implements Language {
--- /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.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.api.batch.fs.internal.FileMetadata;
+
+import static org.mockito.Mockito.mock;
+
+public class MetadataGeneratorProviderTest {
+ @Test
+ public void create_builder() {
+ StatusDetectionFactory statusDetectionFactory = mock(StatusDetectionFactory.class, Mockito.RETURNS_MOCKS);
+
+ MetadataGeneratorProvider factory = new MetadataGeneratorProvider();
+ MetadataGenerator builder = factory.provide(statusDetectionFactory, new FileMetadata());
+ //TODO
+ }
+}
--- /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 static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.api.batch.fs.internal.Metadata;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.api.config.MapSettings;
+import org.sonar.api.scan.filesystem.PathResolver;
+import org.sonar.api.utils.PathUtils;
+
+public class MetadataGeneratorTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Mock
+ private StatusDetection statusDetection;
+ @Mock
+ private DefaultModuleFileSystem fs;
+ @Mock
+ private FileMetadata metadata;
+
+ private MetadataGenerator builder;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ builder = new MetadataGenerator(statusDetection, metadata);
+ // TODO
+ }
+ /*
+ * @Test
+ * public void should_detect_charset_from_BOM() {
+ * File basedir = new File("src/test/resources/org/sonar/scanner/scan/filesystem/");
+ * when(fs.baseDir()).thenReturn(basedir);
+ * when(fs.encoding()).thenReturn(StandardCharsets.US_ASCII);
+ *
+ * assertThat(createAndComplete(builder, new File(basedir, "without_BOM.txt")).charset())
+ * .isEqualTo(StandardCharsets.US_ASCII);
+ * assertThat(createAndComplete(builder, new File(basedir, "UTF-8.txt")).charset())
+ * .isEqualTo(StandardCharsets.UTF_8);
+ * assertThat(createAndComplete(builder, new File(basedir, "UTF-16BE.txt")).charset())
+ * .isEqualTo(StandardCharsets.UTF_16BE);
+ * assertThat(createAndComplete(builder, new File(basedir, "UTF-16LE.txt")).charset())
+ * .isEqualTo(StandardCharsets.UTF_16LE);
+ * assertThat(createAndComplete(builder, new File(basedir, "UTF-32BE.txt")).charset())
+ * .isEqualTo(InputFileBuilder.UTF_32BE);
+ * assertThat(createAndComplete(builder, new File(basedir, "UTF-32LE.txt")).charset())
+ * .isEqualTo(InputFileBuilder.UTF_32LE);
+ *
+ * try {
+ * createAndComplete(builder, new File(basedir, "non_existing"));
+ * Assert.fail();
+ * } catch (IllegalStateException e) {
+ * assertThat(e.getMessage()).isEqualTo("Unable to read file " + new File(basedir, "non_existing").getAbsolutePath());
+ * assertThat(e.getCause()).isInstanceOf(FileNotFoundException.class);
+ * }
+ * }
+ *
+ * private static DefaultInputFile createAndComplete(InputFileBuilder builder, File file) {
+ * DefaultInputFile inputFile = new TestInputFileBuilder("module", file.toString())
+ * .setMetadata()
+ * .build();
+ * return inputFile;
+ * }
+ *
+ * @Test
+ * public void complete_input_file() throws Exception {
+ * // file system
+ * File basedir = temp.newFolder();
+ * File srcFile = new File(basedir, "src/main/java/foo/Bar.java");
+ * FileUtils.touch(srcFile);
+ * FileUtils.write(srcFile, "single line");
+ * when(fs.baseDir()).thenReturn(basedir);
+ * when(fs.encoding()).thenReturn(StandardCharsets.UTF_8);
+ *
+ * // status
+ * when(statusDetection.status("foo", "src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a"))
+ * .thenReturn(InputFile.Status.ADDED);
+ *
+ * Metadata metadata = builder.readMetadata(srcFile);
+ * builder.readMetadata(inputFile);
+ *
+ * assertThat(inputFile.type()).isEqualTo(InputFile.Type.MAIN);
+ * assertThat(inputFile.file()).isEqualTo(srcFile.getAbsoluteFile());
+ * assertThat(inputFile.absolutePath()).isEqualTo(PathUtils.sanitize(srcFile.getAbsolutePath()));
+ * assertThat(inputFile.language()).isEqualTo("java");
+ * assertThat(inputFile.key()).isEqualTo("struts:src/main/java/foo/Bar.java");
+ * assertThat(inputFile.relativePath()).isEqualTo("src/main/java/foo/Bar.java");
+ * assertThat(inputFile.lines()).isEqualTo(1);
+ * }
+ *
+ * @Test
+ * public void set_metadata() throws Exception {
+ * DefaultInputFile inputFile = new TestInputFileBuilder("module", "src/main/java/foo/Bar.java").build();
+ * when()
+ *
+ * Metadata metadata = builder.readMetadata(inputFile);
+ *
+ * }
+ *
+ * @Test
+ * public void return_null_if_language_not_detected() throws Exception {
+ * // file system
+ * File basedir = temp.newFolder();
+ * File srcFile = new File(basedir, "src/main/java/foo/Bar.java");
+ * FileUtils.touch(srcFile);
+ * FileUtils.write(srcFile, "single line");
+ * when(fs.baseDir()).thenReturn(basedir);
+ * when(fs.encoding()).thenReturn(StandardCharsets.UTF_8);
+ *
+ * // lang
+ * when(langDetection.language(any(DefaultIndexedFile.class))).thenReturn(null);
+ *
+ * InputFileBuilder builder = new InputFileBuilder(statusDetection, fs, new FileMetadata());
+ * DefaultInputFile inputFile = builder.create(srcFile);
+ * inputFile = builder.completeAndComputeMetadata(inputFile, InputFile.Type.MAIN);
+ *
+ * assertThat(inputFile).isNull();
+ * }
+ */
+}
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
import org.sonar.api.rule.Severity;
@Test
public void testNoNewIssue() {
settings.setProperty(ConsoleReport.CONSOLE_REPORT_ENABLED_KEY, "true");
- when(inputPathCache.allFiles()).thenReturn(Arrays.<InputFile>asList(new DefaultInputFile("foo", "src/Foo.php")));
+ when(inputPathCache.allFiles()).thenReturn(Arrays.<InputFile>asList(new TestInputFileBuilder("foo", "src/Foo.php").build()));
when(issueCache.all()).thenReturn(Arrays.asList(createIssue(false, null)));
report.execute();
assertDeprecated();
@Test
public void testOneNewIssue() {
settings.setProperty(ConsoleReport.CONSOLE_REPORT_ENABLED_KEY, "true");
- when(inputPathCache.allFiles()).thenReturn(Arrays.<InputFile>asList(new DefaultInputFile("foo", "src/Foo.php")));
+ when(inputPathCache.allFiles()).thenReturn(Arrays.<InputFile>asList(new TestInputFileBuilder("foo", "src/Foo.php").build()));
when(issueCache.all()).thenReturn(Arrays.asList(createIssue(true, Severity.BLOCKER)));
report.execute();
assertDeprecated();
@Test
public void testOneNewIssuePerSeverity() {
settings.setProperty(ConsoleReport.CONSOLE_REPORT_ENABLED_KEY, "true");
- when(inputPathCache.allFiles()).thenReturn(Arrays.<InputFile>asList(new DefaultInputFile("foo", "src/Foo.php")));
+ when(inputPathCache.allFiles()).thenReturn(Arrays.<InputFile>asList(new TestInputFileBuilder("foo", "src/Foo.php").build()));
when(issueCache.all()).thenReturn(Arrays.asList(
createIssue(true, Severity.BLOCKER),
createIssue(true, Severity.CRITICAL),
" +1 info\n" +
"\n-------------------------------------------\n\n");
}
-
+
private void assertDeprecated() {
assertThat(getLogs()).contains("Console report is deprecated");
}
-
+
private void assertNotDeprecated() {
assertThat(getLogs()).doesNotContain("Console report is deprecated");
}
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.batch.rule.internal.RulesBuilder;
import org.sonar.api.config.Settings;
when(server.getVersion()).thenReturn("3.6");
userRepository = mock(UserRepositoryLoader.class);
DefaultInputDir inputDir = new DefaultInputDir("struts", "src/main/java/org/apache/struts");
- DefaultInputFile inputFile = new DefaultInputFile("struts", "src/main/java/org/apache/struts/Action.java");
+ DefaultInputFile inputFile = new TestInputFileBuilder("struts", "src/main/java/org/apache/struts/Action.java").build();
inputFile.setStatus(InputFile.Status.CHANGED);
InputPathCache fileCache = mock(InputPathCache.class);
when(fileCache.allFiles()).thenReturn(Arrays.<InputFile>asList(inputFile));
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.scm.BlameLine;
import org.sonar.scanner.index.BatchComponent;
import org.sonar.scanner.index.BatchComponentCache;
@Test
public void shouldNotFailIfNotSameNumberOfLines() {
- InputFile file = new DefaultInputFile("foo", "src/main/java/Foo.java").setLines(10);
+ InputFile file = new TestInputFileBuilder("foo", "src/main/java/Foo.java").setLines(10).build();
new DefaultBlameOutput(null, null, Arrays.asList(file)).blameResult(file, Arrays.asList(new BlameLine().revision("1").author("guy")));
}
@Test
public void shouldFailIfNotExpectedFile() {
- InputFile file = new DefaultInputFile("foo", "src/main/java/Foo.java").setLines(1);
+ InputFile file = new TestInputFileBuilder("foo", "src/main/java/Foo.java").build();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("It was not expected to blame file src/main/java/Foo.java");
- new DefaultBlameOutput(null, null, Arrays.<InputFile>asList(new DefaultInputFile("foo", "src/main/java/Foo2.java")))
+ new DefaultBlameOutput(null, null, Arrays.<InputFile>asList(new TestInputFileBuilder("foo", "src/main/java/Foo2.java").build()))
.blameResult(file, Arrays.asList(new BlameLine().revision("1").author("guy")));
}
@Test
public void shouldFailIfNullDate() {
- InputFile file = new DefaultInputFile("foo", "src/main/java/Foo.java").setLines(1);
+ InputFile file = new TestInputFileBuilder("foo", "src/main/java/Foo.java").setLines(1).build();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Blame date is null for file src/main/java/Foo.java at line 1");
@Test
public void shouldFailIfNullRevision() {
- InputFile file = new DefaultInputFile("foo", "src/main/java/Foo.java").setLines(1);
+ InputFile file = new TestInputFileBuilder("foo", "src/main/java/Foo.java").setLines(1).build();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Blame revision is blank for file src/main/java/Foo.java at line 1");
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.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
@Test
public void shouldFailIfUnknownMetric() {
- InputFile file = new DefaultInputFile("foo", "src/Foo.php");
+ InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Unknown metric: lines");
@Test
public void shouldSaveFileMeasureToSensorContext() {
- InputFile file = new DefaultInputFile("foo", "src/Foo.php");
+ InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
Resource sonarFile = File.create("src/Foo.php").setEffectiveKey("foo:src/Foo.php");
@Test(expected = UnsupportedOperationException.class)
public void duplicateHighlighting() throws Exception {
Resource sonarFile = File.create("src/Foo.java").setEffectiveKey("foo:src/Foo.java");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.java")
- .setModuleBaseDir(temp.newFolder().toPath());
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
+ .setModuleBaseDir(temp.newFolder().toPath()).build();
componentCache.add(sonarFile, null).setInputComponent(inputFile);
DefaultHighlighting h = new DefaultHighlighting(null)
.onFile(inputFile);
@Test(expected = UnsupportedOperationException.class)
public void duplicateSymbolTable() throws Exception {
Resource sonarFile = File.create("src/Foo.java").setEffectiveKey("foo:src/Foo.java");
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.java")
- .setModuleBaseDir(temp.newFolder().toPath());
+ InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
+ .setModuleBaseDir(temp.newFolder().toPath()).build();
componentCache.add(sonarFile, null).setInputComponent(inputFile);
DefaultSymbolTable st = new DefaultSymbolTable(null)
.onFile(inputFile);
@Test
public void shouldValidateStrictlyPositiveLine() throws Exception {
- DefaultInputFile file = new DefaultInputFile("module", "testfile").setModuleBaseDir(temp.newFolder().toPath());
+ InputFile file = new TestInputFileBuilder("module", "testfile").setModuleBaseDir(temp.newFolder().toPath()).build();
Map<Integer, Integer> map = ImmutableMap.of(0, 3);
String data = KeyValueFormat.format(map);
@Test
public void shouldValidateMaxLine() throws Exception {
- DefaultInputFile file = new DefaultInputFile("module", "testfile").setModuleBaseDir(temp.newFolder().toPath());
+ InputFile file = new TestInputFileBuilder("module", "testfile").setModuleBaseDir(temp.newFolder().toPath()).build();
Map<Integer, Integer> map = ImmutableMap.of(11, 3);
String data = KeyValueFormat.format(map);
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
.onlyOnLanguages("java", "php");
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
- fs.add(new DefaultInputFile("foo", "src/Foo.java").setLanguage("java"));
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.java").setLanguage("java").build());
assertThat(optimizer.shouldExecute(descriptor)).isTrue();
}
.onlyOnFileType(InputFile.Type.MAIN);
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
- fs.add(new DefaultInputFile("foo", "tests/FooTest.java").setType(InputFile.Type.TEST));
+ fs.add(new TestInputFileBuilder("foo", "tests/FooTest.java").setType(InputFile.Type.TEST).build());
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
- fs.add(new DefaultInputFile("foo", "src/Foo.java").setType(InputFile.Type.MAIN));
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.java").setType(InputFile.Type.MAIN).build());
assertThat(optimizer.shouldExecute(descriptor)).isTrue();
}
.onlyOnFileType(InputFile.Type.MAIN);
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
- fs.add(new DefaultInputFile("foo", "tests/FooTest.java").setLanguage("java").setType(InputFile.Type.TEST));
- fs.add(new DefaultInputFile("foo", "src/Foo.cbl").setLanguage("cobol").setType(InputFile.Type.MAIN));
+ fs.add(new TestInputFileBuilder("foo", "tests/FooTest.java").setLanguage("java").setType(InputFile.Type.TEST).build());
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.cbl").setLanguage("cobol").setType(InputFile.Type.MAIN).build());
assertThat(optimizer.shouldExecute(descriptor)).isFalse();
- fs.add(new DefaultInputFile("foo", "src/Foo.java").setLanguage("java").setType(InputFile.Type.MAIN));
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.java").setLanguage("java").setType(InputFile.Type.MAIN).build());
assertThat(optimizer.shouldExecute(descriptor)).isTrue();
}
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
@Test
public void shouldExcludeFileBasedOnPattern() {
- InputFile file = new DefaultInputFile("foo", "src/org/polop/File.php");
+ InputFile file = new TestInputFileBuilder("foo", "src/org/polop/File.php").build();
settings.setProperty("sonar.coverage.exclusions", "src/org/polop/*");
coverageExclusions.initPatterns();
assertThat(coverageExclusions.isExcluded(file)).isTrue();
@Test
public void shouldNotExcludeFileBasedOnPattern() {
- InputFile file = new DefaultInputFile("foo", "src/org/polop/File.php");
+ InputFile file = new TestInputFileBuilder("foo", "src/org/polop/File.php").build();
settings.setProperty("sonar.coverage.exclusions", "src/org/other/*");
coverageExclusions.initPatterns();
assertThat(coverageExclusions.isExcluded(file)).isFalse();
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
File htmlFile = new File(this.getClass().getResource("CodeColorizersTest/package.html").toURI());
SensorStorage sensorStorage = mock(SensorStorage.class);
DefaultHighlighting highlighting = new DefaultHighlighting(sensorStorage);
- highlighting.onFile(new DefaultInputFile("FOO", "package.html")
- .initMetadata(new FileMetadata().readMetadata(htmlFile, StandardCharsets.UTF_8)));
+ highlighting.onFile(new TestInputFileBuilder("FOO", "package.html")
+ .setMetadata(new FileMetadata().readMetadata(htmlFile, StandardCharsets.UTF_8))
+ .build());
codeColorizers.toSyntaxHighlighting(htmlFile, StandardCharsets.UTF_8, "web", highlighting);
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.scanner.source.DefaultHighlightable;
@Test
public void should_store_highlighting_rules() {
SensorStorage sensorStorage = mock(SensorStorage.class);
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.php")
- .initMetadata("azerty\nbla bla");
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata("azerty\nbla bla")
+ .build();
DefaultHighlightable highlightablePerspective = new DefaultHighlightable(inputFile, sensorStorage, mock(AnalysisMode.class));
highlightablePerspective.newHighlighting().highlight(0, 6, "k").highlight(7, 10, "cppd").done();
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.source.Symbol;
import org.sonar.api.source.Symbolizable;
public void should_update_cache_when_done() {
DefaultSensorStorage sensorStorage = mock(DefaultSensorStorage.class);
- DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.php")
- .initMetadata(Strings.repeat("azerty\n", 20));
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata(Strings.repeat("azerty\n", 20)).build();
DefaultSymbolizable symbolPerspective = new DefaultSymbolizable(inputFile, sensorStorage, mock(AnalysisMode.class));
Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolPerspective.newSymbolTableBuilder();
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.source.Symbol;
import org.sonar.api.source.Symbolizable;
@Before
public void prepare() {
- inputFile = new DefaultInputFile("foo", "src/Foo.php")
- .initMetadata(Strings.repeat("azerty\n", 20));
+ inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata(Strings.repeat("azerty\n", 20))
+ .build();
}
@Test
import org.junit.Test;
import org.sonar.api.batch.AnalysisMode;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.source.Highlightable;
@Test
public void should_load_default_perspective() {
HighlightableBuilder builder = new HighlightableBuilder(mock(SensorStorage.class), mock(AnalysisMode.class));
- Highlightable perspective = builder.loadPerspective(Highlightable.class, new DefaultInputFile("foo", "foo.c"));
+ Highlightable perspective = builder.loadPerspective(Highlightable.class, new TestInputFileBuilder("foo", "foo.c").build());
assertThat(perspective).isNotNull().isInstanceOf(DefaultHighlightable.class);
}
import org.junit.Test;
import org.sonar.api.batch.AnalysisMode;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.component.Perspective;
import org.sonar.api.source.Symbolizable;
import org.sonar.scanner.sensor.DefaultSensorStorage;
@Test
public void should_load_perspective() {
SymbolizableBuilder perspectiveBuilder = new SymbolizableBuilder(mock(DefaultSensorStorage.class), mock(AnalysisMode.class));
- Perspective perspective = perspectiveBuilder.loadPerspective(Symbolizable.class, new DefaultInputFile("foo", "foo.c"));
+ Perspective perspective = perspectiveBuilder.loadPerspective(Symbolizable.class, new TestInputFileBuilder("foo", "foo.c").build());
assertThat(perspective).isInstanceOf(Symbolizable.class);
}