Search in sources :

Example 1 with ClassData

use of com.intellij.rt.coverage.data.ClassData 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 ClassData

use of com.intellij.rt.coverage.data.ClassData in project intellij-community by JetBrains.

the class CoverageDataManagerImpl method selectSubCoverage.

public void selectSubCoverage(@NotNull final CoverageSuitesBundle suite, final List<String> testNames) {
    suite.restoreCoverageData();
    final ProjectData data = suite.getCoverageData();
    if (data == null)
        return;
    mySubCoverageIsActive = true;
    final Map<String, Set<Integer>> executionTrace = new HashMap<>();
    for (CoverageSuite coverageSuite : suite.getSuites()) {
        final String fileName = coverageSuite.getCoverageDataFileName();
        final File tracesDir = getTracesDirectory(fileName);
        for (String testName : testNames) {
            final File file = new File(tracesDir, FileUtil.sanitizeFileName(testName) + ".tr");
            if (file.exists()) {
                DataInputStream in = null;
                try {
                    in = new DataInputStream(new FileInputStream(file));
                    int traceSize = in.readInt();
                    for (int i = 0; i < traceSize; i++) {
                        final String className = in.readUTF();
                        final int linesSize = in.readInt();
                        Set<Integer> lines = executionTrace.get(className);
                        if (lines == null) {
                            lines = new HashSet<>();
                            executionTrace.put(className, lines);
                        }
                        for (int l = 0; l < linesSize; l++) {
                            lines.add(in.readInt());
                        }
                    }
                } catch (Exception e) {
                    LOG.error(e);
                } finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        LOG.error(e);
                    }
                }
            }
        }
    }
    final ProjectData projectData = new ProjectData();
    for (String className : executionTrace.keySet()) {
        ClassData loadedClassData = projectData.getClassData(className);
        if (loadedClassData == null) {
            loadedClassData = projectData.getOrCreateClassData(className);
        }
        final Set<Integer> lineNumbers = executionTrace.get(className);
        final ClassData oldData = data.getClassData(className);
        LOG.assertTrue(oldData != null, "missed className: \"" + className + "\"");
        final Object[] oldLines = oldData.getLines();
        LOG.assertTrue(oldLines != null);
        int maxNumber = oldLines.length;
        for (Integer lineNumber : lineNumbers) {
            if (lineNumber >= maxNumber) {
                maxNumber = lineNumber + 1;
            }
        }
        final LineData[] lines = new LineData[maxNumber];
        for (Integer line : lineNumbers) {
            final int lineIdx = line.intValue() - 1;
            String methodSig = null;
            if (lineIdx < oldData.getLines().length) {
                final LineData oldLineData = oldData.getLineData(lineIdx);
                if (oldLineData != null) {
                    methodSig = oldLineData.getMethodSignature();
                }
            }
            final LineData lineData = new LineData(lineIdx, methodSig);
            if (methodSig != null) {
                loadedClassData.registerMethodSignature(lineData);
            }
            lineData.setStatus(LineCoverage.FULL);
            lines[lineIdx] = lineData;
        }
        loadedClassData.setLines(lines);
    }
    suite.setCoverageData(projectData);
    renewCoverageData(suite);
}
Also used : HashSet(com.intellij.util.containers.HashSet) Set(java.util.Set) HashMap(com.intellij.util.containers.HashMap) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) WriteExternalException(com.intellij.openapi.util.WriteExternalException) InvalidDataException(com.intellij.openapi.util.InvalidDataException) IOException(java.io.IOException) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) ProjectData(com.intellij.rt.coverage.data.ProjectData)

Example 3 with ClassData

use of com.intellij.rt.coverage.data.ClassData in project intellij-community by JetBrains.

the class CoberturaLoaderUtil method load.

public static ProjectData load(final File sessionDataFile) {
    ProjectData projectInfo = new ProjectData();
    DataInputStream dataFile = null;
    try {
        dataFile = new DataInputStream(new FileInputStream(sessionDataFile));
        int classesCount = dataFile.read();
        for (int i = 0; i < classesCount; i++) {
            final String classFQName = dataFile.readUTF();
            //sourcefilename
            dataFile.readUTF();
            final ClassData classData = projectInfo.getOrCreateClassData(classFQName);
            final int numberOfLines = dataFile.read();
            for (int l = 0; l < numberOfLines; l++) {
                final int lineNumber = dataFile.read();
                //todo classData.getOrCreateLine(lineNumber, dataFile.readUTF() + dataFile.readUTF());
                final LineData lineData = null;
                long hits = dataFile.readLong();
                final int jumpsNumber = dataFile.read();
                int trueHits = 0;
                int falseHits = 0;
                int totalHits = 0;
                for (int j = 0; j < jumpsNumber; j++) {
                    //jump number
                    dataFile.read();
                    totalHits++;
                    if (dataFile.readLong() > 0)
                        trueHits++;
                    totalHits++;
                    if (dataFile.readLong() > 0)
                        falseHits++;
                }
                int defaultHitsNumber = 0;
                int branchHitNumber = 0;
                final int switchNumber = dataFile.read();
                for (int s = 0; s < switchNumber; s++) {
                    //switch number
                    dataFile.read();
                    //number of keys
                    dataFile.read();
                    long defaultHits = dataFile.readLong();
                    if (defaultHits > 0)
                        defaultHitsNumber++;
                    int coveredSwitchBranches = 0;
                    final int switchBranchesNumber = dataFile.read();
                    for (int b = 0; b < switchBranchesNumber; b++) {
                        final long branchHit = dataFile.readLong();
                        if (branchHit > 0)
                            coveredSwitchBranches++;
                    }
                    if (coveredSwitchBranches == switchBranchesNumber)
                        branchHitNumber++;
                }
                if (hits > 0) {
                    if (totalHits == trueHits + falseHits) {
                        if (defaultHitsNumber == switchNumber && branchHitNumber == switchNumber) {
                            lineData.setStatus(LineCoverage.FULL);
                            continue;
                        }
                    }
                    lineData.setStatus(LineCoverage.PARTIAL);
                } else {
                    lineData.setStatus(LineCoverage.NONE);
                }
            }
        }
    } catch (IOException e) {
        LOG.info(e);
    } finally {
        if (dataFile != null) {
            try {
                dataFile.close();
            } catch (IOException e) {
                LOG.error(e);
            }
        }
    }
    return projectInfo;
}
Also used : LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ProjectData(com.intellij.rt.coverage.data.ProjectData) FileInputStream(java.io.FileInputStream)

Example 4 with ClassData

use of com.intellij.rt.coverage.data.ClassData in project intellij-community by JetBrains.

the class SimpleCoverageAnnotator method collectBaseFileCoverage.

@Nullable
protected FileCoverageInfo collectBaseFileCoverage(@NotNull final VirtualFile file, @NotNull final Annotator annotator, @NotNull final ProjectData projectData, @NotNull final Map<String, String> normalizedFiles2Files) {
    final String filePath = normalizeFilePath(file.getPath());
    // process file
    final FileCoverageInfo info;
    final ClassData classData = getClassData(filePath, projectData, normalizedFiles2Files);
    if (classData != null) {
        // fill info from coverage data
        info = fileInfoForCoveredFile(classData);
    } else {
        // file wasn't mentioned in coverage information
        info = fillInfoForUncoveredFile(VfsUtilCore.virtualToIoFile(file));
    }
    if (info != null) {
        annotator.annotateFile(filePath, info);
    }
    return info;
}
Also used : ClassData(com.intellij.rt.coverage.data.ClassData) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ClassData

use of com.intellij.rt.coverage.data.ClassData in project intellij-plugins by JetBrains.

the class DartCoverageRunner method doLoadCoverageData.

@Nullable
private static ProjectData doLoadCoverageData(@NotNull final File sessionDataFile, @NotNull final DartCoverageSuite coverageSuite) {
    final ProcessHandler coverageProcess = coverageSuite.getCoverageProcess();
    // coverageProcess == null means that we are switching to data gathered earlier
    if (coverageProcess != null) {
        for (int i = 0; i < 100; ++i) {
            ProgressManager.checkCanceled();
            if (coverageProcess.waitFor(100)) {
                break;
            }
        }
        if (!coverageProcess.isProcessTerminated()) {
            coverageProcess.destroyProcess();
            return null;
        }
    }
    final Project project = coverageSuite.getProject();
    final String contextFilePath = coverageSuite.getContextFilePath();
    if (project == null || contextFilePath == null) {
        return null;
    }
    final String contextId = DartAnalysisServerService.getInstance(project).execution_createContext(contextFilePath);
    if (contextId == null) {
        return null;
    }
    final ProjectData projectData = new ProjectData();
    try {
        DartCoverageData data = new Gson().fromJson(new BufferedReader(new FileReader(sessionDataFile)), DartCoverageData.class);
        if (data == null) {
            LOG.warn("Coverage file does not contain valid data.");
            return null;
        }
        for (Map.Entry<String, SortedMap<Integer, Integer>> entry : data.getMergedDartFileCoverageData().entrySet()) {
            ProgressManager.checkCanceled();
            String filePath = getFileForUri(project, contextId, entry.getKey());
            if (filePath == null) {
                // File is not found.
                continue;
            }
            SortedMap<Integer, Integer> lineHits = entry.getValue();
            ClassData classData = projectData.getOrCreateClassData(filePath);
            if (lineHits.size() == 0) {
                classData.setLines(new LineData[1]);
                continue;
            }
            LineData[] lines = new LineData[lineHits.lastKey() + 1];
            for (Map.Entry<Integer, Integer> hit : lineHits.entrySet()) {
                LineData lineData = new LineData(hit.getKey(), null);
                lineData.setHits(hit.getValue());
                lines[hit.getKey()] = lineData;
            }
            classData.setLines(lines);
        }
    } catch (FileNotFoundException | JsonSyntaxException e) {
        LOG.warn(e);
    } finally {
        DartAnalysisServerService.getInstance(project).execution_deleteContext(contextId);
    }
    return projectData;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) Project(com.intellij.openapi.project.Project) LineData(com.intellij.rt.coverage.data.LineData) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClassData(com.intellij.rt.coverage.data.ClassData) SortedMap(java.util.SortedMap) BufferedReader(java.io.BufferedReader) ProcessHandler(com.intellij.execution.process.ProcessHandler) FileReader(java.io.FileReader) Map(java.util.Map) SortedMap(java.util.SortedMap) ProjectData(com.intellij.rt.coverage.data.ProjectData) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ClassData (com.intellij.rt.coverage.data.ClassData)13 LineData (com.intellij.rt.coverage.data.LineData)9 ProjectData (com.intellij.rt.coverage.data.ProjectData)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Map (java.util.Map)4 Nullable (org.jetbrains.annotations.Nullable)4 PsiFile (com.intellij.psi.PsiFile)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 CoverageReport (com.google.jstestdriver.idea.rt.coverage.CoverageReport)2 Module (com.intellij.openapi.module.Module)2 DataInputStream (java.io.DataInputStream)2 List (java.util.List)2 NotNull (org.jetbrains.annotations.NotNull)2 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ExportToHTMLSettings (com.intellij.codeEditor.printing.ExportToHTMLSettings)1 ProcessHandler (com.intellij.execution.process.ProcessHandler)1 LcovCoverageReport (com.intellij.javascript.testFramework.coverage.LcovCoverageReport)1