-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMappingDetector.java
More file actions
107 lines (85 loc) · 3.36 KB
/
MappingDetector.java
File metadata and controls
107 lines (85 loc) · 3.36 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
package edu.rit.se.testsmells;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.AnnotationDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
public class MappingDetector {
TestFile testFile;
String productionFileName, productionFilePath;
boolean ignoreFile;
public MappingDetector() {
productionFileName = "";
productionFilePath = "";
ignoreFile = false;
}
public TestFile detectMapping(String testFilePath) throws IOException {
testFile = new TestFile(testFilePath);
int index = testFile.getFileName().toLowerCase().lastIndexOf("test");
if (index == 0) {
//the name of the test file starts with the name 'test'
productionFileName = testFile.getFileName().substring(4);
} else {
//the name of the test file ends with the name 'test'
productionFileName = testFile.getFileName().substring(0, testFile.getFileName().toLowerCase().lastIndexOf("test")) + ".java";
}
Path startDir = Paths.get(testFile.getProjectRootFolder());
Files.walkFileTree(startDir, new FindJavaTestFilesVisitor());
if (isFileSyntacticallyValid(productionFilePath))
testFile.setProductionFilePath(productionFilePath);
else
testFile.setProductionFilePath("");
return testFile;
}
/**
* Determines if the identified production file is syntactically correct by parsing it and generating its AST
*
* @param filePath of the production file
*/
private boolean isFileSyntacticallyValid(String filePath) {
boolean valid = false;
ignoreFile = false;
if (filePath.length() != 0) {
try {
FileInputStream fTemp = new FileInputStream(filePath);
CompilationUnit compilationUnit = JavaParser.parse(fTemp);
MappingDetector.ClassVisitor classVisitor;
classVisitor = new MappingDetector.ClassVisitor();
classVisitor.visit(compilationUnit, null);
valid = !ignoreFile;
} catch (Exception error) {
valid = false;
}
}
return valid;
}
public class FindJavaTestFilesVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().toLowerCase().equals(productionFileName.toLowerCase())) {
productionFilePath = file.toAbsolutePath().toString();
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
}
/**
* Visitor class
*/
private class ClassVisitor extends VoidVisitorAdapter<Void> {
@Override
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
ignoreFile = n.isInterface();
super.visit(n, arg);
}
@Override
public void visit(AnnotationDeclaration n, Void arg) {
ignoreFile = true;
super.visit(n, arg);
}
}
}