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