Search in sources :

Example 66 with TestInputFileBuilder

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

the class SensorContextTesterTest method duplicateMeasures.

@Test(expected = IllegalStateException.class)
public void duplicateMeasures() {
    tester.<Integer>newMeasure().on(new TestInputFileBuilder("foo", "src/Foo.java").build()).forMetric(CoreMetrics.NCLOC).withValue(2).save();
    tester.<Integer>newMeasure().on(new TestInputFileBuilder("foo", "src/Foo.java").build()).forMetric(CoreMetrics.NCLOC).withValue(2).save();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Test(org.junit.Test)

Example 67 with TestInputFileBuilder

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

the class SensorContextTesterTest method testCpdTokens.

@Test
public void testCpdTokens() {
    assertThat(tester.cpdTokens("foo:src/Foo.java")).isNull();
    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").addToken(inputFile.newRange(7, 12), "class").addToken(inputFile.newRange(13, 16), "$IDENTIFIER").addToken(inputFile.newRange(17, 18), "{").addToken(inputFile.newRange(3, 0, 3, 1), "}").save();
    assertThat(tester.cpdTokens("foo:src/Foo.java")).extracting("value", "startLine", "startUnit", "endUnit").containsExactly(tuple("publicclass$IDENTIFIER{", 1, 1, 4), tuple("}", 3, 5, 5));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 68 with TestInputFileBuilder

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

the class JGitBlameCommandTest method testBlame.

@Test
public void testBlame() throws IOException {
    File projectDir = temp.newFolder();
    javaUnzip("dummy-git.zip", projectDir);
    JGitBlameCommand jGitBlameCommand = newJGitBlameCommand();
    File baseDir = new File(projectDir, "dummy-git");
    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    when(input.fileSystem()).thenReturn(fs);
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", DUMMY_JAVA).setModuleBaseDir(baseDir.toPath()).build();
    fs.add(inputFile);
    BlameOutput blameResult = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile));
    jGitBlameCommand.blame(input, blameResult);
    Date revisionDate1 = DateUtils.parseDateTime("2012-07-17T16:12:48+0200");
    String revision1 = "6b3aab35a3ea32c1636fee56f996e677653c48ea";
    String author1 = "david@gageot.net";
    // second commit, which has a commit date different than the author date
    Date revisionDate2 = DateUtils.parseDateTime("2015-05-19T13:31:09+0200");
    String revision2 = "0d269c1acfb8e6d4d33f3c43041eb87e0df0f5e7";
    String author2 = "duarte.meneses@sonarsource.com";
    List<BlameLine> expectedBlame = new LinkedList<>();
    for (int i = 0; i < 25; i++) {
        expectedBlame.add(new BlameLine().revision(revision1).date(revisionDate1).author(author1));
    }
    for (int i = 0; i < 3; i++) {
        expectedBlame.add(new BlameLine().revision(revision2).date(revisionDate2).author(author2));
    }
    for (int i = 0; i < 1; i++) {
        expectedBlame.add(new BlameLine().revision(revision1).date(revisionDate1).author(author1));
    }
    verify(blameResult).blameResult(inputFile, expectedBlame);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) BlameLine(org.sonar.api.batch.scm.BlameLine) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) BlameOutput(org.sonar.api.batch.scm.BlameCommand.BlameOutput) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Date(java.util.Date) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 69 with TestInputFileBuilder

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

the class JGitBlameCommandTest method dontFailOnNewFile.

@Test
public void dontFailOnNewFile() throws IOException {
    File projectDir = temp.newFolder();
    javaUnzip("dummy-git.zip", projectDir);
    JGitBlameCommand jGitBlameCommand = newJGitBlameCommand();
    File baseDir = new File(projectDir, "dummy-git");
    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    when(input.fileSystem()).thenReturn(fs);
    String relativePath = DUMMY_JAVA;
    String relativePath2 = "src/main/java/org/dummy/Dummy2.java";
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", relativePath).build();
    fs.add(inputFile);
    DefaultInputFile inputFile2 = new TestInputFileBuilder("foo", relativePath2).build();
    fs.add(inputFile2);
    // Emulate a new file
    FileUtils.copyFile(new File(baseDir, relativePath), new File(baseDir, relativePath2));
    BlameOutput blameResult = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile, inputFile2));
    jGitBlameCommand.blame(input, blameResult);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) BlameOutput(org.sonar.api.batch.scm.BlameCommand.BlameOutput) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 70 with TestInputFileBuilder

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

the class JGitBlameCommandTest method dontFailOnModifiedFile.

@Test
public void dontFailOnModifiedFile() throws IOException {
    File projectDir = temp.newFolder();
    javaUnzip("dummy-git.zip", projectDir);
    JGitBlameCommand jGitBlameCommand = newJGitBlameCommand();
    File baseDir = new File(projectDir, "dummy-git");
    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    when(input.fileSystem()).thenReturn(fs);
    String relativePath = DUMMY_JAVA;
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", relativePath).build();
    fs.add(inputFile);
    // Emulate a modification
    Files.write(baseDir.toPath().resolve(relativePath), "modification and \n some new line".getBytes());
    BlameOutput blameResult = mock(BlameOutput.class);
    when(input.filesToBlame()).thenReturn(Arrays.asList(inputFile));
    jGitBlameCommand.blame(input, blameResult);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) BlameOutput(org.sonar.api.batch.scm.BlameCommand.BlameOutput) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) 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