Search in sources :

Example 1 with Line

use of org.kanonizo.framework.objects.Line 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 Line

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

the class Schwa method getTestsCoveringClass.

private List<TestCase> getTestsCoveringClass(List<TestCase> candidates, String filePath) {
    String fullPath = fw.getRootFolder().getAbsolutePath() + File.separator + filePath;
    File javaFile = new File(fullPath);
    try {
        List<String> lines = Files.readLines(javaFile, Charset.defaultCharset());
        Optional<String> pkgOpt = lines.stream().filter(line -> line.startsWith("package")).findFirst();
        String pkg;
        if (pkgOpt.isPresent()) {
            pkg = pkgOpt.get();
            pkg = pkg.substring("package".length() + 1, pkg.length() - 1);
        } else {
            pkg = "";
        }
        String className = filePath.substring(filePath.lastIndexOf(File.separator) + 1, filePath.length() - ".java".length());
        ClassUnderTest cut = ClassStore.get(pkg.isEmpty() ? className : pkg + "." + className);
        if (cut == null) {
            // test class
            return Collections.emptyList();
        }
        Set<Line> linesInCut = inst.getLines(cut);
        return candidates.stream().filter(tc -> inst.getLinesCovered(tc).stream().anyMatch(l -> linesInCut.contains(l))).collect(Collectors.toList());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Collections.emptyList();
}
Also used : ClassStore(org.kanonizo.framework.ClassStore) Framework(org.kanonizo.Framework) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) ArrayList(java.util.ArrayList) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) AdditionalComparator(org.kanonizo.algorithms.heuristics.comparators.AdditionalComparator) Charset(java.nio.charset.Charset) Files(com.google.common.io.Files) Gson(com.google.gson.Gson) Prerequisite(org.kanonizo.annotations.Prerequisite) Iterator(java.util.Iterator) Set(java.util.Set) ConditionalParameter(org.kanonizo.annotations.ConditionalParameter) IOException(java.io.IOException) Algorithm(org.kanonizo.annotations.Algorithm) OptionProvider(org.kanonizo.annotations.OptionProvider) Collectors(java.util.stream.Collectors) File(java.io.File) TestCasePrioritiser(org.kanonizo.algorithms.TestCasePrioritiser) List(java.util.List) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) GreedyComparator(org.kanonizo.algorithms.heuristics.comparators.GreedyComparator) Optional(java.util.Optional) Line(org.kanonizo.framework.objects.Line) FileReader(java.io.FileReader) Comparator(java.util.Comparator) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Line(org.kanonizo.framework.objects.Line) IOException(java.io.IOException) File(java.io.File) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest)

Example 3 with Line

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

the class ScytheInstrumenter method collectLines.

private Set<org.kanonizo.framework.objects.Line> collectLines(TestCase testCase) {
    Set<org.kanonizo.framework.objects.Line> 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.Line> lines = ClassAnalyzer.getCoverableLines(cl.getName());
            Set<com.scythe.instrumenter.instrumentation.objectrepresentation.Line> linesCovered = lines.stream().filter(line -> line.getHits() > 0).collect(Collectors.toSet());
            Set<org.kanonizo.framework.objects.Line> kanLines = linesCovered.stream().map(line -> LineStore.with(parent, line.getLineNumber())).collect(Collectors.toSet());
            covered.addAll(kanLines);
        }
    }
    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) Line(org.kanonizo.framework.objects.Line) HashSet(java.util.HashSet) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest)

Aggregations

ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Framework (org.kanonizo.Framework)3 ClassUnderTest (org.kanonizo.framework.objects.ClassUnderTest)3 Line (org.kanonizo.framework.objects.Line)3 Gson (com.google.gson.Gson)2 Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)2 File (java.io.File)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 ClassStore (org.kanonizo.framework.ClassStore)2 Instrumenter (org.kanonizo.framework.instrumentation.Instrumenter)2