Skip to content

Commit

Permalink
feat: add API to get hit counts
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb committed Jun 22, 2021
1 parent 12ec5ac commit 941c27c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,30 @@ private void buildCoverage(List<Map.Entry<TestCaseInfo, BlockMetrics>> sublist,
JSONObject currentValues = jsonTestTargets.getJSONObject((String) key);
((List) currentValues.get("statements")).stream()
.map(list -> ((Map) list).get("sl"))
.forEach(line ->
CloverReader.coverage.addCoverage(testClassName, testName, targetClassName, (Integer) line,
this.fileInfo.getNamedClass(this.fileInfo.getName().split("\\.")[0])
.forEach(line -> {
try {
final int hitCount = this.fileInfo.getNamedClass(this.fileInfo.getName().split("\\.")[0])
.getAllMethods()
.stream()
.flatMap(methodInfo -> methodInfo.getStatements().stream())
.filter(statementInfo -> statementInfo.getStartLine() == (Integer)line)
.filter(statementInfo -> statementInfo.getStartLine() == (Integer) line)
.findFirst()
.get()
.getHitCount()
)
);
.getHitCount();
CloverReader.coverage.addCoverage(testClassName, testName, targetClassName, (Integer) line, hitCount);
} catch (Exception e) {
e.printStackTrace();
final int hitCount = this.fileInfo.getNamedClass(this.fileInfo.getName().split("\\.")[0])
.getAllMethods()
.stream()
.flatMap(methodInfo -> methodInfo.getStatements().stream())
.filter(statementInfo -> statementInfo.getStartLine() == (Integer) line)
.findFirst()
.get()
.getHitCount();
CloverReader.coverage.addCoverage(testClassName, testName, targetClassName, (Integer) line, hitCount);
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class CloverReader {
* @return a map, that associate test method names and the map of executed line in each classes
*/
public Coverage read(String directory) {
coverage = new Coverage();
final File rootDirectoryOfCloverFiles = new File(directory + ROOT_DIRECTORY);
System.out.println("Reading Clover data " + rootDirectoryOfCloverFiles.getAbsolutePath());
HtmlReporter.runReport(new String[]{
"-i", rootDirectoryOfCloverFiles.getAbsolutePath() + DATABASE_FILE,
"-o", rootDirectoryOfCloverFiles.getAbsolutePath() + REPORT_DIRECTORY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ public List<LineCoverage> getCoverages() {
public boolean contains(int line) {
return this.coverages.stream().anyMatch(lineCoverage -> lineCoverage.line == line);
}

public int getHitCountForLine(int line) {
return this.coverages.stream()
.filter(coverage -> coverage.line == line)
.findFirst()
.orElse(new LineCoverage(0, 0))
.hitCount;
}

@Override
public String toString() {
return "ClassCoverage{" +
"className='" + className + '\'' +
", coverages=" + coverages +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package eu.stamp_project.diff_test_selection.coverage;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author Benjamin DANGLOT
Expand Down Expand Up @@ -44,4 +42,19 @@ public List<LineCoverage> getCoverageForTestClassTestMethodAndClassNames(String
public Map<String, ClassCoverage> getTestMethodCoverageForClassName(String testClassName, String testMethodName) {
return this.testClassCoverage.get(testClassName).getTestMethodCoverage(testMethodName);
}

public Map<String, Integer> getHitCountFromClassNameForLineForAll(String className, int line) {
final Map<String, Integer> allHitCountFromClassNameForLine = new HashMap<>();
for (TestClassCoverage value : this.testClassCoverage.values()) {
allHitCountFromClassNameForLine.putAll(value.getHitCountFromClassNameForLineForAll(className, line));
}
return allHitCountFromClassNameForLine;
}

@Override
public String toString() {
return "Coverage{" +
"testClassCoverage=" + testClassCoverage.toString() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public LineCoverage(int line, int hitCount) {
this.line = line;
this.hitCount = hitCount;
}

@Override
public String toString() {
return "LineCoverage{" +
"line=" + line +
", hitCount=" + hitCount +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,20 @@ public Map<String, ClassCoverage> getTestMethodCoverage(String testMethodName) {
return this.testMethodsCoverage.get(testMethodName).classCoverageList;
}

public Map<String, Integer> getHitCountFromClassNameForLineForAll(String className, int line) {
final Map<String, Integer> hitCountForLinePerTestMethodName = new HashMap<>();
for (TestMethodCoverage testMethodCoverage : testMethodsCoverage.values()) {
final int hitCountFromClassNameForLine = testMethodCoverage.getHitCountFromClassNameForLine(className, line);
hitCountForLinePerTestMethodName.put(this.testClassName + "#" + testMethodCoverage.testMethodName, hitCountFromClassNameForLine);
}
return hitCountForLinePerTestMethodName;
}

@Override
public String toString() {
return "TestClassCoverage{" +
"testClassName='" + testClassName + '\'' +
", testMethodsCoverage=" + testMethodsCoverage +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ public List<LineCoverage> getCoverageForClass(String className) {
return this.classCoverageList.get(className).getCoverages();
}

public int getHitCountFromClassNameForLine(String className, int line) {
return this.classCoverageList.get(className).getHitCountForLine(line);
}

@Override
public String toString() {
return "TestMethodCoverage{" +
"testMethodName='" + testMethodName + '\'' +
", classCoverageList=" + classCoverageList +
'}';
}
}

0 comments on commit 941c27c

Please sign in to comment.