Search in sources :

Example 1 with ClassUnderTest

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

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

the class GreedyAlgorithmTest method testOrdering.

@Test
public void testOrdering() {
    List<TestCase> testCases = algorithm.getCurrentOptimal().getTestCases();
    ClassUnderTest cut = ClassStore.get("sample_classes.Stack");
    for (int i = 0; i < testCases.size() - 2; i++) {
        TestCase test1 = testCases.get(i);
        TestCase test2 = testCases.get(i + 1);
        int linesCovered1 = scytheInst.getLinesCovered(test1).size();
        int linesCovered2 = scytheInst.getLinesCovered(test2).size();
        assertTrue("Test Case: " + test1 + " has lower coverage than " + test2, linesCovered1 >= linesCovered2);
    }
}
Also used : TestCase(org.kanonizo.framework.objects.TestCase) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) Test(org.junit.Test) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest)

Example 3 with ClassUnderTest

use of org.kanonizo.framework.objects.ClassUnderTest 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 4 with ClassUnderTest

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

the class Framework method loadClasses.

public void loadClasses() throws ClassNotFoundException {
    sut = new SystemUnderTest();
    List<File> sourceFiles = findClasses(sourceFolder);
    Premain.instrument = true;
    for (File file : sourceFiles) {
        Class<?> cl = loadClassFromFile(file);
        sut.addClass(new ClassUnderTest(cl));
        logger.info("Added class " + cl.getName());
    }
    Premain.instrument = false;
    List<File> testFiles = findClasses(testFolder);
    for (File file : testFiles) {
        Class<?> cl = loadClassFromFile(file);
        if (cl != null) {
            if (Util.isTestClass(cl)) {
                List<Method> testMethods = TestingUtils.getTestMethods(cl);
                logger.info("Adding " + testMethods.size() + " test methods from " + cl.getName());
                for (Method m : testMethods) {
                    if (TestingUtils.isParameterizedTest(cl, m)) {
                        Optional<Method> parameterMethod = Arrays.asList(cl.getMethods()).stream().filter(method -> method.getAnnotation(Parameters.class) != null).findFirst();
                        if (parameterMethod.isPresent()) {
                            try {
                                Iterable<Object[]> parameters = (Iterable<Object[]>) parameterMethod.get().invoke(null, new Object[] {});
                                for (Object[] inst : parameters) {
                                    ParameterisedTestCase ptc = new ParameterisedTestCase(cl, m, inst);
                                    sut.addTestCase(ptc);
                                }
                            } catch (IllegalAccessException e) {
                                logger.error(e);
                            } catch (InvocationTargetException e) {
                                logger.error(e);
                            }
                        } else {
                            logger.error("Trying to create parameterized test case that has no parameter method");
                        }
                    } else {
                        TestCase t = new TestCase(cl, m);
                        sut.addTestCase(t);
                    }
                }
            } else {
                sut.addExtraClass(cl);
                logger.info("Adding supporting test class " + cl.getName());
            }
        }
    }
    ClassUnderTest.resetCount();
    logger.info("Finished adding source and test files. Total " + sut.getClassesUnderTest().size() + " classes and " + sut.getTestSuite().size() + " test cases");
}
Also used : Arrays(java.util.Arrays) Reflections(org.reflections.Reflections) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) APLCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APLCFunction) CoverageWriter(org.kanonizo.reporting.CoverageWriter) Gson(com.google.gson.Gson) Method(java.lang.reflect.Method) TestCaseSelectionListener(org.kanonizo.listeners.TestCaseSelectionListener) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) TestingUtils(org.kanonizo.junit.TestingUtils) Expose(com.google.gson.annotations.Expose) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) PropertyChangeListener(java.beans.PropertyChangeListener) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Display(org.kanonizo.display.Display) InstrumentedFitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.InstrumentedFitnessFunction) APBCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APBCFunction) Parameters(org.junit.runners.Parameterized.Parameters) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) ClassParser(org.apache.bcel.classfile.ClassParser) JsonReader(com.google.gson.stream.JsonReader) APFDFunction(org.kanonizo.algorithms.metaheuristics.fitness.APFDFunction) ArrayList(java.util.ArrayList) CsvWriter(org.kanonizo.reporting.CsvWriter) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Prerequisite(org.kanonizo.annotations.Prerequisite) JsonWriter(com.google.gson.stream.JsonWriter) PropertyChangeEvent(java.beans.PropertyChangeEvent) MutationSearchAlgorithm(org.kanonizo.algorithms.MutationSearchAlgorithm) JavaClass(org.apache.bcel.classfile.JavaClass) FitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.FitnessFunction) MiscStatsWriter(org.kanonizo.reporting.MiscStatsWriter) Iterator(java.util.Iterator) NullInstrumenter(org.kanonizo.instrumenters.NullInstrumenter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Algorithm(org.kanonizo.annotations.Algorithm) TestCaseOrderingWriter(org.kanonizo.reporting.TestCaseOrderingWriter) File(java.io.File) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) PropertyChangeSupport(java.beans.PropertyChangeSupport) FileReader(java.io.FileReader) NullDisplay(org.kanonizo.display.NullDisplay) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Parameters(org.junit.runners.Parameterized.Parameters) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TestCase(org.kanonizo.framework.objects.TestCase) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) File(java.io.File) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest)

Example 5 with ClassUnderTest

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

ClassUnderTest (org.kanonizo.framework.objects.ClassUnderTest)6 TestCase (org.kanonizo.framework.objects.TestCase)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Gson (com.google.gson.Gson)4 Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)4 File (java.io.File)4 FileReader (java.io.FileReader)4 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 Iterator (java.util.Iterator)4 LogManager (org.apache.logging.log4j.LogManager)4 Logger (org.apache.logging.log4j.Logger)4 Framework (org.kanonizo.Framework)4 Instrumenter (org.kanonizo.framework.instrumentation.Instrumenter)4 SystemUnderTest (org.kanonizo.framework.objects.SystemUnderTest)4 Util (org.kanonizo.util.Util)4