Search in sources :

Example 56 with TestInputFileBuilder

use of org.sonar.api.batch.fs.internal.TestInputFileBuilder in project sonarqube by SonarSource.

the class ExternalIssueImporterTest method prepare.

@Before
public void prepare() throws Exception {
    File baseDir = temp.newFolder();
    context = SensorContextTester.create(baseDir);
    sourceFile = new TestInputFileBuilder("foo", "src/Foo.java").setModuleBaseDir(baseDir.toPath()).initMetadata("the first line\nthe second line").setCharset(UTF_8).setLanguage("java").build();
    context.fileSystem().add(sourceFile);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Before(org.junit.Before)

Example 57 with TestInputFileBuilder

use of org.sonar.api.batch.fs.internal.TestInputFileBuilder in project sonarqube by SonarSource.

the class FileMetadataTest method ignore_whitespace_when_computing_line_hashes.

@Test
public void ignore_whitespace_when_computing_line_hashes() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, " foo\nb ar\r\nbaz \t", StandardCharsets.UTF_8, true);
    DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName()).setModuleBaseDir(tempFile.getParentFile().toPath()).setCharset(StandardCharsets.UTF_8).build();
    FileMetadata.computeLineHashesForIssueTracking(f, new FileMetadata.LineHashConsumer() {

        @Override
        public void consume(int lineIdx, @Nullable byte[] hash) {
            switch(lineIdx) {
                case 1:
                    assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("foo"));
                    break;
                case 2:
                    assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("bar"));
                    break;
                case 3:
                    assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("baz"));
                    break;
                default:
                    fail("Invalid line");
            }
        }
    });
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 58 with TestInputFileBuilder

use of org.sonar.api.batch.fs.internal.TestInputFileBuilder in project sonarqube by SonarSource.

the class FileMetadataTest method dont_fail_on_empty_file.

@Test
public void dont_fail_on_empty_file() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, "", StandardCharsets.UTF_8, true);
    DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName()).setModuleBaseDir(tempFile.getParentFile().toPath()).setCharset(StandardCharsets.UTF_8).build();
    FileMetadata.computeLineHashesForIssueTracking(f, new FileMetadata.LineHashConsumer() {

        @Override
        public void consume(int lineIdx, @Nullable byte[] hash) {
            switch(lineIdx) {
                case 1:
                    assertThat(hash).isNull();
                    break;
                default:
                    fail("Invalid line");
            }
        }
    });
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 59 with TestInputFileBuilder

use of org.sonar.api.batch.fs.internal.TestInputFileBuilder in project sonarqube by SonarSource.

the class DefaultFileSystemTest method inputFiles_using_optimized_predicates.

@Test
public void inputFiles_using_optimized_predicates() {
    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());
    assertThat(fs.inputFiles(fs.predicates().hasFilename("Foo.php"))).hasSize(1);
    assertThat(fs.inputFiles(fs.predicates().hasFilename("unknown"))).isEmpty();
    assertThat(fs.inputFiles(fs.predicates().hasExtension("java"))).hasSize(2);
    assertThat(fs.inputFiles(fs.predicates().hasExtension("unknown"))).isEmpty();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Test(org.junit.Test)

Example 60 with TestInputFileBuilder

use of org.sonar.api.batch.fs.internal.TestInputFileBuilder in project sonarqube by SonarSource.

the class DefaultFileSystemTest method hasFiles_using_optimized_predicates.

@Test
public void hasFiles_using_optimized_predicates() {
    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());
    assertThat(fs.hasFiles(fs.predicates().hasFilename("Foo.php"))).isTrue();
    assertThat(fs.hasFiles(fs.predicates().hasFilename("unknown"))).isFalse();
    assertThat(fs.hasFiles(fs.predicates().hasExtension("java"))).isTrue();
    assertThat(fs.hasFiles(fs.predicates().hasExtension("unknown"))).isFalse();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Test(org.junit.Test)

Aggregations

TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)199 Test (org.junit.Test)163 File (java.io.File)89 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)88 InputFile (org.sonar.api.batch.fs.InputFile)87 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)41 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)23 Before (org.junit.Before)22 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)16 SonarComponents (org.sonar.java.SonarComponents)13 JavaCheck (org.sonar.plugins.java.api.JavaCheck)13 BlameLine (org.sonar.api.batch.scm.BlameLine)12 DefaultSensorDescriptor (org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor)9 Path (java.nio.file.Path)8 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)8 AnalyzerMessage (org.sonar.java.AnalyzerMessage)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 IOException (java.io.IOException)6 ZipFile (java.util.zip.ZipFile)6 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)6