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();
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations