Search in sources :

Example 21 with InputFile

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

the class UnitTestAnalyzer method analyzeLinesCoveredByTests.

private boolean analyzeLinesCoveredByTests(String sessionId, ExecutionDataStore executionDataStore) {
    int i = sessionId.indexOf(' ');
    if (i < 0) {
        return false;
    }
    String testClassName = sessionId.substring(0, i);
    String testName = sessionId.substring(i + 1);
    InputFile testResource = javaResourceLocator.findResourceByClassName(testClassName);
    if (testResource == null) {
        // No such test class
        return false;
    }
    boolean result = false;
    CoverageBuilder coverageBuilder = jacocoReportReader.analyzeFiles(executionDataStore, classFilesOfStore(executionDataStore));
    for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) {
        InputFile resource = getResource(coverage);
        if (resource != null) {
            List<Integer> coveredLines = coveredLines(coverage);
            if (!coveredLines.isEmpty() && addCoverage(resource, testResource, testName, coveredLines)) {
                result = true;
            }
        }
    }
    return result;
}
Also used : ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) InputFile(org.sonar.api.batch.fs.InputFile)

Example 22 with InputFile

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

the class PostAnalysisIssueFilter method scanFile.

@Override
public void scanFile(JavaFileScannerContext context) {
    InputFile component = fileSystem.inputFile(fileSystem.predicates().is(context.getFile()));
    if (component == null) {
        throw new AnalysisException("Component not found: " + context.getFileKey());
    }
    String componentKey = component.key();
    for (JavaIssueFilter javaIssueFilter : getIssueFilters()) {
        javaIssueFilter.setComponentKey(componentKey);
        javaIssueFilter.scanFile(context);
    }
}
Also used : AnalysisException(org.sonar.java.AnalysisException) InputFile(org.sonar.api.batch.fs.InputFile)

Example 23 with InputFile

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

the class JavaSquidSensorTest method createContext.

private static SensorContextTester createContext(InputFile.Type onType) throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("src/test/java/").getAbsoluteFile());
    DefaultFileSystem fs = context.fileSystem();
    String effectiveKey = "org/sonar/plugins/java/JavaSquidSensorTest.java";
    File file = new File(fs.baseDir(), effectiveKey);
    DefaultInputFile inputFile = new TestInputFileBuilder("", effectiveKey).setLanguage("java").setModuleBaseDir(fs.baseDirPath()).setType(onType).initMetadata(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
    fs.add(inputFile);
    return context;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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)

Example 24 with InputFile

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

the class DefaultJavaResourceLocator method findResourceByClassName.

@Override
public InputFile findResourceByClassName(String className) {
    String name = className.replace('.', '/');
    InputFile inputFile = resourcesByClass.get(name);
    if (inputFile == null) {
        LOG.debug("Class not found in resource cache : {}", className);
    }
    return inputFile;
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile)

Example 25 with InputFile

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

the class XmlFileSensor method execute.

@Override
public void execute(SensorContext context) {
    if (hasXmlFiles()) {
        Iterable<InputFile> xmlInputFiles = getXmlFiles();
        // make xml files visible in SQ UI, when XML plugin is not installed
        xmlInputFiles.forEach(context::markForPublishing);
        sonarComponents.registerCheckClasses(CheckList.REPOSITORY_KEY, CheckList.getXmlChecks());
        sonarComponents.setSensorContext(context);
        new XmlAnalyzer(sonarComponents, sonarComponents.checkClasses()).scan(toFile(xmlInputFiles));
    }
}
Also used : XmlAnalyzer(org.sonar.java.xml.XmlAnalyzer) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)221 Test (org.junit.Test)120 File (java.io.File)100 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)58 AnalysisResult (org.sonar.scanner.mediumtest.AnalysisResult)32 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)24 FileSystem (org.sonar.api.batch.fs.FileSystem)21 FilePredicates (org.sonar.api.batch.fs.FilePredicates)19 IOException (java.io.IOException)18 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)15 List (java.util.List)14 Test (org.junit.jupiter.api.Test)12 BlameLine (org.sonar.api.batch.scm.BlameLine)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 Before (org.junit.Before)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8