-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileWalker.java
More file actions
88 lines (75 loc) · 3.63 KB
/
FileWalker.java
File metadata and controls
88 lines (75 loc) · 3.63 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
package edu.rit.se.testfiledetector;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class FileWalker {
private List<Path> files;
public List<Path> getJavaTestFiles(String directoryPath, boolean recursive) throws IOException {
files = new ArrayList<>();
Path startDir = Paths.get(directoryPath);
if (recursive) {
Files.walkFileTree(startDir, new FindJavaTestFilesVisitor());
} else {
Files.walk(startDir, 1)
.filter(Files::isRegularFile)
.forEach(filePath -> {
if(filePath.getFileName().toString().lastIndexOf(".")!=-1) {
String fileNameWithoutExtension = filePath.getFileName().toString().substring(0, filePath.getFileName().toString().lastIndexOf("."));
//test files should have 'test(s)' as either a prefix or suffix
if (filePath.toString().toLowerCase().endsWith(".java") &&
(fileNameWithoutExtension.toLowerCase().startsWith("test") || fileNameWithoutExtension.toLowerCase().endsWith("test") ||
fileNameWithoutExtension.toLowerCase().startsWith("tests") || fileNameWithoutExtension.toLowerCase().endsWith("tests"))) {
files.add(filePath);
}
}
});
}
return files;
}
public List<Path> getJavaFiles(String directoryPath, boolean recursive) throws IOException {
files = new ArrayList<>();
Path startDir = Paths.get(directoryPath);
if (recursive) {
Files.walkFileTree(startDir, new FindJavaFilesVisitor());
} else {
Files.walk(startDir, 1)
.filter(Files::isRegularFile)
.forEach(filePath -> {
if (filePath.toString().toLowerCase().endsWith(".java")) {
files.add(filePath);
}
});
}
return files;
}
private class FindJavaFilesVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
throws IOException {
if (file.toString().toLowerCase().endsWith(".java")) {
files.add(file);
}
return FileVisitResult.CONTINUE;
}
}
public class FindJavaTestFilesVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
throws IOException {
if (file.getFileName().toString().lastIndexOf(".") !=-1) {
String fileNameWithoutExtension = file.getFileName().toString().substring(0, file.getFileName().toString().lastIndexOf("."));
//test files should have 'test(s)' as either a prefix or suffix
if (file.toString().toLowerCase().endsWith(".java") &&
(fileNameWithoutExtension.toLowerCase().startsWith("test") || fileNameWithoutExtension.toLowerCase().endsWith("test") ||
fileNameWithoutExtension.toLowerCase().startsWith("tests") || fileNameWithoutExtension.toLowerCase().endsWith("tests"))) {
files.add(file);
}
}
return FileVisitResult.CONTINUE;
}
}
}