Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASTDiff Tests + Fix #666

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private void diff() throws RefactoringMinerTimedOutException {
projectASTDiff.setRefactorings(this.modelDiffRefactorings);
long diff_execution_started = System.currentTimeMillis();
makeASTDiff(modelDiff.getCommonClassDiffList(),false);
makeASTDiff(modelDiff.getClassRenameDiffList(),false);
makeASTDiff(modelDiff.getClassMoveDiffList(),false);
makeASTDiff(withCorrectOrder(modelDiff.getClassRenameDiffList()),false);
makeASTDiff(withCorrectOrder(modelDiff.getClassMoveDiffList()),false);
makeASTDiff(modelDiff.getInnerClassMoveDiffList(),true);
makeASTDiff(getExtraDiffs(),true);
long diff_execution_finished = System.currentTimeMillis();
Expand All @@ -71,6 +71,32 @@ private void diff() throws RefactoringMinerTimedOutException {
}
computeAllEditScripts();
}

private List<? extends UMLAbstractClassDiff> withCorrectOrder(List<? extends UMLAbstractClassDiff> umlDiffs) {
ArrayList<UMLAbstractClassDiff> result = new ArrayList<>(umlDiffs);
Set<UMLAbstractClassDiff> seen = new HashSet<>();
for (UMLAbstractClassDiff umlDiff : umlDiffs) {
UMLAbstractClassDiff found = findDiffWith(result, umlDiff.getOriginalClassName(), umlDiff.getNextClassName());
if (found != null && !seen.contains(found))
{
seen.add(found);
result.remove(found);
result.add(0, found);
}
}
return result;
}

private UMLAbstractClassDiff findDiffWith(ArrayList<? extends UMLAbstractClassDiff> result, String originalClassName, String nextClassName) {
for (UMLAbstractClassDiff umlAbstractClassDiff : result) {
if (umlAbstractClassDiff.getOriginalClassName().equals(originalClassName)
&&
umlAbstractClassDiff.getNextClassName().equals(nextClassName))
return umlAbstractClassDiff;
}
return null;
}

private List<? extends UMLAbstractClassDiff> getExtraDiffs() {
List<UMLAbstractClassDiff> extraDiffs = new ArrayList<>();
for (Refactoring modelDiffRefactoring : modelDiffRefactorings) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.refactoringminer.astDiff.tests;

import com.fasterxml.jackson.core.JsonProcessingException;
import net.joshka.junit.json.params.JsonFileSource;
import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.refactoringminer.astDiff.actions.ASTDiff;
import org.refactoringminer.astDiff.utils.CaseInfo;
import org.refactoringminer.astDiff.utils.MappingExportModel;
import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl;
import org.skyscreamer.jsonassert.JSONAssert;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.fail;
import static org.refactoringminer.astDiff.utils.UtilMethods.*;

/**
* @author Pourya Alikhani Fard [email protected]
*/
public class Defects4JProblematicCasesTest {
private static final String dir = getDefects4jMappingPath();
@ParameterizedTest(name= "{index}: {0}")
@JsonFileSource(resources = "/astDiff/defects4j/cases-problematic.json")
public void testSubTreeMappings(@ConvertWith(CaseInfo.CaseInfoConverter.class) CaseInfo info) throws JsonProcessingException, JSONException {
File mappingsDirFile = new File(getFinalFolderPath(dir, info.getRepo(), info.getCommit()));
String[] files = mappingsDirFile.list();
List<String> expectedFilesList = new ArrayList<>(List.of(Objects.requireNonNull(files)));
String projectDir = info.getRepo();
String bugID = info.getCommit();
Set<ASTDiff> astDiffs = new GitHistoryRefactoringMinerImpl().diffAtDirectories(
Path.of(getDefect4jBeforeDir(projectDir, bugID)),
Path.of(getDefect4jAfterDir(projectDir, bugID))).getDiffSet();

for (ASTDiff astDiff : astDiffs) {
String finalFilePath = getFinalFilePath(astDiff, dir, info.getRepo(), info.getCommit());
finalFilePath = getSnapShotPath(finalFilePath);
//Because we store the previous snapshot of problematic cases in /astDiff/SNAPSHOT/
String calculated = MappingExportModel.exportString(astDiff.getAllMappings());
String msg = String.format("Failed for %s/commit/%s , srcFileName: %s",info.getRepo().replace(".git",""),info.getCommit(),astDiff.getSrcPath());
String expected = null;
try {
expected = FileUtils.readFileToString(new File(finalFilePath), "utf-8");
} catch (IOException e) {
fail("File not found: " + finalFilePath + " for " + info.getRepo() + "/commit/" + info.getCommit() + " , srcFileName: " + astDiff.getSrcPath());
}
JSONAssert.assertEquals(msg,expected, calculated, false);
expectedFilesList.remove(getFileNameFromSrcDiff(astDiff.getSrcPath()));
}
for (String expectedButNotGeneratedFile : expectedFilesList) {
String expectedDiffName = getSrcASTDiffFromFile(expectedButNotGeneratedFile);
fail(expectedDiffName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.refactoringminer.astDiff.tests;

import net.joshka.junit.json.params.JsonFileSource;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.refactoringminer.astDiff.actions.ASTDiff;
import org.refactoringminer.astDiff.utils.CaseInfo;
import org.refactoringminer.astDiff.utils.MappingExportModel;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.refactoringminer.astDiff.utils.UtilMethods.*;
import static org.refactoringminer.astDiff.utils.UtilMethods.getSrcASTDiffFromFile;

/* Created by pourya on 2024-02-17*/
public class RefactoringOracleProblematicCasesTest {
private static final String dir = getCommitsMappingsPath();
@ParameterizedTest(name= "{index}: {0}")
@JsonFileSource(resources = {"/astDiff/commits/cases-problematic.json"})
public void testSubTreeMappings(@ConvertWith(CaseInfo.CaseInfoConverter.class) CaseInfo info) throws Exception {
//TODO : Fix the flakiness for this case
if (info.makeURL().equals("https://github.com/infinispan/infinispan/commit/03573a655bcbb77f7a76d8e22d851cc22796b4f8")) return;

File mappingsDirFile = new File(getFinalFolderPath(dir, info.getRepo(), info.getCommit()));
String[] files = mappingsDirFile.list();
List<String> expectedFilesList = new ArrayList<>(List.of(Objects.requireNonNull(files)));
boolean partial = info.getSrc_files() != null && !info.getSrc_files().isEmpty();
Set<ASTDiff> astDiffs = getProjectDiffLocally(info.makeURL());
boolean hit = false;
for (ASTDiff astDiff : astDiffs) {
String finalFilePath = getFinalFilePath(astDiff, dir, info.getRepo(), info.getCommit());
finalFilePath = getSnapShotPath(finalFilePath);
//Because we store the previous snapshot of problematic cases in /astDiff/SNAPSHOT/
if (partial)
if (!info.getSrc_files().contains(astDiff.getSrcPath()))
continue;
hit = true;
String calculated = MappingExportModel.exportString(astDiff.getAllMappings()).replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
String msg = String.format("Failed for %s/commit/%s , srcFileName: %s",info.getRepo().replace(".git",""),info.getCommit(),astDiff.getSrcPath());
String expected = null;
try {
expected = FileUtils.readFileToString(new File(finalFilePath), "utf-8");
assertEquals(expected.length(), calculated.length(), msg);
} catch (IOException e) {
fail("File not found: " + finalFilePath + " for " + info.getRepo() + "/commit/" + info.getCommit() + " , srcFileName: " + astDiff.getSrcPath());
}
assertEquals(calculated, expected, msg);
expectedFilesList.remove(getFileNameFromSrcDiff(astDiff.getSrcPath()));
}
for (String expectedButNotGeneratedFile : expectedFilesList) {
String expectedDiffName = getSrcASTDiffFromFile(expectedButNotGeneratedFile);
fail(expectedDiffName + " not generated for " + expectedDiffName);
}
if (!hit)
fail("No diff checked for " + info.getRepo() + "/commit/" + info.getCommit());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public static String getFinalFilePath(ASTDiff astDiff, String dir, String repo,
String exportName = getFileNameFromSrcDiff(astDiff.getSrcPath());
return getFinalFolderPath(dir,repo,commit)+ exportName;
}
public static String getSnapShotPath(String path){
return path.replace("/resources/astDiff/", "/resources/astDiff/PREV-SNAPSHOT/");
}

public static String getFileNameFromSrcDiff(String astSrcName)
{
String exportName1 = astSrcName.replace("/",".").replace(".java","");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import java.util.Scanner;
import java.util.Set;

import static org.refactoringminer.astDiff.utils.UtilMethods.getFinalFilePath;
import static org.refactoringminer.astDiff.utils.UtilMethods.getFinalFolderPath;
import static org.refactoringminer.astDiff.utils.UtilMethods.*;

// Command class for the "add" command
@Parameters(commandDescription = "Add files to the index")
Expand All @@ -29,6 +28,13 @@ public class AddCommand extends BaseCommand {
required = false)
private Set<String> files;

@Parameter(names = "-snapshot", description = "Snapshot option", required = false)
private boolean isSnapshot;

public boolean isSnapshot() {
return isSnapshot;
}

@Override
void postValidationExecution() {
String infoFile = diffDataSet.resolve(problematic);
Expand All @@ -41,20 +47,23 @@ void postValidationExecution() {
commit,
projectASTDiff,
dir,
infoFile, files);
infoFile, files, isSnapshot());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void addTestCase(String repo, String commit, ProjectASTDiff projectASTDiff, String mappingsPath, String testInfoFile, Set<String> selected_files) throws IOException {
private static void addTestCase(String repo, String commit, ProjectASTDiff projectASTDiff, String mappingsPath, String testInfoFile, Set<String> selected_files, boolean snapshot) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String jsonFile = mappingsPath + testInfoFile;

for (ASTDiff astDiff : projectASTDiff.getDiffSet()) {
String finalPath = getFinalFilePath(astDiff, mappingsPath, repo, commit);
if (snapshot)
finalPath = getSnapShotPath(finalPath);
Files.createDirectories(Paths.get(getFinalFolderPath(mappingsPath,repo,commit)));
MappingExportModel.exportToFile(new File(finalPath), astDiff.getAllMappings());
}
if (snapshot) return;
List<CaseInfo> infos = mapper.readValue(new File(jsonFile), new TypeReference<List<CaseInfo>>(){});
CaseInfo caseInfo = new CaseInfo(repo,commit, selected_files);
boolean goingToAdd = true;
Expand Down
Loading