use of org.kanonizo.framework.objects.ClassUnderTest 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.ClassUnderTest 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.ClassUnderTest 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.ClassUnderTest 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");
}
use of org.kanonizo.framework.objects.ClassUnderTest in project kanonizo by kanonizo.
the class ScytheInstrumenter method collectLines.
private Set<org.kanonizo.framework.objects.Line> collectLines(TestCase testCase) {
Set<org.kanonizo.framework.objects.Line> covered = new HashSet<>();
List<Class<?>> changedClasses = ClassAnalyzer.getChangedClasses();
for (Class<?> cl : changedClasses) {
if (ClassStore.get(cl.getName()) != null) {
ClassUnderTest parent = ClassStore.get(cl.getName());
List<com.scythe.instrumenter.instrumentation.objectrepresentation.Line> lines = ClassAnalyzer.getCoverableLines(cl.getName());
Set<com.scythe.instrumenter.instrumentation.objectrepresentation.Line> linesCovered = lines.stream().filter(line -> line.getHits() > 0).collect(Collectors.toSet());
Set<org.kanonizo.framework.objects.Line> kanLines = linesCovered.stream().map(line -> LineStore.with(parent, line.getLineNumber())).collect(Collectors.toSet());
covered.addAll(kanLines);
}
}
return covered;
}
Aggregations