Search in sources :

Example 1 with Instrumenter

use of org.kanonizo.framework.instrumentation.Instrumenter 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 Instrumenter

use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.

the class InstrumentedFitnessFunction method instrument.

public void instrument(TestSuite chrom) {
    Instrumenter inst = Framework.getInstance().getInstrumenter();
    inst.setTestSuite(chrom);
    inst.collectCoverage();
    calculateTotalGoalsCovered();
}
Also used : Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter)

Example 3 with Instrumenter

use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.

the class Main method setInstrumenter.

private static void setInstrumenter(Framework fw, String instrumenter) {
    Reflections r = Util.getReflections();
    Set<?> instrumenters = r.getTypesAnnotatedWith(org.kanonizo.annotations.Instrumenter.class).stream().map(cl -> {
        try {
            return cl.newInstance();
        } catch (IllegalAccessException | InstantiationException e) {
            logger.error(e);
        }
        return null;
    }).collect(Collectors.toSet());
    for (Object inst : instrumenters) {
        if (instrumenter.equals(((Instrumenter) inst).readableName())) {
            fw.setInstrumenter((org.kanonizo.framework.instrumentation.Instrumenter) inst);
        }
    }
}
Also used : TestSuite(org.kanonizo.framework.objects.TestSuite) ConsoleDisplay(org.kanonizo.display.ConsoleDisplay) Options(org.apache.commons.cli.Options) Reflections(org.reflections.Reflections) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) HelpFormatter(org.apache.commons.cli.HelpFormatter) MutationProperties(com.scythe.instrumenter.mutation.MutationProperties) DefaultParser(org.apache.commons.cli.DefaultParser) TestCase(org.kanonizo.framework.objects.TestCase) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) CommandLine(org.apache.commons.cli.CommandLine) Prerequisite(org.kanonizo.annotations.Prerequisite) SystemConfigurationException(org.kanonizo.exception.SystemConfigurationException) Method(java.lang.reflect.Method) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) InstrumentingClassLoader(com.scythe.instrumenter.instrumentation.InstrumentingClassLoader) Set(java.util.Set) Field(java.lang.reflect.Field) MissingOptionException(org.apache.commons.cli.MissingOptionException) Algorithm(org.kanonizo.annotations.Algorithm) Collectors(java.util.stream.Collectors) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) KanonizoFrame(org.kanonizo.display.fx.KanonizoFrame) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) Optional(java.util.Optional) Display(org.kanonizo.display.Display) LogManager(org.apache.logging.log4j.LogManager) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Reflections(org.reflections.Reflections)

Example 4 with Instrumenter

use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.

the class Framework method getAvailableInstrumenters.

public static List<Instrumenter> getAvailableInstrumenters() throws InstantiationException, IllegalAccessException {
    Reflections r = Util.getReflections();
    Set<Class<?>> algorithms = r.getTypesAnnotatedWith(org.kanonizo.annotations.Instrumenter.class);
    List<Instrumenter> algorithmsInst = algorithms.stream().map(cl -> {
        try {
            return (Instrumenter) cl.newInstance();
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }
        throw new RuntimeException("Could not instantiate one of more instrumenters");
    }).collect(Collectors.toList());
    return algorithmsInst;
}
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) JavaClass(org.apache.bcel.classfile.JavaClass) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) NullInstrumenter(org.kanonizo.instrumenters.NullInstrumenter) Reflections(org.reflections.Reflections)

Example 5 with Instrumenter

use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.

the class TestSuite method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("\n-------------------------------------------\nMAXIMUM FITNESS: " + String.format("%.4f", getFitness()) + "\n-------------------------------------------\n");
    Set<Line> covered = new HashSet<>();
    Instrumenter inst = Framework.getInstance().getInstrumenter();
    testCases.stream().forEach(tc -> {
        Set<Line> branches = inst.getLinesCovered(tc);
        covered.addAll(branches);
    });
    int coveredBranches = covered.size();
    int totalBranches = parent.getClassesUnderTest().stream().mapToInt(cut -> inst.getTotalLines(cut)).sum();
    sb.append("Line Coverage: " + (double) coveredBranches / (double) totalBranches);
    sb.append("\n-------------------------------------------\nMaximum fitness found by " + getFitnessFunction().getClass().getSimpleName() + "\n-------------------------------------------\n");
    return sb.toString();
}
Also used : FitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.FitnessFunction) InstrumentedFitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.InstrumentedFitnessFunction) Properties(org.kanonizo.Properties) APBCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APBCFunction) Set(java.util.Set) Disposable(org.kanonizo.Disposable) Framework(org.kanonizo.Framework) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Logger(org.apache.logging.log4j.Logger) APLCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APLCFunction) ClassAnalyzer(com.scythe.instrumenter.analysis.ClassAnalyzer) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Modifier(java.lang.reflect.Modifier) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) RandomInstance(org.kanonizo.util.RandomInstance) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) HashSet(java.util.HashSet)

Aggregations

Instrumenter (org.kanonizo.framework.instrumentation.Instrumenter)5 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)3 ArrayList (java.util.ArrayList)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 SystemUnderTest (org.kanonizo.framework.objects.SystemUnderTest)3 TestCase (org.kanonizo.framework.objects.TestCase)3 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Modifier (java.lang.reflect.Modifier)2 Optional (java.util.Optional)2 SearchAlgorithm (org.kanonizo.algorithms.SearchAlgorithm)2 APBCFunction (org.kanonizo.algorithms.metaheuristics.fitness.APBCFunction)2 APLCFunction (org.kanonizo.algorithms.metaheuristics.fitness.APLCFunction)2 FitnessFunction (org.kanonizo.algorithms.metaheuristics.fitness.FitnessFunction)2 InstrumentedFitnessFunction (org.kanonizo.algorithms.metaheuristics.fitness.InstrumentedFitnessFunction)2