Search in sources :

Example 41 with TestInputFileBuilder

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

the class XooTokenizerTest method testExecution.

@Test
public void testExecution() throws IOException {
    File source = new File(baseDir, "src/foo.xoo");
    FileUtils.write(source, "token1 token2 token3\ntoken4");
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
    fileSystem.add(inputFile);
    XooTokenizer tokenizer = new XooTokenizer(fileSystem);
    SourceCode sourceCode = mock(SourceCode.class);
    when(sourceCode.getFileName()).thenReturn(inputFile.absolutePath());
    Tokens cpdTokens = new Tokens();
    tokenizer.tokenize(sourceCode, cpdTokens);
    // 4 tokens + EOF
    assertThat(cpdTokens.getTokens()).hasSize(5);
    assertThat(cpdTokens.getTokens().get(3)).isEqualTo(new TokenEntry("token4", "src/foo.xoo", 2));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SourceCode(net.sourceforge.pmd.cpd.SourceCode) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Tokens(net.sourceforge.pmd.cpd.Tokens) Test(org.junit.Test)

Example 42 with TestInputFileBuilder

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

the class AnalysisErrorSensorTest method test.

@Test
public void test() throws IOException {
    Path baseDir = temp.newFolder().toPath().toAbsolutePath();
    createErrorFile(baseDir);
    int[] offsets = { 10, 20, 30, 40 };
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setOriginalLineOffsets(offsets).setModuleBaseDir(baseDir).setLines(4).build();
    context = SensorContextTester.create(baseDir);
    context.fileSystem().add(inputFile);
    sensor.execute(context);
    assertThat(context.allAnalysisErrors()).hasSize(1);
    AnalysisError error = context.allAnalysisErrors().iterator().next();
    assertThat(error.inputFile()).isEqualTo(inputFile);
    assertThat(error.location()).isEqualTo(new DefaultTextPointer(1, 4));
    assertThat(error.message()).isEqualTo("my error");
}
Also used : Path(java.nio.file.Path) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) AnalysisError(org.sonar.api.batch.sensor.error.AnalysisError) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) Test(org.junit.Test)

Example 43 with TestInputFileBuilder

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

the class OneIssuePerLineSensorTest method testForceSeverity.

@Test
public void testForceSeverity() throws IOException {
    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);
    context.settings().setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR");
    sensor.execute(context);
    // One issue per line
    assertThat(context.allIssues()).hasSize(10);
    for (Issue issue : context.allIssues()) {
        assertThat(issue.overriddenSeverity()).isEqualTo(Severity.MINOR);
    }
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Issue(org.sonar.api.batch.sensor.issue.Issue) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 44 with TestInputFileBuilder

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

the class OneIssuePerLineSensorTest method testProvideGap_before_5_5.

@Test
public void testProvideGap_before_5_5() throws IOException {
    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);
    context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2");
    context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.parse("5.4"), SonarQubeSide.SCANNER));
    sensor.execute(context);
    // One issue per line
    assertThat(context.allIssues()).hasSize(10);
    for (Issue issue : context.allIssues()) {
        assertThat(issue.gap()).isEqualTo(1.2d);
    }
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Issue(org.sonar.api.batch.sensor.issue.Issue) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 45 with TestInputFileBuilder

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

the class OneIssuePerLineSensorTest method testRule.

@Test
public void testRule() throws IOException {
    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);
    sensor.execute(context);
    // One issue per line
    assertThat(context.allIssues()).hasSize(10);
    for (Issue issue : context.allIssues()) {
        assertThat(issue.gap()).isNull();
    }
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Issue(org.sonar.api.batch.sensor.issue.Issue) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Aggregations

TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)87 Test (org.junit.Test)77 InputFile (org.sonar.api.batch.fs.InputFile)41 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)23 File (java.io.File)19 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)9 Before (org.junit.Before)7 DefaultInputDir (org.sonar.api.batch.fs.internal.DefaultInputDir)6 InputModuleHierarchy (org.sonar.api.batch.fs.internal.InputModuleHierarchy)5 BlameLine (org.sonar.api.batch.scm.BlameLine)5 AnalysisMode (org.sonar.api.batch.AnalysisMode)4 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)4 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)4 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)4 Issue (org.sonar.api.batch.sensor.issue.Issue)4 PathResolver (org.sonar.api.scan.filesystem.PathResolver)4 InputComponentStore (org.sonar.scanner.scan.filesystem.InputComponentStore)4 DefaultBlameOutput (org.sonar.scanner.scm.DefaultBlameOutput)4 DefaultTextPointer (org.sonar.api.batch.fs.internal.DefaultTextPointer)3 DefaultSensorDescriptor (org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor)3