Skip to content

Commit

Permalink
ASTDiff: Test for Problematic cases based on the previous snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
pouryafard75 authored and tsantalis committed Feb 18, 2024
1 parent 28aeb68 commit fac0757
Show file tree
Hide file tree
Showing 256 changed files with 7,175,144 additions and 4 deletions.
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,62 @@
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 {
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

0 comments on commit fac0757

Please sign in to comment.