Search in sources :

Example 71 with InputFile

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

the class FileExtensionPredicateTest method mockWithName.

private InputFile mockWithName(String filename) throws IOException {
    InputFile inputFile = mock(InputFile.class);
    when(inputFile.file()).thenReturn(new File("dummy parent", filename));
    return inputFile;
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile)

Example 72 with InputFile

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

the class SensorContextTesterTest method testAnalysisErrors.

@Test
public void testAnalysisErrors() {
    assertThat(tester.allAnalysisErrors()).isEmpty();
    NewAnalysisError newAnalysisError = tester.newAnalysisError();
    InputFile file = new TestInputFileBuilder("foo", "src/Foo.java").build();
    newAnalysisError.onFile(file).message("error").at(new DefaultTextPointer(5, 2)).save();
    assertThat(tester.allAnalysisErrors()).hasSize(1);
    AnalysisError analysisError = tester.allAnalysisErrors().iterator().next();
    assertThat(analysisError.inputFile()).isEqualTo(file);
    assertThat(analysisError.message()).isEqualTo("error");
    assertThat(analysisError.location()).isEqualTo(new DefaultTextPointer(5, 2));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) NewAnalysisError(org.sonar.api.batch.sensor.error.NewAnalysisError) AnalysisError(org.sonar.api.batch.sensor.error.AnalysisError) NewAnalysisError(org.sonar.api.batch.sensor.error.NewAnalysisError) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 73 with InputFile

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

the class FilenamePredicateTest method should_not_match_file_by_different_filename.

@Test
public void should_not_match_file_by_different_filename() throws IOException {
    String filename = "some name";
    InputFile inputFile = mock(InputFile.class);
    when(inputFile.file()).thenReturn(newDummyFile(filename + "x"));
    assertThat(new FilenamePredicate(filename).apply(inputFile)).isFalse();
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile)

Example 74 with InputFile

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

the class FilenamePredicateTest method should_match_file_by_filename.

@Test
public void should_match_file_by_filename() throws IOException {
    String filename = "some name";
    InputFile inputFile = mock(InputFile.class);
    when(inputFile.file()).thenReturn(newDummyFile(filename));
    assertThat(new FilenamePredicate(filename).apply(inputFile)).isTrue();
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile)

Example 75 with InputFile

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

the class CpdExecutor method runCpdAnalysis.

@VisibleForTesting
void runCpdAnalysis(ExecutorService executorService, String componentKey, final Collection<Block> fileBlocks, long timeout) {
    DefaultInputComponent component = (DefaultInputComponent) componentStore.getByKey(componentKey);
    if (component == null) {
        LOG.error("Resource not found in component store: {}. Skipping CPD computation for it", componentKey);
        return;
    }
    InputFile inputFile = (InputFile) component;
    LOG.debug("Detection of duplications for {}", inputFile.absolutePath());
    progressReport.message(String.format("%d/%d - current file: %s", count, total, inputFile.absolutePath()));
    List<CloneGroup> duplications;
    Future<List<CloneGroup>> futureResult = executorService.submit(() -> SuffixTreeCloneDetectionAlgorithm.detect(index, fileBlocks));
    try {
        duplications = futureResult.get(timeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        LOG.warn("Timeout during detection of duplications for " + inputFile.absolutePath());
        futureResult.cancel(true);
        return;
    } catch (Exception e) {
        throw new IllegalStateException("Fail during detection of duplication for " + inputFile.absolutePath(), e);
    }
    List<CloneGroup> filtered;
    if (!"java".equalsIgnoreCase(inputFile.language())) {
        Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(inputFile.language()));
        filtered = from(duplications).filter(minimumTokensPredicate).toList();
    } else {
        filtered = duplications;
    }
    saveDuplications(component, filtered);
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) List(java.util.List) CloneGroup(org.sonar.duplications.index.CloneGroup) TimeoutException(java.util.concurrent.TimeoutException) InputFile(org.sonar.api.batch.fs.InputFile) TimeoutException(java.util.concurrent.TimeoutException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)102 Test (org.junit.Test)61 File (java.io.File)45 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)39 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)20 TaskResult (org.sonar.scanner.mediumtest.TaskResult)19 IOException (java.io.IOException)10 FilePredicates (org.sonar.api.batch.fs.FilePredicates)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)7 List (java.util.List)6 FileSystem (org.sonar.api.batch.fs.FileSystem)6 BlameLine (org.sonar.api.batch.scm.BlameLine)6 ArrayList (java.util.ArrayList)4 PathResolver (org.sonar.api.scan.filesystem.PathResolver)4 DefaultBlameOutput (org.sonar.scanner.scm.DefaultBlameOutput)4 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)3 DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)3 MutableTestPlan (org.sonar.api.test.MutableTestPlan)3 Block (org.sonar.duplications.block.Block)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2