|
6 | 6 | import java.nio.file.Files; |
7 | 7 | import java.nio.file.Path; |
8 | 8 | import java.nio.file.PathMatcher; |
| 9 | +import java.nio.file.Paths; |
9 | 10 | import java.util.Comparator; |
10 | 11 | import java.util.Objects; |
11 | 12 | import java.util.Optional; |
@@ -53,6 +54,64 @@ private PathUtils() { |
53 | 54 | throw new AssertionError("PathUtils is a static utility class that cannot be instantiated"); |
54 | 55 | } |
55 | 56 |
|
| 57 | + private static final String SUREFIRE_PATH = "surefire-reports"; |
| 58 | + private static final String FAILSAFE_PATH = "failsafe-reports"; |
| 59 | + |
| 60 | + public enum ReportsDirectory { |
| 61 | + |
| 62 | + SUREFIRE_1("(Test)(.*)", SUREFIRE_PATH), |
| 63 | + SUREFIRE_2("(.*)(Test)", SUREFIRE_PATH), |
| 64 | + SUREFIRE_3("(.*)(Tests)", SUREFIRE_PATH), |
| 65 | + SUREFIRE_4("(.*)(TestCase)", SUREFIRE_PATH), |
| 66 | + FAILSAFE_1("(IT)(.*)", FAILSAFE_PATH), |
| 67 | + FAILSAFE_2("(.*)(IT)", FAILSAFE_PATH), |
| 68 | + FAILSAFE_3("(.*)(ITCase)", FAILSAFE_PATH), |
| 69 | + ARTIFACT(".*", "artifact-capture"); |
| 70 | + |
| 71 | + private String regex; |
| 72 | + private String folder; |
| 73 | + |
| 74 | + ReportsDirectory(String regex, String folder) { |
| 75 | + this.regex = regex; |
| 76 | + this.folder = folder; |
| 77 | + } |
| 78 | + |
| 79 | + public String getRegEx() { |
| 80 | + return regex; |
| 81 | + } |
| 82 | + |
| 83 | + public String getFolder() { |
| 84 | + return folder; |
| 85 | + } |
| 86 | + |
| 87 | + public Path getPath() { |
| 88 | + return getTargetPath().resolve(folder); |
| 89 | + } |
| 90 | + |
| 91 | + public static ReportsDirectory fromObject(Object obj) { |
| 92 | + String name = obj.getClass().getSimpleName(); |
| 93 | + for (ReportsDirectory constant : values()) { |
| 94 | + if (name.matches(constant.regex)) { |
| 95 | + return constant; |
| 96 | + } |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + public static Path getPathForObject(Object obj) { |
| 102 | + ReportsDirectory constant = fromObject(obj); |
| 103 | + Path path = getTargetPath(); |
| 104 | + if (constant != null) { |
| 105 | + path = path.resolve(constant.folder); |
| 106 | + } |
| 107 | + return path; |
| 108 | + } |
| 109 | + |
| 110 | + private static Path getTargetPath() { |
| 111 | + return Paths.get(getBaseDir(), "target"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
56 | 115 | /** |
57 | 116 | * Get the next available path in sequence for the specified base name and extension in the specified folder. |
58 | 117 | * |
@@ -110,4 +169,14 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio |
110 | 169 | return targetPath.resolve(newName); |
111 | 170 | } |
112 | 171 |
|
| 172 | + |
| 173 | + /** |
| 174 | + * Get project base directory. |
| 175 | + * |
| 176 | + * @return project base directory |
| 177 | + */ |
| 178 | + public static String getBaseDir() { |
| 179 | + Path currentRelativePath = Paths.get(System.getProperty("user.dir")); |
| 180 | + return currentRelativePath.toAbsolutePath().toString(); |
| 181 | + } |
113 | 182 | } |
0 commit comments