Search in sources :

Example 56 with DefaultInputFile

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

the class InputComponentStoreTest method should_add_input_file.

@Test
public void should_add_input_file() throws Exception {
    InputComponentStore cache = new InputComponentStore(new PathResolver());
    String rootModuleKey = "struts";
    File rootBaseDir = temp.newFolder();
    DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(rootModuleKey, rootBaseDir);
    cache.put(rootModule);
    String subModuleKey = "struts-core";
    DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(subModuleKey, temp.newFolder());
    rootModule.definition().addSubProject(subModule.definition());
    cache.put(subModule);
    DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java").setModuleBaseDir(rootBaseDir.toPath()).setPublish(true).build();
    cache.put(fooFile);
    cache.put(new TestInputFileBuilder(subModuleKey, "src/main/java/Bar.java").setLanguage("bla").setPublish(false).setType(Type.MAIN).setStatus(Status.ADDED).setLines(2).setCharset(StandardCharsets.UTF_8).setModuleBaseDir(temp.newFolder().toPath()).build());
    DefaultInputFile loadedFile = (DefaultInputFile) cache.getFile(subModuleKey, "src/main/java/Bar.java");
    assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java");
    assertThat(loadedFile.charset()).isEqualTo(StandardCharsets.UTF_8);
    assertThat(cache.filesByModule(rootModuleKey)).hasSize(1);
    assertThat(cache.filesByModule(subModuleKey)).hasSize(1);
    assertThat(cache.allFiles()).hasSize(2);
    for (InputPath inputPath : cache.allFiles()) {
        assertThat(inputPath.relativePath()).startsWith("src/main/java/");
    }
    List<InputFile> toPublish = new LinkedList<>();
    cache.allFilesToPublish().forEach(toPublish::add);
    assertThat(toPublish).containsExactly(fooFile);
    cache.remove(fooFile);
    assertThat(cache.allFiles()).hasSize(1);
    cache.removeModule(rootModuleKey);
    assertThat(cache.filesByModule(rootModuleKey)).hasSize(0);
    assertThat(cache.filesByModule(subModuleKey)).hasSize(1);
    assertThat(cache.allFiles()).hasSize(1);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputPath(org.sonar.api.batch.fs.InputPath) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) PathResolver(org.sonar.api.scan.filesystem.PathResolver) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) LinkedList(java.util.LinkedList) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 57 with DefaultInputFile

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

the class DefaultSymbolizableTest method should_update_cache_when_done.

@Test
public void should_update_cache_when_done() {
    DefaultSensorStorage sensorStorage = mock(DefaultSensorStorage.class);
    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();
    Symbol firstSymbol = symbolTableBuilder.newSymbol(4, 8);
    symbolTableBuilder.newReference(firstSymbol, 12);
    symbolTableBuilder.newReference(firstSymbol, 70);
    Symbol otherSymbol = symbolTableBuilder.newSymbol(25, 33);
    symbolTableBuilder.newReference(otherSymbol, 44);
    symbolTableBuilder.newReference(otherSymbol, 60);
    symbolTableBuilder.newReference(otherSymbol, 108);
    Symbolizable.SymbolTable symbolTable = symbolTableBuilder.build();
    symbolPerspective.setSymbolTable(symbolTable);
    ArgumentCaptor<DefaultSymbolTable> argCaptor = ArgumentCaptor.forClass(DefaultSymbolTable.class);
    verify(sensorStorage).store(argCaptor.capture());
    // Map<Symbol, Set<TextRange>>
    assertThat(argCaptor.getValue().getReferencesBySymbol().keySet()).hasSize(2);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Symbol(org.sonar.api.source.Symbol) DefaultSensorStorage(org.sonar.scanner.sensor.DefaultSensorStorage) AnalysisMode(org.sonar.api.batch.AnalysisMode) DefaultSymbolizable(org.sonar.scanner.source.DefaultSymbolizable) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) Symbolizable(org.sonar.api.source.Symbolizable) DefaultSymbolizable(org.sonar.scanner.source.DefaultSymbolizable) Test(org.junit.Test)

Aggregations

DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)57 Test (org.junit.Test)29 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)22 File (java.io.File)13 InputFile (org.sonar.api.batch.fs.InputFile)13 TaskResult (org.sonar.scanner.mediumtest.TaskResult)9 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)8 Path (java.nio.file.Path)6 InputModuleHierarchy (org.sonar.api.batch.fs.internal.InputModuleHierarchy)6 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)5 InputDir (org.sonar.api.batch.fs.InputDir)5 DefaultInputDir (org.sonar.api.batch.fs.internal.DefaultInputDir)5 ScannerReportReader (org.sonar.scanner.protocol.output.ScannerReportReader)5 Block (org.sonar.duplications.block.Block)4 CheckForNull (javax.annotation.CheckForNull)3 TextRange (org.sonar.api.batch.fs.TextRange)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3 Issue (org.sonar.api.batch.sensor.issue.Issue)3 ProjectAnalysisInfo (org.sonar.scanner.ProjectAnalysisInfo)3 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)3