Search in sources :

Example 11 with TestCase

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

the class RandomAlgorithm method generateSolution.

@Override
protected void generateSolution() {
    TestSuite suite = problem.clone().getTestSuite();
    List<TestCase> testCases = suite.getTestCases();
    Collections.shuffle(testCases);
    suite.setTestCases(testCases);
    setCurrentOptimal(suite);
    fitnessEvaluations++;
}
Also used : TestSuite(org.kanonizo.framework.objects.TestSuite) TestCase(org.kanonizo.framework.objects.TestCase)

Example 12 with TestCase

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

the class RandomSearchAlgorithm method generateSolution.

@Override
public void generateSolution() {
    FitnessWriter writer = new FitnessWriter(this);
    List<TestCase> testCases = problem.getTestSuite().getTestCases();
    Display d = Framework.getInstance().getDisplay();
    System.out.println("Running Random Search");
    while (!shouldFinish()) {
        age++;
        TestSuite clone = getCurrentOptimal().clone();
        List<TestCase> randomOrdering = generateRandomOrder(testCases);
        clone.setTestCases(randomOrdering);
        fitnessEvaluations++;
        if (clone.fitter(getCurrentOptimal()).equals(clone)) {
            setCurrentOptimal(clone);
        }
        if (TRACK_GENERATION_FITNESS) {
            writer.addRow(age, getCurrentOptimal().getFitness());
        } else {
            writer.addRow(age, clone.getFitness());
        }
        d.reportProgress(Math.min((double) System.currentTimeMillis() - startTime, MAX_EXECUTION_TIME), MAX_EXECUTION_TIME);
    }
    System.out.println();
    writer.write();
}
Also used : TestSuite(org.kanonizo.framework.objects.TestSuite) TestCase(org.kanonizo.framework.objects.TestCase) FitnessWriter(org.kanonizo.reporting.FitnessWriter) Display(org.kanonizo.display.Display)

Example 13 with TestCase

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

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

the class KOptimalAlgorithm method selectOptimal.

private List<TestCase> selectOptimal(List<TestCase> testCases) {
    List<TestCase> cases = new ArrayList<TestCase>();
    double maxFitness = 0.0;
    for (int i = 0; i < testCases.size(); i++) {
        for (int j = 0; j < testCases.size(); j++) {
            if (i > j) {
                TestCase tc1 = testCases.get(i);
                TestCase tc2 = testCases.get(j);
                double fitness = getFitness(tc1, tc2);
                if (fitness > maxFitness) {
                    maxFitness = fitness;
                    cases.clear();
                    cases.add(tc1);
                    cases.add(tc2);
                }
            }
        }
    }
    return cases;
}
Also used : TestCase(org.kanonizo.framework.objects.TestCase) ArrayList(java.util.ArrayList)

Example 15 with TestCase

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

the class Schwa method selectTestCase.

@Override
public TestCase selectTestCase(List<TestCase> testCases) {
    while (testCasesForActive.size() == 0) {
        if (classes.size() > 0) {
            // pick next n classes to prioritise tests for, either CLASSES_PER_GROUP or all remaining classes
            active.clear();
            for (int i = 0; i < Math.min(CLASSES_PER_GROUP, classes.size()); i++) {
                active.add(classes.get(i));
            }
            classes.removeAll(active);
            // grab covering test cases
            testCasesForActive = getTestsCoveringClasses(testCases, active);
        } else {
            // if there are no more classes reported by Schwa, just add test cases in the order we found them
            testCasesForActive = testCases;
        }
        if (COMPARATOR != null) {
            // secondary objective sort for test cases
            testCasesForActive.sort(COMPARATOR);
        }
    }
    TestCase next = testCasesForActive.get(0);
    testCasesForActive.remove(next);
    return next;
}
Also used : TestCase(org.kanonizo.framework.objects.TestCase)

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