Search in sources :

Example 1 with ISessionInfoVisitor

use of org.jacoco.core.data.ISessionInfoVisitor in project intellij-community by JetBrains.

the class JaCoCoCoverageRunner method loadExecutionData.

private static void loadExecutionData(@NotNull final File sessionDataFile, ProjectData data, @NotNull Project project) throws IOException {
    final ExecutionDataStore executionDataStore = new ExecutionDataStore();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(sessionDataFile);
        final ExecutionDataReader executionDataReader = new ExecutionDataReader(fis);
        executionDataReader.setExecutionDataVisitor(executionDataStore);
        executionDataReader.setSessionInfoVisitor(new ISessionInfoVisitor() {

            public void visitSessionInfo(SessionInfo info) {
                System.out.println(info.toString());
            }
        });
        while (executionDataReader.read()) {
        }
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    final CoverageBuilder coverageBuilder = new CoverageBuilder();
    final Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
    final Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
        final CompilerModuleExtension compilerModuleExtension = CompilerModuleExtension.getInstance(module);
        if (compilerModuleExtension != null) {
            final VirtualFile[] roots = compilerModuleExtension.getOutputRoots(true);
            for (VirtualFile root : roots) {
                analyzer.analyzeAll(VfsUtil.virtualToIoFile(root));
            }
        }
    }
    for (IClassCoverage classCoverage : coverageBuilder.getClasses()) {
        String className = classCoverage.getName();
        className = className.replace('\\', '.').replace('/', '.');
        final ClassData classData = data.getOrCreateClassData(className);
        final Collection<IMethodCoverage> methods = classCoverage.getMethods();
        LineData[] lines = new LineData[classCoverage.getLastLine() + 1];
        for (IMethodCoverage method : methods) {
            final String desc = method.getName() + method.getDesc();
            // Line numbers are 1-based here.
            final int firstLine = method.getFirstLine();
            final int lastLine = method.getLastLine();
            for (int i = firstLine; i <= lastLine; i++) {
                final ILine methodLine = method.getLine(i);
                final int methodLineStatus = methodLine.getStatus();
                if (methodLineStatus == ICounter.EMPTY)
                    continue;
                final LineData lineData = new LineData(i, desc) {

                    @Override
                    public int getStatus() {
                        switch(methodLineStatus) {
                            case ICounter.FULLY_COVERED:
                                return LineCoverage.FULL;
                            case ICounter.PARTLY_COVERED:
                                return LineCoverage.PARTIAL;
                            default:
                                return LineCoverage.NONE;
                        }
                    }
                };
                lineData.setHits(methodLineStatus == ICounter.FULLY_COVERED || methodLineStatus == ICounter.PARTLY_COVERED ? 1 : 0);
                lines[i] = lineData;
            }
        }
        classData.setLines(lines);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ISessionInfoVisitor(org.jacoco.core.data.ISessionInfoVisitor) SessionInfo(org.jacoco.core.data.SessionInfo) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension) FileInputStream(java.io.FileInputStream) ExecutionDataReader(org.jacoco.core.data.ExecutionDataReader) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) ExecutionDataStore(org.jacoco.core.data.ExecutionDataStore) Module(com.intellij.openapi.module.Module)

Example 2 with ISessionInfoVisitor

use of org.jacoco.core.data.ISessionInfoVisitor in project jacoco by jacoco.

the class ExecInfo method dump.

private void dump(final File file, final PrintWriter out) throws IOException {
    out.printf("[INFO] Loading exec file %s.%n", file);
    out.println("CLASS ID         HITS/PROBES   CLASS NAME");
    final FileInputStream in = new FileInputStream(file);
    final ExecutionDataReader reader = new ExecutionDataReader(in);
    reader.setSessionInfoVisitor(new ISessionInfoVisitor() {

        public void visitSessionInfo(final SessionInfo info) {
            out.printf("Session \"%s\": %s - %s%n", info.getId(), new Date(info.getStartTimeStamp()), new Date(info.getDumpTimeStamp()));
        }
    });
    reader.setExecutionDataVisitor(new IExecutionDataVisitor() {

        public void visitClassExecution(final ExecutionData data) {
            out.printf("%016x  %3d of %3d   %s%n", Long.valueOf(data.getId()), Integer.valueOf(getHitCount(data.getProbes())), Integer.valueOf(data.getProbes().length), data.getName());
        }
    });
    reader.read();
    in.close();
    out.println();
}
Also used : ISessionInfoVisitor(org.jacoco.core.data.ISessionInfoVisitor) SessionInfo(org.jacoco.core.data.SessionInfo) IExecutionDataVisitor(org.jacoco.core.data.IExecutionDataVisitor) FileInputStream(java.io.FileInputStream) Date(java.util.Date) ExecutionDataReader(org.jacoco.core.data.ExecutionDataReader) ExecutionData(org.jacoco.core.data.ExecutionData)

Example 3 with ISessionInfoVisitor

use of org.jacoco.core.data.ISessionInfoVisitor in project jacoco by jacoco.

the class ExecDump method dump.

private void dump(final String file) throws IOException {
    out.printf("exec file: %s%n", file);
    out.println("CLASS ID         HITS/PROBES   CLASS NAME");
    final FileInputStream in = new FileInputStream(file);
    final ExecutionDataReader reader = new ExecutionDataReader(in);
    reader.setSessionInfoVisitor(new ISessionInfoVisitor() {

        public void visitSessionInfo(final SessionInfo info) {
            out.printf("Session \"%s\": %s - %s%n", info.getId(), new Date(info.getStartTimeStamp()), new Date(info.getDumpTimeStamp()));
        }
    });
    reader.setExecutionDataVisitor(new IExecutionDataVisitor() {

        public void visitClassExecution(final ExecutionData data) {
            out.printf("%016x  %3d of %3d   %s%n", Long.valueOf(data.getId()), Integer.valueOf(getHitCount(data.getProbes())), Integer.valueOf(data.getProbes().length), data.getName());
        }
    });
    reader.read();
    in.close();
    out.println();
}
Also used : ISessionInfoVisitor(org.jacoco.core.data.ISessionInfoVisitor) SessionInfo(org.jacoco.core.data.SessionInfo) IExecutionDataVisitor(org.jacoco.core.data.IExecutionDataVisitor) FileInputStream(java.io.FileInputStream) Date(java.util.Date) ExecutionDataReader(org.jacoco.core.data.ExecutionDataReader) ExecutionData(org.jacoco.core.data.ExecutionData)

Example 4 with ISessionInfoVisitor

use of org.jacoco.core.data.ISessionInfoVisitor in project sonar-java by SonarSource.

the class JaCoCoReportMerger method mergeReports.

/**
 * Merge all reports in reportOverall.
 * @param reportOverall destination file of merge.
 * @param reports files to be merged.
 */
public static void mergeReports(File reportOverall, File... reports) {
    ExecutionDataVisitor edv = new ExecutionDataVisitor();
    boolean isCurrentVersionFormat = loadSourceFiles(edv, reports);
    try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(reportOverall))) {
        Object visitor;
        if (isCurrentVersionFormat) {
            visitor = new ExecutionDataWriter(outputStream);
        } else {
            visitor = new org.jacoco.previous.core.data.ExecutionDataWriter(outputStream);
        }
        for (Map.Entry<String, ExecutionDataStore> entry : edv.getSessions().entrySet()) {
            SessionInfo sessionInfo = new SessionInfo(entry.getKey(), 0, 0);
            ((ISessionInfoVisitor) visitor).visitSessionInfo(sessionInfo);
            entry.getValue().accept((IExecutionDataVisitor) visitor);
        }
    } catch (IOException e) {
        throw new AnalysisException(String.format("Unable to write overall coverage report %s", reportOverall.getAbsolutePath()), e);
    }
}
Also used : ISessionInfoVisitor(org.jacoco.core.data.ISessionInfoVisitor) SessionInfo(org.jacoco.core.data.SessionInfo) IOException(java.io.IOException) IExecutionDataVisitor(org.jacoco.core.data.IExecutionDataVisitor) ExecutionDataStore(org.jacoco.core.data.ExecutionDataStore) FileOutputStream(java.io.FileOutputStream) AnalysisException(org.sonar.java.AnalysisException) ExecutionDataWriter(org.jacoco.core.data.ExecutionDataWriter) BufferedOutputStream(java.io.BufferedOutputStream) Map(java.util.Map)

Aggregations

ISessionInfoVisitor (org.jacoco.core.data.ISessionInfoVisitor)4 SessionInfo (org.jacoco.core.data.SessionInfo)4 FileInputStream (java.io.FileInputStream)3 ExecutionDataReader (org.jacoco.core.data.ExecutionDataReader)3 IExecutionDataVisitor (org.jacoco.core.data.IExecutionDataVisitor)3 Date (java.util.Date)2 ExecutionData (org.jacoco.core.data.ExecutionData)2 ExecutionDataStore (org.jacoco.core.data.ExecutionDataStore)2 Module (com.intellij.openapi.module.Module)1 CompilerModuleExtension (com.intellij.openapi.roots.CompilerModuleExtension)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ClassData (com.intellij.rt.coverage.data.ClassData)1 LineData (com.intellij.rt.coverage.data.LineData)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ExecutionDataWriter (org.jacoco.core.data.ExecutionDataWriter)1 AnalysisException (org.sonar.java.AnalysisException)1