From 941c27c417aa4b835521dd4ef3f99f973bcc30e5 Mon Sep 17 00:00:00 2001 From: danglotb Date: Tue, 22 Jun 2021 17:38:36 +0200 Subject: [PATCH] feat: add API to get hit counts --- .../reporters/html/RenderFileAction.java | 26 ++++++++++++++----- .../clover/CloverReader.java | 2 ++ .../coverage/ClassCoverage.java | 16 ++++++++++++ .../coverage/Coverage.java | 21 ++++++++++++--- .../coverage/LineCoverage.java | 8 ++++++ .../coverage/TestClassCoverage.java | 16 ++++++++++++ .../coverage/TestMethodCoverage.java | 11 ++++++++ 7 files changed, 89 insertions(+), 11 deletions(-) diff --git a/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java b/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java index 8547cf1e4..9ecd137ed 100644 --- a/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java +++ b/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java @@ -319,18 +319,30 @@ private void buildCoverage(List> 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); + } + }); } } diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverReader.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverReader.java index a2d07f36b..ccc795e59 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverReader.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverReader.java @@ -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, diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/ClassCoverage.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/ClassCoverage.java index 34d15b650..cd7d2830d 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/ClassCoverage.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/ClassCoverage.java @@ -30,4 +30,20 @@ public List 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 + + '}'; + } } diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java index f0632a2e0..bae9efbd3 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java @@ -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 @@ -44,4 +42,19 @@ public List getCoverageForTestClassTestMethodAndClassNames(String public Map getTestMethodCoverageForClassName(String testClassName, String testMethodName) { return this.testClassCoverage.get(testClassName).getTestMethodCoverage(testMethodName); } + + public Map getHitCountFromClassNameForLineForAll(String className, int line) { + final Map 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() + + '}'; + } } diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/LineCoverage.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/LineCoverage.java index ab144471b..20d1dfb23 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/LineCoverage.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/LineCoverage.java @@ -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 + + '}'; + } } diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestClassCoverage.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestClassCoverage.java index 93c7cc100..26e0dc2d8 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestClassCoverage.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestClassCoverage.java @@ -41,4 +41,20 @@ public Map getTestMethodCoverage(String testMethodName) { return this.testMethodsCoverage.get(testMethodName).classCoverageList; } + public Map getHitCountFromClassNameForLineForAll(String className, int line) { + final Map 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 + + '}'; + } } diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java index 3a9c497f9..743831768 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java @@ -33,4 +33,15 @@ public List 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 + + '}'; + } }