-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: coverage map into dedicated classes (#986)
* refactor: use now dedicated object instead of map of map of map... * test: update the test with the new API * feat: add API to get hit counts
- Loading branch information
Showing
17 changed files
with
491 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/ClassCoverage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package eu.stamp_project.diff_test_selection.coverage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Benjamin DANGLOT | ||
* [email protected] | ||
* on 14/06/2021 | ||
*/ | ||
public class ClassCoverage { | ||
|
||
public final String className; | ||
|
||
public final List<LineCoverage> coverages; | ||
|
||
public ClassCoverage(String className) { | ||
this.className = className; | ||
this.coverages = new ArrayList<>(); | ||
} | ||
|
||
public void addCoverage(int line, int hitCounts) { | ||
this.coverages.add(new LineCoverage(line, hitCounts)); | ||
} | ||
|
||
public List<LineCoverage> getCoverages() { | ||
return this.coverages; | ||
} | ||
|
||
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 + | ||
'}'; | ||
} | ||
} |
90 changes: 35 additions & 55 deletions
90
...-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,60 @@ | ||
package eu.stamp_project.diff_test_selection.coverage; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* created by Benjamin DANGLOT | ||
* [email protected] | ||
* on 21/09/18 | ||
* <p> | ||
* This class is responsible to compute the Coverage of the provided diff. | ||
* @author Benjamin DANGLOT | ||
* [email protected] | ||
* on 14/06/2021 | ||
*/ | ||
public class Coverage { | ||
|
||
private Map<String, Set<Integer>> executedLinePerQualifiedName; | ||
|
||
private Map<String, Set<Integer>> modifiedLinePerQualifiedName; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(Coverage.class); | ||
public final Map<String, TestClassCoverage> testClassCoverage; | ||
|
||
public Coverage() { | ||
this.modifiedLinePerQualifiedName = new LinkedHashMap<>(); | ||
this.executedLinePerQualifiedName = new LinkedHashMap<>(); | ||
this.testClassCoverage = new LinkedHashMap<>(); | ||
} | ||
|
||
public void covered(String fullQualifiedName, Integer line) { | ||
if (!this.executedLinePerQualifiedName.containsKey(fullQualifiedName)) { | ||
this.executedLinePerQualifiedName.put(fullQualifiedName, new HashSet<>()); | ||
} | ||
if (this.modifiedLinePerQualifiedName.containsKey(fullQualifiedName) && | ||
this.modifiedLinePerQualifiedName.get(fullQualifiedName).contains(line) | ||
&& this.executedLinePerQualifiedName.get(fullQualifiedName).add(line)) { | ||
LOGGER.info(fullQualifiedName + ":" + line + " covered."); | ||
public void addCoverage(String testClassName, String testMethodName, String className, int line, int hitCounts) { | ||
if (!this.testClassCoverage.containsKey(testClassName)) { | ||
this.testClassCoverage.put(testClassName, new TestClassCoverage(testClassName)); | ||
} | ||
this.testClassCoverage.get(testClassName).addCoverage(testMethodName, className, line, hitCounts); | ||
} | ||
|
||
public void addModifiedLines(String fullQualifiedName, List<Integer> lines) { | ||
if (!this.modifiedLinePerQualifiedName.containsKey(fullQualifiedName)) { | ||
this.modifiedLinePerQualifiedName.put(fullQualifiedName, new HashSet<>()); | ||
} | ||
lines.forEach(this.modifiedLinePerQualifiedName.get(fullQualifiedName)::add); | ||
LOGGER.info(fullQualifiedName + ":" + lines.toString() + " are modified."); | ||
public Set<String> getTestClasses() { | ||
return this.testClassCoverage.keySet(); | ||
} | ||
|
||
public void addModifiedLine(String fullQualifiedName, Integer line) { | ||
if (!this.modifiedLinePerQualifiedName.containsKey(fullQualifiedName)) { | ||
this.modifiedLinePerQualifiedName.put(fullQualifiedName, new HashSet<>()); | ||
} | ||
this.modifiedLinePerQualifiedName.get(fullQualifiedName).add(line); | ||
LOGGER.info(fullQualifiedName + ":" + line + " is modified."); | ||
public Set<String> getClassesForTestClassAndMethodName(String testClassName, String testMethodName) { | ||
return this.testClassCoverage.get(testClassName).getClassesForTestMethodName(testMethodName); | ||
} | ||
|
||
@Deprecated | ||
public void addModifiedLines(final Map<String, List<Integer>> newModifiedLinesPerQualifiedName) { | ||
newModifiedLinesPerQualifiedName.keySet() | ||
.forEach(key -> { | ||
if (!this.modifiedLinePerQualifiedName.containsKey(key)) { | ||
this.modifiedLinePerQualifiedName.put(key, new HashSet<>()); | ||
} | ||
newModifiedLinesPerQualifiedName.get(key) | ||
.forEach(line -> { | ||
if (this.modifiedLinePerQualifiedName.get(key).add(line)) { | ||
LOGGER.info(key + ":" + line + " is modified."); | ||
} | ||
} | ||
); | ||
} | ||
); | ||
public Set<String> getTestMethodsForTestClassName(String testClassName) { | ||
return this.testClassCoverage.get(testClassName).getTestMethods(); | ||
} | ||
|
||
public Map<String, Set<Integer>> getExecutedLinePerQualifiedName() { | ||
return executedLinePerQualifiedName; | ||
public List<LineCoverage> getCoverageForTestClassTestMethodAndClassNames(String testClassName, String testMethodName, String className) { | ||
return this.testClassCoverage.get(testClassName).getCoverageForTestMethodAndClassNames(testMethodName, className); | ||
} | ||
|
||
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; | ||
} | ||
|
||
public Map<String, Set<Integer>> getModifiedLinePerQualifiedName() { | ||
return modifiedLinePerQualifiedName; | ||
@Override | ||
public String toString() { | ||
return "Coverage{" + | ||
"testClassCoverage=" + testClassCoverage.toString() + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.