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; }
private boolean readCoveragePerTests(ExecutionDataVisitor executionDataVisitor) { boolean collectedCoveragePerTest = false; for (Map.Entry<String, ExecutionDataStore> entry : executionDataVisitor.getSessions().entrySet()) { if (analyzeLinesCoveredByTests(entry.getKey(), entry.getValue())) { collectedCoveragePerTest = true; } } return collectedCoveragePerTest; }
public final void analyse(SensorContext context) { classFilesCache = Maps.newHashMap(); for (File classesDir : javaClasspath.getBinaryDirs()) { populateClassFilesCache(classesDir, ""); } if (classFilesCache.isEmpty()) { JaCoCoExtensions.LOG.info("No JaCoCo analysis of project coverage can be done since there is no class files."); return; } readExecutionData(report, context); classFilesCache = null; }
private void readExecutionData(@Nullable File jacocoExecutionData, SensorContext context) { File newJacocoExecutionData = jacocoExecutionData; if (newJacocoExecutionData == null || !newJacocoExecutionData.isFile()) { JaCoCoExtensions.LOG.info("Project coverage is set to 0% as no JaCoCo execution data has been dumped: {}", newJacocoExecutionData); newJacocoExecutionData = null; } ExecutionDataVisitor executionDataVisitor = new ExecutionDataVisitor(); jacocoReportReader = new JacocoReportReader(newJacocoExecutionData).readJacocoReport(executionDataVisitor, executionDataVisitor); boolean collectedCoveragePerTest = readCoveragePerTests(executionDataVisitor); CoverageBuilder coverageBuilder = jacocoReportReader.analyzeFiles(executionDataVisitor.getMerged(), classFilesCache.values()); int analyzedResources = 0; for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) { InputFile inputFile = getResource(coverage); if (inputFile != null) { NewCoverage newCoverage = context.newCoverage().onFile(inputFile); analyzeFile(newCoverage, inputFile, coverage); newCoverage.save(); analyzedResources++; } } if (analyzedResources == 0) { JaCoCoExtensions.LOG.warn("Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?"); } else if (collectedCoveragePerTest) { JaCoCoExtensions.LOG.info("Information about coverage per test has been collected."); } else if (newJacocoExecutionData != null) { JaCoCoExtensions.LOG.info("No information about coverage per test."); } }
@Override public void execute(SensorContext context) { if (context.config().hasKey(REPORT_MISSING_FORCE_ZERO)) { LOG.warn("Property '{}' is deprecated and its value will be ignored.", REPORT_MISSING_FORCE_ZERO); } Set<File> reportPaths = getReportPaths(context); if (reportPaths.isEmpty()) { return; } // Merge JaCoCo reports File reportMerged; if(reportPaths.size() == 1) { reportMerged = reportPaths.iterator().next(); } else { reportMerged = new File(context.fileSystem().workDir(), JACOCO_MERGED_FILENAME); reportMerged.getParentFile().mkdirs(); JaCoCoReportMerger.mergeReports(reportMerged, reportPaths.toArray(new File[0])); } new UnitTestAnalyzer(reportMerged, perspectives, javaResourceLocator, javaClasspath).analyse(context); }
private void populateClassFilesCache(File dir, String path) { File[] files = dir.listFiles(); if (files == null) { return; } for (File file : files) { if (file.isDirectory()) { populateClassFilesCache(file, path + file.getName() + "/"); } else if (file.getName().endsWith(".class")) { String className = path + StringUtils.removeEnd(file.getName(), ".class"); classFilesCache.put(className, file); } } }
private InputFile getResource(ISourceFileCoverage coverage) { String className = fullyQualifiedClassName(coverage.getPackageName(), coverage.getName()); InputFile inputFile = javaResourceLocator.findResourceByClassName(className); if (inputFile == null) { // Do not save measures on resource which doesn't exist in the context return null; } if (inputFile.type() == InputFile.Type.TEST) { return null; } return inputFile; }