Search in sources :

Example 1 with TestCase

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

the class JUnit4TestRunner method runTest.

@Override
public KanonizoTestResult runTest(TestCase tc) {
    Request request = getRequest(tc);
    Runner testRunner = request.getRunner();
    Result testResult = runner.run(testRunner);
    List<KanonizoTestFailure> failures = testResult.getFailures().stream().map(failure -> new KanonizoTestFailure(failure.getException(), failure.getTrace())).collect(Collectors.toList());
    return new KanonizoTestResult(tc.getTestClass(), tc.getMethod(), testResult.wasSuccessful(), failures, testResult.getRunTime());
}
Also used : Arrays(java.util.Arrays) JUnitCore(org.junit.runner.JUnitCore) Result(org.junit.runner.Result) Request(org.junit.runner.Request) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) TestCase(org.kanonizo.framework.objects.TestCase) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Runner(org.junit.runner.Runner) ClassRequest(org.junit.internal.requests.ClassRequest) KanonizoTestFailure(org.kanonizo.junit.KanonizoTestFailure) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) AssumptionViolatedException(org.junit.AssumptionViolatedException) BlockJUnit4ClassRunnerWithParameters(org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Iterator(java.util.Iterator) EachTestNotifier(org.junit.internal.runners.model.EachTestNotifier) Filter(org.junit.runner.manipulation.Filter) Description(org.junit.runner.Description) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Ignore(org.junit.Ignore) RunNotifier(org.junit.runner.notification.RunNotifier) LogManager(org.apache.logging.log4j.LogManager) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Runner(org.junit.runner.Runner) KanonizoTestFailure(org.kanonizo.junit.KanonizoTestFailure) Request(org.junit.runner.Request) ClassRequest(org.junit.internal.requests.ClassRequest) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult) Result(org.junit.runner.Result) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult)

Example 2 with TestCase

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

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

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

the class TestCasePrioritiser method generateSolution.

@Override
protected final void generateSolution() {
    TestSuite suite = problem.clone().getTestSuite();
    List<TestCase> testCases = suite.getTestCases();
    List<TestCase> orderedTestCases = new ArrayList<>();
    init(testCases);
    while (!testCases.isEmpty() && !shouldFinish()) {
        TestCase tc = selectTestCase(testCases);
        testCases.remove(tc);
        orderedTestCases.add(tc);
        fw.notifyTestCaseSelection(tc);
        fw.getDisplay().reportProgress(orderedTestCases.size(), testCases.size() + orderedTestCases.size());
    }
    if (!testCases.isEmpty()) {
        StoppingCondition terminatingStoppingCondition = stoppingConditions.stream().filter(cond -> cond.shouldFinish(this)).findFirst().get();
        logger.info("Algorithm terminated by " + terminatingStoppingCondition.getClass().getSimpleName());
        orderedTestCases.addAll(testCases);
    }
    suite.setTestCases(orderedTestCases);
    fw.getDisplay().fireTestSuiteChange(suite);
    setCurrentOptimal(suite);
}
Also used : TestSuite(org.kanonizo.framework.objects.TestSuite) StoppingCondition(org.kanonizo.algorithms.stoppingconditions.StoppingCondition) TestCase(org.kanonizo.framework.objects.TestCase) ArrayList(java.util.ArrayList)

Example 5 with TestCase

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

the class Schwa method init.

public void init(List<TestCase> testCases) {
    super.init(testCases);
    // run schwa
    try {
        boolean createTemp = !validSchwaFile();
        if (createTemp) {
            SCHWA_FILE = File.createTempFile("schwa-json-output", ".tmp");
            Framework.getInstance().getDisplay().notifyTaskStart("Running Schwa", true);
            runProcess(SCHWA_FILE, "schwa", fw.getRootFolder().getAbsolutePath(), "-j");
            Framework.getInstance().getDisplay().reportProgress(1, 1);
        }
        Gson gson = new Gson();
        SchwaRoot root = gson.fromJson(new FileReader(SCHWA_FILE), SchwaRoot.class);
        classes = root.getChildren().stream().filter(cl -> cl.getPath().endsWith(".java")).collect(Collectors.toList());
        // sort classes by probability of containing a fault
        Collections.sort(classes, Comparator.comparingDouble(o -> o.getProb()));
        if (createTemp) {
            SCHWA_FILE.delete();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
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) Gson(com.google.gson.Gson) FileReader(java.io.FileReader) 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