Skip to content

Commit

Permalink
fix: use concurrent map and synchonized methods (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb authored Jun 30, 2021
1 parent 1b072a8 commit 6978c3a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.stamp_project.diff_test_selection.coverage;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
Expand All @@ -13,10 +14,10 @@ public class Coverage {
public final Map<String, TestClassCoverage> testClassCoverage;

public Coverage() {
this.testClassCoverage = new LinkedHashMap<>();
this.testClassCoverage = new ConcurrentHashMap<>();
}

public void addCoverage(String testClassName, String testMethodName, String className, int line, int hitCounts) {
public synchronized void addCoverage(String testClassName, String testMethodName, String className, int line, int hitCounts) {
if (!this.testClassCoverage.containsKey(testClassName)) {
this.testClassCoverage.put(testClassName, new TestClassCoverage(testClassName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* created by Benjamin DANGLOT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.stamp_project.diff_test_selection.coverage;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Benjamin DANGLOT
Expand All @@ -15,10 +16,10 @@ public class TestClassCoverage {

public TestClassCoverage(String testClassName) {
this.testClassName = testClassName;
this.testMethodsCoverage = new LinkedHashMap<>();
this.testMethodsCoverage = new ConcurrentHashMap<>();
}

public void addCoverage(String testMethodName, String className, int line, int hitCounts) {
public synchronized void addCoverage(String testMethodName, String className, int line, int hitCounts) {
if (!this.testMethodsCoverage.containsKey(testMethodName)) {
this.testMethodsCoverage.put(testMethodName, new TestMethodCoverage(testMethodName));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.stamp_project.diff_test_selection.coverage;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Benjamin DANGLOT
Expand All @@ -15,13 +16,14 @@ public class TestMethodCoverage {

public TestMethodCoverage(String testMethodName) {
this.testMethodName = testMethodName;
this.classCoverageList = new LinkedHashMap<>();
this.classCoverageList = new ConcurrentHashMap<>();
}

public void addCoverage(String className, int line, int hitCounts) {
public synchronized void addCoverage(String className, int line, int hitCounts) {
if (!this.classCoverageList.containsKey(className)) {
this.classCoverageList.put(className, new ClassCoverage(className));
}
System.out.println(this.classCoverageList.get(className));
this.classCoverageList.get(className).addCoverage(line, hitCounts);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package eu.stamp_project.diff_test_selection.diff;

import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.actions.model.Delete;
import com.github.gumtreediff.actions.model.Insert;
import eu.stamp_project.diff_test_selection.selector.DiffTestSelectionImpl;
import com.github.gumtreediff.actions.model.*;

import gumtree.spoon.AstComparator;
import gumtree.spoon.diff.Diff;
Expand All @@ -17,6 +14,8 @@
import java.io.File;
import java.util.*;

import static eu.stamp_project.diff_test_selection.diff.ModifiedLinesUtils.filterOperationFromNode;

public class ModifiedLinesTool {

private static final Logger LOGGER = LoggerFactory.getLogger(ModifiedLinesTool.class);
Expand Down Expand Up @@ -105,31 +104,48 @@ private void buildMap(Diff compare) {
final List<Operation> allOperations = compare.getAllOperations();
final List<CtStatement> statements = new ArrayList<>();
for (Operation operation : allOperations) {
if (!isDeletionOrAddition(operation.getAction())) {
continue;
}
final CtElement node = ModifiedLinesUtils.filterOperation(operation);
if (node != null && !statements.contains(node.getParent(CtStatement.class))) {
final int line = node.getPosition().getLine();
final String qualifiedName = node
.getPosition()
.getCompilationUnit()
.getMainType()
.getQualifiedName();
this.addToCorrespondingMap(operation.getAction(), qualifiedName, line);
// TODO
// if (!(node.getParent(CtStatement.class) instanceof CtBlock<?>)) {
// this.coverage.addModifiedLine(qualifiedName, line);
// }
statements.add(node.getParent(CtStatement.class));
CtElement node = null;
Class<? extends Action> actionClass = null;
if (isDeletionOrAddition(operation.getAction())) {
node = ModifiedLinesUtils.filterOperation(operation);
actionClass = operation.getAction().getClass();
} else if (operation.getAction() instanceof Move || operation.getAction() instanceof Update) {
if (filterOperationFromNode(operation.getSrcNode())) {
node = operation.getSrcNode();
actionClass = Addition.class;
computeLineAndAddToMap(statements, node, actionClass, operation);
node = null;
}
if (filterOperationFromNode(operation.getDstNode())) {
node = operation.getDstNode();
actionClass = Delete.class;
}
}
computeLineAndAddToMap(statements, node, actionClass, operation);
}
}

private void computeLineAndAddToMap(List<CtStatement> statements,
CtElement node,
Class<? extends Action> actionClass,
Operation operation) {
if (node != null && !statements.contains(node.getParent(CtStatement.class))) {
final int line = node.getPosition().getLine();
final String qualifiedName = node
.getPosition()
.getCompilationUnit()
.getMainType()
.getQualifiedName();
this.addToCorrespondingMap(actionClass, qualifiedName, line +
((operation.getAction() instanceof Move || operation.getAction() instanceof Update) ? 1 : 0));
statements.add(node.getParent(CtStatement.class));
}
}

private void addToCorrespondingMap(Action action, String qualifiedName, int line) {
if (action instanceof Insert) {
private void addToCorrespondingMap(Class<? extends Action> actionClass, String qualifiedName, int line) {
if (Addition.class.isAssignableFrom(actionClass)) {
this.addToGivenMap(this.additionPerQualifiedName, qualifiedName, line);
} else {
} else if (Delete.class.isAssignableFrom(actionClass)) {
this.addToGivenMap(this.deletionPerQualifiedName, qualifiedName, line);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public class DiffTestSelectionMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (new File(this.project.getBasedir().getAbsolutePath() + "/src").exists()) {
final String module = this.project.getBasedir().getAbsolutePath().substring(this.pathDirSecondVersion.length());
//final String module = this.project.getBasedir().getAbsolutePath().substring(this.pathDirSecondVersion.length());
getLog().info("Running on:");
getLog().info(this.project.getBasedir().getAbsolutePath());
getLog().info(this.pathDirSecondVersion + "/" + module);
//getLog().info(this.pathDirSecondVersion + "/" + module);
Main.run(
new Configuration(
this.project.getBasedir().getAbsolutePath(),
this.pathDirSecondVersion + "/" + module,
this.pathDirSecondVersion,// + "/" + module,
this.outputPath,
this.outputFormat,
this.pathToDiff,
Expand Down

0 comments on commit 6978c3a

Please sign in to comment.