Skip to content

Commit

Permalink
Method Oracle test with local files (checkstyle project only)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Apr 3, 2024
1 parent 89d2eae commit 2f226ff
Show file tree
Hide file tree
Showing 9 changed files with 1,376 additions and 31 deletions.
587 changes: 587 additions & 0 deletions src/main/java/org/codetracker/BaseTrackerWithLocalFiles.java

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/main/java/org/codetracker/GitLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.codetracker;

import java.util.List;
import java.util.Map;

public class GitLog {
private Map<String, List<String>> commitLogMap;

public GitLog() {

}

public GitLog(Map<String, List<String>> commitLogMap) {
this.commitLogMap = commitLogMap;
}

public Map<String, List<String>> getCommitLogMap() {
return commitLogMap;
}
}
736 changes: 736 additions & 0 deletions src/main/java/org/codetracker/MethodTrackerWithLocalFilesImpl.java

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/main/java/org/codetracker/api/MethodTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.eclipse.jgit.lib.Repository;
import org.codetracker.MethodTrackerImpl;
import org.codetracker.MethodTrackerWithLocalFilesImpl;
import org.codetracker.element.Method;

public interface MethodTracker extends CodeTracker {
Expand All @@ -10,6 +11,7 @@ public interface MethodTracker extends CodeTracker {

class Builder {
private Repository repository;
private String gitURL;
private String startCommitId;
private String filePath;
private String methodName;
Expand All @@ -20,6 +22,11 @@ public Builder repository(Repository repository) {
return this;
}

public Builder gitURL(String gitURL) {
this.gitURL = gitURL;
return this;
}

public Builder startCommitId(String startCommitId) {
this.startCommitId = startCommitId;
return this;
Expand Down Expand Up @@ -49,5 +56,9 @@ public MethodTracker build() {
return new MethodTrackerImpl(repository, startCommitId, filePath, methodName, methodDeclarationLineNumber);
}

public MethodTracker buildWithLocalFiles() {
checkInput();
return new MethodTrackerWithLocalFilesImpl(gitURL, startCommitId, filePath, methodName, methodDeclarationLineNumber);
}
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/codetracker/util/AttributeOracleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ private static History<Attribute> attributeTracker(AttributeHistoryInfo attribut
.build();
return attributeTracker.track();
}

/*
public static Stream<Arguments> testProvider() throws IOException {
return getArgumentsStream(AttributeOracle.all(), EXPECTED, AttributeOracleTest::attributeTracker);
}
*/
}
3 changes: 2 additions & 1 deletion src/test/java/org/codetracker/util/BlockOracleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ private static History<Block> blockTracker(BlockHistoryInfo blockHistoryInfo, Re
.build();
return blockTracker.track();
}

/*
public static Stream<Arguments> testProvider() throws IOException {
return getArgumentsStream(BlockOracle.all(), EXPECTED, BlockOracleTest::blockTracker);
}
*/
}
9 changes: 3 additions & 6 deletions src/test/java/org/codetracker/util/MethodOracleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@
import org.codetracker.element.Method;
import org.codetracker.experiment.oracle.MethodOracle;
import org.codetracker.experiment.oracle.history.MethodHistoryInfo;
import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.provider.Arguments;

import java.io.IOException;
import java.util.stream.Stream;

@Disabled
public class MethodOracleTest extends OracleTest {
private static final String EXPECTED = System.getProperty("user.dir") + "/src/test/resources/method/";

private static History<Method> methodTracker(MethodHistoryInfo methodHistoryInfo, Repository repository) throws Exception {
private static History<Method> methodTracker(MethodHistoryInfo methodHistoryInfo, String gitURL) throws Exception {
MethodTracker methodTracker = CodeTracker.methodTracker()
.repository(repository)
.gitURL(gitURL)
.filePath(methodHistoryInfo.getFilePath())
.startCommitId(methodHistoryInfo.getStartCommitId())
.methodName(methodHistoryInfo.getFunctionName())
.methodDeclarationLineNumber(methodHistoryInfo.getFunctionStartLine())
.build();
.buildWithLocalFiles();
return methodTracker.track();
}
public static Stream<Arguments> testProvider() throws IOException {
Expand Down
35 changes: 13 additions & 22 deletions src/test/java/org/codetracker/util/OracleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
import org.codetracker.api.Edge;
import org.codetracker.api.History;
import org.codetracker.change.Change;
import org.codetracker.experiment.AbstractExperimentStarter;
import org.codetracker.experiment.AbstractExperimentStarter.CheckedBiFunction;
import org.codetracker.experiment.oracle.AbstractOracle;
import org.codetracker.experiment.oracle.history.AbstractHistoryInfo;
import org.codetracker.experiment.oracle.history.ChangeHistory;
import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.refactoringminer.api.GitService;
import org.refactoringminer.util.GitServiceImpl;

import java.io.BufferedReader;
import java.io.FileReader;
Expand All @@ -29,13 +24,9 @@
import java.util.stream.Stream;

public abstract class OracleTest {
private static final String FOLDER_TO_CLONE = "tmp/";
private static final Map<String, Integer> expectedTP = new HashMap<>();
private static final Map<String, Integer> expectedFP = new HashMap<>();
private static final Map<String, Integer> expectedFN = new HashMap<>();
protected static final int ALL_CORES = Runtime.getRuntime().availableProcessors();
protected static final int HALF_CORES = Runtime.getRuntime().availableProcessors()/2;
protected static final int QUARTER_CORES = Runtime.getRuntime().availableProcessors()/4;

protected static void loadExpected(String filePath) {
try {
Expand All @@ -58,27 +49,24 @@ protected static void loadExpected(String filePath) {
}

protected static <H extends AbstractHistoryInfo, E extends CodeElement> Stream<Arguments> codeTrackerTestProvider
(AbstractOracle<H> oracle, CheckedBiFunction<H, Repository, History<E>> tracker) {
GitService gitService = new GitServiceImpl();
(AbstractOracle<H> oracle, CheckedBiFunction<H, String, History<E>> tracker) {
Stream.Builder<Arguments> builder = Stream.builder();
for (Map.Entry<String, H> oracleInstance : oracle.getOracle().entrySet()) {
String fileName = oracleInstance.getKey();
H historyInfo = oracleInstance.getValue();
builder.add(Arguments.of(tracker,historyInfo, gitService, fileName));
builder.add(Arguments.of(tracker,historyInfo, fileName));
}
return builder.build();
}

@ParameterizedTest(name = "{index}: {3}")
@ParameterizedTest(name = "{index}: {2}")
@MethodSource(value = "testProvider")
public <H extends AbstractHistoryInfo, E extends CodeElement> void testCodeTracker(CheckedBiFunction<H, Repository, History<E>> tracker, H historyInfo, GitService gitService, String fileName) {
public <H extends AbstractHistoryInfo, E extends CodeElement> void testCodeTracker(CheckedBiFunction<H, String, History<E>> tracker, H historyInfo, String fileName) throws Exception {
String repositoryWebURL = historyInfo.getRepositoryWebURL();
String repositoryName = repositoryWebURL.replace("https://github.com/", "").replace(".git", "").replace("/", "\\");
String projectDirectory = FOLDER_TO_CLONE + repositoryName;

try (Repository repository = gitService.cloneIfNotExists(projectDirectory, repositoryWebURL)) {
//TODO temporary if check, remove when all local files are created
if(fileName.startsWith("checkstyle")) {
HashMap<String, ChangeHistory> oracleChanges = oracle(historyInfo.getExpectedChanges());
History<E> history = tracker.apply(historyInfo, repository);
History<E> history = tracker.apply(historyInfo, repositoryWebURL);
HashMap<String, ChangeHistory> detectedChanges = new HashMap<>();
HashMap<String, ChangeHistory> notDetectedChanges = new HashMap<>(oracleChanges);
HashMap<String, ChangeHistory> falseDetectedChanges = processHistory((HistoryImpl<E>) history);
Expand All @@ -99,11 +87,9 @@ public <H extends AbstractHistoryInfo, E extends CodeElement> void testCodeTrack
() -> Assertions.assertEquals(expectedFP.get(fileName), actualFP, String.format("Should have %s False Positives, but has %s", expectedFP.get(fileName), actualFP)),
() -> Assertions.assertEquals(expectedFN.get(fileName), actualFN, String.format("Should have %s False Negatives, but has %s", expectedFN.get(fileName), actualFN))
);
} catch (Exception e) {
e.printStackTrace();
}
}
protected static <H extends AbstractHistoryInfo, E extends CodeElement> Stream<Arguments> getArgumentsStream(List<? extends AbstractOracle<H>> all, String expected, AbstractExperimentStarter.CheckedBiFunction<H, Repository, History<E>> tracker) {
protected static <H extends AbstractHistoryInfo, E extends CodeElement> Stream<Arguments> getArgumentsStream(List<? extends AbstractOracle<H>> all, String expected, CheckedBiFunction<H, String, History<E>> tracker) {
return all.stream().flatMap(oracle -> {
loadExpected(expected + oracle.getName() + "-expected.txt");
return codeTrackerTestProvider(oracle, tracker);
Expand Down Expand Up @@ -168,4 +154,9 @@ protected static String getChangeKey(Change.Type changeType, String commitId) {
protected static String getChangeKey(String changeType, String commitId) {
return String.format("%s-%s", commitId, changeType);
}

@FunctionalInterface
public interface CheckedBiFunction<T, U, R> {
R apply(T t, U u) throws Exception;
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/codetracker/util/VariableOracleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ private static History<Variable> variableTracker(VariableHistoryInfo variableHis
.build();
return variableTracker.track();
}

/*
public static Stream<Arguments> testProvider() throws IOException {
return getArgumentsStream(VariableOracle.all(), EXPECTED, VariableOracleTest::variableTracker);
}
*/
}

0 comments on commit 2f226ff

Please sign in to comment.