Search in sources :

Example 16 with TestCase

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

the class AdditionalGreedyAlgorithm method selectTestCase.

@Override
public TestCase selectTestCase(List<TestCase> testCases) {
    Collections.sort(testCases, comp);
    TestCase next = testCases.get(0);
    return next;
}
Also used : TestCase(org.kanonizo.framework.objects.TestCase)

Example 17 with TestCase

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

the class ScytheInstrumenter method collectCoverage.

@Override
public void collectCoverage() {
    if (SCYTHE_READ) {
        if (SCYTHE_FILE.exists()) {
            Gson gson = getGson();
            try {
                Framework.getInstance().getDisplay().notifyTaskStart("Reading Coverage File", true);
                ScytheInstrumenter inst = gson.fromJson(new FileReader(SCYTHE_FILE), ScytheInstrumenter.class);
                // removing loading window
                Framework.getInstance().getDisplay().reportProgress(1, 1);
                this.linesCovered = inst.linesCovered;
                this.branchesCovered = inst.branchesCovered;
                this.testSuite = inst.testSuite;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            throw new RuntimeException("Scythe Coverage file is missing. Ensure that -Dscythe_output_dir exists and -Dscythe_filename exists within that directory");
        }
    } else {
        try {
            Util.suppressOutput();
            Framework.getInstance().getDisplay().notifyTaskStart("Running Test Cases", true);
            for (TestCase testCase : testSuite.getTestCases()) {
                try {
                    testCase.run();
                    // debug code to find out where/why failures are occurring. Use
                    // breakpoints after execution to locate failures
                    ClassAnalyzer.collectHitCounters(true);
                    linesCovered.put(testCase, collectLines(testCase));
                    branchesCovered.put(testCase, collectBranches(testCase));
                    ClassAnalyzer.resetCoverage();
                    Framework.getInstance().getDisplay().reportProgress((double) testSuite.getTestCases().indexOf(testCase) + 1, testSuite.getTestCases().size());
                } catch (Throwable e) {
                    e.printStackTrace();
                // as much as I hate to catch throwables, it has to be done in this
                // instance because not all tests can be guaranteed to run at all
                // properly, and sometimes the Java API will
                // shutdown without this line
                }
            }
            Util.resumeOutput();
            System.out.println("");
            logger.info("Finished instrumentation");
        } catch (final Exception e) {
            // runtime startup exception
            reportException(e);
        }
        if (!SCYTHE_READ && SCYTHE_WRITE) {
            if (!SCYTHE_FILE.exists()) {
                try {
                    SCYTHE_FILE.createNewFile();
                } catch (IOException e) {
                    logger.debug("Failed to create coverage file");
                }
            }
            Gson gson = getGson();
            String serialised = gson.toJson(this);
            try {
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(SCYTHE_FILE, false));
                out.write(serialised.getBytes());
                out.flush();
            } catch (FileNotFoundException e) {
                logger.debug("Output file is missing!");
                e.printStackTrace();
            } catch (IOException e) {
                logger.debug("Failed to write output");
                e.printStackTrace();
            }
        }
    }
}
Also used : TestCase(org.kanonizo.framework.objects.TestCase) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) FileReader(java.io.FileReader) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 18 with TestCase

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

Example 19 with TestCase

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

Example 20 with TestCase

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

the class Mutation method parseKillMap.

private static void parseKillMap(File kill, TestSuite testSuite) {
    CSVParser parser = null;
    try {
        parser = new CSVParser(new FileReader(kill), CSVFormat.DEFAULT);
        for (CSVRecord record : parser.getRecords()) {
            if (record.getRecordNumber() == 0) {
                continue;
            }
            int testCase = Integer.parseInt(record.get(0));
            int mutantKilled = Integer.parseInt(record.get(1));
            TestCase test = testSuite.getOriginalOrdering().get(testCase - 1);
            if (!killMap.containsKey(test)) {
                killMap.put(test, new ArrayList<Mutant>());
            }
            killMap.get(test).addAll(getMutants(mutant -> mutant.getMutantId() == mutantKilled));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            parser.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : TestSuite(org.kanonizo.framework.objects.TestSuite) Predicate(java.util.function.Predicate) CSVRecord(org.apache.commons.csv.CSVRecord) Scanner(java.util.Scanner) IOException(java.io.IOException) HashMap(java.util.HashMap) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) TestCase(org.kanonizo.framework.objects.TestCase) List(java.util.List) Util(org.kanonizo.util.Util) CSVFormat(org.apache.commons.csv.CSVFormat) ClassAnalyzer(com.scythe.instrumenter.analysis.ClassAnalyzer) Map(java.util.Map) CSVParser(org.apache.commons.csv.CSVParser) FileReader(java.io.FileReader) TestCase(org.kanonizo.framework.objects.TestCase) CSVParser(org.apache.commons.csv.CSVParser) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException)

Aggregations

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