Search in sources :

Example 6 with TestCase

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

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

the class KOptimalAlgorithm method selectTestCase.

@Override
public TestCase selectTestCase(List<TestCase> testCases) {
    if (bestK.size() == 0) {
        bestK = selectOptimal(testCases);
    }
    TestCase best = bestK.get(0);
    bestK.remove(0);
    cache.addAll(inst.getLinesCovered(best));
    return best;
}
Also used : TestCase(org.kanonizo.framework.objects.TestCase)

Example 8 with TestCase

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

the class GeneticAlgorithm method generateInitialPopulation.

protected void generateInitialPopulation() {
    logger.info("Generating initial population");
    for (int i = 0; i < POPULATION_SIZE; i++) {
        TestSuite clone = problem.clone().getTestSuite();
        List<Integer> testCaseOrdering = IntStream.range(0, clone.getTestCases().size()).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        List<TestCase> randomOrdering = new ArrayList<TestCase>();
        while (!testCaseOrdering.isEmpty()) {
            int index = RandomInstance.nextInt(testCaseOrdering.size());
            TestCase tc = clone.getTestCases().get(testCaseOrdering.get(index));
            randomOrdering.add(tc);
            testCaseOrdering.remove(index);
        }
        clone.setTestCases(randomOrdering);
        population.add(clone);
    }
}
Also used : TestSuite(org.kanonizo.framework.objects.TestSuite) TestCase(org.kanonizo.framework.objects.TestCase) ArrayList(java.util.ArrayList)

Example 9 with TestCase

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

the class APFDFunction method getGoalMap.

protected Map<Goal, Integer> getGoalMap() {
    Map<Goal, Integer> goalMap = new HashMap<>();
    List<TestCase> testCases = sut.getTestSuite().getTestCases();
    for (int i = 0; i < testCases.size(); i++) {
        final int ind = i;
        TestCase tc = testCases.get(i);
        Set<? extends Goal> goalsCovered = getCoveredGoals(tc);
        goalsCovered.forEach(goal -> {
            if (!goalMap.containsKey(goal)) {
                goalMap.put(goal, ind + 1);
            }
        });
    }
    return goalMap;
}
Also used : Goal(org.kanonizo.framework.objects.Goal) HashMap(java.util.HashMap) TestCase(org.kanonizo.framework.objects.TestCase)

Example 10 with TestCase

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

the class RandomSearchAlgorithm method generateRandomOrder.

private List<TestCase> generateRandomOrder(List<TestCase> testCases) {
    List<TestCase> unorderedCases = new ArrayList<TestCase>(testCases);
    List<TestCase> orderedCases = new ArrayList<TestCase>();
    while (unorderedCases.size() > 0) {
        int index = RandomInstance.nextInt(unorderedCases.size());
        TestCase chr = unorderedCases.get(index);
        orderedCases.add(chr);
        unorderedCases.remove(chr);
    }
    return orderedCases;
}
Also used : TestCase(org.kanonizo.framework.objects.TestCase) ArrayList(java.util.ArrayList)

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