-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceJunitAnnotations.java
More file actions
105 lines (94 loc) · 4.81 KB
/
Copy pathReplaceJunitAnnotations.java
File metadata and controls
105 lines (94 loc) · 4.81 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
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.regex.Pattern;
public class ReplaceJunitAnnotations {
private List<String> ignoreFiles;
private Map<String, String> replacements;
public ReplaceJunitAnnotations() {
ignoreFiles = Arrays.asList(
"/Users/kumara8/Workspace/shri_ftbe/a360team/e2o/widget-apps/data/api/test/com/qontext/e2o/widgets/apps/data/api/imports/h.java");
replacements = new HashMap<>();
replacements.put("import org.junit.Rule;\n", "import org.junit.jupiter.api.extension.RegisterExtension;\n");
replacements.put("import com.qontext.sas.config.ConfigRegistryRule;\n", "import com.qontext.sas.config.ConfigRegistryRuleJunit5;\n");
replacements.put(Pattern.quote("@Rule\n"), "@RegisterExtension\n");
replacements.put(Pattern.quote("public ConfigRegistryRule configRegistryRule = new ConfigRegistryRule();\n"),
"public ConfigRegistryRuleJunit5 configRegistryRule = new ConfigRegistryRuleJunit5();\n");
replacements.put(Pattern.quote("public ConfigRegistryRule chain = new ConfigRegistryRule();\n"),
"public ConfigRegistryRuleJunit5 chain = new ConfigRegistryRuleJunit5();\n");
replacements.put(Pattern.quote("@RunWith(MockitoJUnitRunner.class)\n"),
"@ExtendWith(MockitoExtension.class)\n@MockitoSettings(strictness = Strictness.LENIENT)\n");
replacements.put("@Before\n", "@BeforeEach\n");
replacements.put("@After\n", "@AfterEach\n");
replacements.put("@BeforeClass\n", "@BeforeAll\n");
replacements.put("import org.junit.Before;\n", "import org.junit.jupiter.api.BeforeEach;\n");
replacements.put("import org.junit.After;\n", "import org.junit.jupiter.api.AfterEach;\n");
replacements.put("import org.junit.Test;\n", "import org.junit.jupiter.api.Test;\n");
replacements.put("import org.junit.runner.RunWith;\n", "import org.junit.jupiter.api.extension.ExtendWith;\n");
replacements.put("import org.mockito.junit.MockitoJUnitRunner;\n", "import org.mockito.junit.jupiter.MockitoExtension;\n" +
"import org.mockito.junit.jupiter.MockitoSettings;\n" +
"import org.mockito.quality.Strictness;\n");
replacements.put("import static org.junit.Assert.assertTrue;\n", "import static org.junit.jupiter.api.Assertions.assertTrue;\n");
replacements.put("import static org.junit.Assert.assertEquals;\n", "import static org.junit.jupiter.api.Assertions.assertEquals;\n");
replacements.put("import static org.junit.Assert.assertNull;\n", "import static org.junit.jupiter.api.Assertions.assertNull;\n");
replacements.put("import static org.junit.Assert.assertNotNull;\n", "import static org.junit.jupiter.api.Assertions.assertNotNull;\n");
replacements.put("import org.junit.BeforeClass;\n", "import org.junit.jupiter.api.BeforeAll;\n");
// replacements.put("mport static org.junit.Assert.assertEquals;\n", "")
// import static org.junit.jupiter.api.Assertions.assertEquals;
//import static org.junit.jupiter.api.Assertions.assertFalse;
//import static org.junit.jupiter.api.Assertions.assertTrue;
}
public static void main(String[] args) throws IOException {
new ReplaceJunitAnnotations().execute(
"/Users/kumara8/Workspace/shri_ftbe/a360team/sas/groups/impl/test/com/qontext/sas/groups/impl");
}
public void execute(String rootPath) {
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new DirectoryProcessor(new File(rootPath)));
pool.shutdown();
}
class DirectoryProcessor extends RecursiveAction {
private final File file;
public DirectoryProcessor(File file) {
this.file = file;
}
@Override
protected void compute() {
if (file.isDirectory()) {
File[] files = file.listFiles((dir, name) -> !name.startsWith("."));
if (files != null) {
for (File f : files) {
DirectoryProcessor processor = new DirectoryProcessor(f);
processor.fork();
}
for (File f : files) {
DirectoryProcessor processor = new DirectoryProcessor(f);
processor.join();
}
}
} else if (file.isFile() && !ignoreFiles.contains(file.getPath())) {
processFile(file);
}
}
private void processFile(File file) {
try {
Path path = Paths.get(file.getAbsolutePath());
String content = new String(Files.readAllBytes(path));
for (Map.Entry<String, String> entry : replacements.entrySet()) {
content = content.replaceAll(entry.getKey(), entry.getValue());
}
Files.write(path, content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}