Search in sources :

Example 1 with Branch

use of org.kanonizo.framework.objects.Branch in project kanonizo by kanonizo.

the class CoverageWriter method prepareCsv.

@Override
public void prepareCsv() {
    String[] headers = new String[] { "Class", "ClassId", "NumLinesCovered", "NumLinesMissed", "LinesCovered", "LinesMissed", "PercentageLineCoverage", "Total Branches", "BranchesCovered", "BranchesMissed", "PercentageBranchCoverage" };
    setHeaders(headers);
    List<ClassUnderTest> cuts = system.getClassesUnderTest();
    List<TestCase> testCases = system.getTestSuite().getTestCases();
    Instrumenter inst = Framework.getInstance().getInstrumenter();
    for (ClassUnderTest cut : cuts) {
        if (!cut.getCUT().isInterface()) {
            Set<Line> linesCovered = new HashSet<>();
            Set<Branch> branchesCovered = new HashSet<>();
            for (TestCase tc : testCases) {
                Set<Line> lines = inst.getLinesCovered(tc).stream().filter(line -> line.getParent().equals(cut)).collect(Collectors.toSet());
                Set<Branch> branches = inst.getBranchesCovered(tc);
                linesCovered.addAll(lines);
                branchesCovered.addAll(branches);
            }
            int totalLines = inst.getTotalLines(cut);
            int totalBranches = inst.getTotalBranches(cut);
            Set<Line> linesMissed = cut.getLines().stream().filter(line -> !linesCovered.contains(line)).collect(HashSet::new, HashSet::add, HashSet::addAll);
            Set<Branch> branchesMissed = cut.getBranches().stream().filter(branch -> !branchesCovered.contains(branch)).collect(HashSet::new, HashSet::add, HashSet::addAll);
            List<Line> orderedLinesCovered = new ArrayList<>(linesCovered);
            Collections.sort(orderedLinesCovered);
            List<Line> orderedLinesMissed = new ArrayList<Line>(linesMissed);
            Collections.sort(orderedLinesMissed);
            double percentageCoverage = totalLines > 0 ? (double) linesCovered.size() / totalLines : 0;
            double percentageBranch = totalBranches > 0 ? (double) branchesCovered.size() / totalBranches : 0;
            String[] csv = new String[] { cut.getCUT().getName(), Integer.toString(cut.getId()), Integer.toString(linesCovered.size()), Integer.toString(linesMissed.size()), linesCovered.size() > 0 ? orderedLinesCovered.stream().map(line -> Integer.toString(line.getLineNumber())).reduce((lineNumber, lineNumber2) -> lineNumber + ":" + lineNumber2).get() : "", linesMissed.size() > 0 ? orderedLinesMissed.stream().map(line -> Integer.toString(line.getLineNumber())).reduce((lineNumber, lineNumber2) -> lineNumber + ":" + lineNumber2).get() : "", Double.toString(percentageCoverage), Integer.toString(totalBranches), Double.toString(branchesCovered.size()), Double.toString(branchesMissed.size()), Double.toString(percentageBranch) };
            addRow(csv);
        }
    }
}
Also used : Branch(org.kanonizo.framework.objects.Branch) Set(java.util.Set) Framework(org.kanonizo.Framework) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) HashSet(java.util.HashSet) List(java.util.List) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) HashSetCollector(org.kanonizo.util.HashSetCollector) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Line(org.kanonizo.framework.objects.Line) Collections(java.util.Collections) ArrayList(java.util.ArrayList) Line(org.kanonizo.framework.objects.Line) TestCase(org.kanonizo.framework.objects.TestCase) Branch(org.kanonizo.framework.objects.Branch) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) HashSet(java.util.HashSet)

Example 2 with Branch

use of org.kanonizo.framework.objects.Branch in project kanonizo by kanonizo.

the class ScytheInstrumenter method collectBranches.

private Set<org.kanonizo.framework.objects.Branch> collectBranches(TestCase testCase) {
    Set<org.kanonizo.framework.objects.Branch> covered = new HashSet<>();
    List<Class<?>> changedClasses = ClassAnalyzer.getChangedClasses();
    for (Class<?> cl : changedClasses) {
        if (ClassStore.get(cl.getName()) != null) {
            ClassUnderTest parent = ClassStore.get(cl.getName());
            List<com.scythe.instrumenter.instrumentation.objectrepresentation.Branch> branches = ClassAnalyzer.getCoverableBranches(cl.getName());
            Set<com.scythe.instrumenter.instrumentation.objectrepresentation.Branch> branchesCovered = branches.stream().filter(branch -> branch.getHits() > 0).collect(Collectors.toSet());
            Set<Branch> kanBranches = branchesCovered.stream().map(branch -> BranchStore.with(parent, branch.getLineNumber(), branch.getGoalId())).collect(Collectors.toSet());
            covered.addAll(kanBranches);
        }
    }
    return covered;
}
Also used : Arrays(java.util.Arrays) ClassStore(org.kanonizo.framework.ClassStore) LineStore(org.kanonizo.framework.objects.LineStore) TestSuite(org.kanonizo.framework.objects.TestSuite) Branch(org.kanonizo.framework.objects.Branch) HashMap(java.util.HashMap) Framework(org.kanonizo.Framework) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) InstrumentationProperties(com.scythe.instrumenter.InstrumentationProperties) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) JsonReader(com.google.gson.stream.JsonReader) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) ClassReplacementTransformer(com.scythe.instrumenter.instrumentation.ClassReplacementTransformer) HashSet(java.util.HashSet) HashSetCollector(org.kanonizo.util.HashSetCollector) Gson(com.google.gson.Gson) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Map(java.util.Map) Goal(org.kanonizo.framework.objects.Goal) JsonWriter(com.google.gson.stream.JsonWriter) InstrumentingClassLoader(com.scythe.instrumenter.instrumentation.InstrumentingClassLoader) Iterator(java.util.Iterator) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) BranchStore(org.kanonizo.framework.objects.BranchStore) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) ClassAnalyzer(com.scythe.instrumenter.analysis.ClassAnalyzer) Line(org.kanonizo.framework.objects.Line) FileReader(java.io.FileReader) Comparator(java.util.Comparator) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) TestCaseStore(org.kanonizo.framework.TestCaseStore) Branch(org.kanonizo.framework.objects.Branch) HashSet(java.util.HashSet) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest)

Aggregations

ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Framework (org.kanonizo.Framework)2 Instrumenter (org.kanonizo.framework.instrumentation.Instrumenter)2 Branch (org.kanonizo.framework.objects.Branch)2 ClassUnderTest (org.kanonizo.framework.objects.ClassUnderTest)2 Line (org.kanonizo.framework.objects.Line)2 SystemUnderTest (org.kanonizo.framework.objects.SystemUnderTest)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeAdapter (com.google.gson.TypeAdapter)1 JsonReader (com.google.gson.stream.JsonReader)1 JsonWriter (com.google.gson.stream.JsonWriter)1 InstrumentationProperties (com.scythe.instrumenter.InstrumentationProperties)1 Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)1 ClassAnalyzer (com.scythe.instrumenter.analysis.ClassAnalyzer)1