-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDataPreparer.java
More file actions
140 lines (121 loc) · 4.69 KB
/
Copy pathDataPreparer.java
File metadata and controls
140 lines (121 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package edu.lu.uni.serval.dataprepare;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.JavaLibrary;
import edu.lu.uni.serval.utils.PathUtils;
/**
* Prepare data for fault localization.
*
* @author kui.liu
*
*/
public class DataPreparer {
private String buggyProjectParentPath;
public String bugDir; // the working directory for GZoltar.
public String classPath;
public String srcPath;
public String testClassPath;
public String testSrcPath;
public List<String> libPaths = new ArrayList<>();
public boolean validPaths = true;
public String[] testCases;
public URL[] classPaths;
public DataPreparer(String path){
if (!path.endsWith("/")){
path += "/";
}
buggyProjectParentPath = path;
}
public void prepareData(String buggyProject){
// TODO
// libPaths.add(FromString.class.getProtectionDomain().getCodeSource().getLocation().getFile());
// libPaths.add(EasyMock.class.getProtectionDomain().getCodeSource().getLocation().getFile());
// libPaths.add(IOUtils.class.getProtectionDomain().getCodeSource().getLocation().getFile());
loadPaths(buggyProject);
if (!checkProjectDirectories()){
validPaths = false;
return;
}
loadTestCases();
loadClassPaths();
}
private void loadPaths(String buggyProject) {
String projectDir = buggyProjectParentPath;
List<String> paths = PathUtils.getSrcPath(buggyProject);
classPath = projectDir + buggyProject + paths.get(0);
testClassPath = projectDir + buggyProject + paths.get(1);
srcPath = projectDir + buggyProject + paths.get(2);
testSrcPath = projectDir + buggyProject + paths.get(3);
bugDir = projectDir + buggyProject; // set bugDir.
List<File> libPackages = new ArrayList<>();// dependencies.
if (new File(projectDir + buggyProject + "/lib/").exists()) {
libPaths.add(projectDir + buggyProject + "/lib/");
libPackages.addAll(FileHelper.getAllFiles(projectDir + buggyProject + "/lib/", ".jar"));
}
if (new File(projectDir + buggyProject + "/build/lib/").exists()) {
libPaths.add(projectDir + buggyProject + "/build/lib/");
libPackages.addAll(FileHelper.getAllFiles(projectDir + buggyProject + "/build/lib/", ".jar"));
}
// if (new File(projectDir + buggyProject + "/build/libs/").exists()) {
// libPaths.add(projectDir + buggyProject + "/build/libs/");
// libPackages.addAll(FileHelper.getAllFiles(projectDir + buggyProject + "/build/lib/", ".jar"));
// }
// Class files in lib directory. TODO
for (File libPackage : libPackages) {
libPaths.add(libPackage.getAbsolutePath());
}
}
private boolean checkProjectDirectories() {
if (!new File(classPath).exists()) {
System.err.println("Class path: " + classPath + " does not exist!");
return false;
}
if (!new File(srcPath).exists()) {
System.err.println("Source code path: " + srcPath + " does not exist!");
return false;
}
if (!new File(testClassPath).exists()) {
System.err.println("Test class path: " + testClassPath + " does not exist!");
return false;
}
if (!new File(testSrcPath).exists()) {
System.err.println("Test source path: " + testSrcPath + " does not exist!");
return false;
}
return true;
}
private void loadTestCases() {
testCases = new TestClassesFinder().findIn(JavaLibrary.classPathFrom(testClassPath + ":" + classPath), false);
// List<File> testCasesFiles = FileHelper.getAllFiles(testClassPath, ".class");
//// testCasesFiles.addAll(FileHelper.getAllFiles(testClassPath, "Tests.class"));
// StringBuilder b = new StringBuilder();
// List<String> testCaseNames = new ArrayList<>();
// int i = testCasesFiles.get(0).getPath().indexOf(testClassPath) + testClassPath.length();
// for (File file : testCasesFiles) {
// String fileName = file.getName();
// if (fileName.contains("Test") && !fileName.contains("$")) {
//// if (fileName.startsWith("Test") || fileName.endsWith("Test.class")) {//Time
// String filePath = file.getPath();
// filePath = filePath.substring(i, filePath.lastIndexOf(".")).replace("/", ".");
// testCaseNames.add(filePath);
// b.append(filePath).append("\n");
// }
// }
// FileHelper.outputToFile("log/testcases_2.txt", b, false);
// testCases = testCaseNames.toArray(new String[testCaseNames.size()]);
Arrays.sort(testCases);
}
private void loadClassPaths() {
classPaths = JavaLibrary.classPathFrom(testClassPath);
classPaths = JavaLibrary.extendClassPathWith(classPath, classPaths);
if (libPaths != null) {
for (String lpath : libPaths) {
classPaths = JavaLibrary.extendClassPathWith(lpath, classPaths);
}
}
}
}