|
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; |
|
47 | 48 | * ... |
48 | 49 | * </pre> |
49 | 50 | */ |
50 | | -public class PathUtils { |
| 51 | +public final class PathUtils { |
51 | 52 |
|
52 | 53 | 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 | + /** |
| 61 | + * This enumeration contains methods to help build proxy subclass names and select reports directories. |
| 62 | + */ |
| 63 | + public enum ReportsDirectory { |
| 64 | + |
| 65 | + SUREFIRE_1("(Test)(.*)", SUREFIRE_PATH), |
| 66 | + SUREFIRE_2("(.*)(Test)", SUREFIRE_PATH), |
| 67 | + SUREFIRE_3("(.*)(Tests)", SUREFIRE_PATH), |
| 68 | + SUREFIRE_4("(.*)(TestCase)", SUREFIRE_PATH), |
| 69 | + FAILSAFE_1("(IT)(.*)", FAILSAFE_PATH), |
| 70 | + FAILSAFE_2("(.*)(IT)", FAILSAFE_PATH), |
| 71 | + FAILSAFE_3("(.*)(ITCase)", FAILSAFE_PATH), |
| 72 | + ARTIFACT(".*", "artifact-capture"); |
| 73 | + |
| 74 | + private String regex; |
| 75 | + private String folder; |
| 76 | + |
| 77 | + ReportsDirectory(String regex, String folder) { |
| 78 | + this.regex = regex; |
| 79 | + this.folder = folder; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the regular expression that matches class names for this constant. |
| 84 | + * |
| 85 | + * @return class-matching regular expression string |
| 86 | + */ |
| 87 | + public String getRegEx() { |
| 88 | + return regex; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the name of the folder associated with this constant. |
| 93 | + * |
| 94 | + * @return class-related folder name |
| 95 | + */ |
| 96 | + public String getFolder() { |
| 97 | + return folder; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the resolved Maven-derived path associated with this constant. |
| 102 | + * |
| 103 | + * @return Maven folder path |
| 104 | + */ |
| 105 | + public Path getPath() { |
| 106 | + return getTargetPath().resolve(folder); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get the reports directory constant for the specified test class object. |
| 111 | + * |
| 112 | + * @param obj test class object |
| 113 | + * @return reports directory constant |
| 114 | + */ |
| 115 | + public static ReportsDirectory fromObject(Object obj) { |
| 116 | + String name = obj.getClass().getSimpleName(); |
| 117 | + for (ReportsDirectory constant : values()) { |
| 118 | + if (name.matches(constant.regex)) { |
| 119 | + return constant; |
| 120 | + } |
| 121 | + } |
| 122 | + throw new IllegalStateException("Someone removed the 'default' pattern from this enumeration"); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get reports directory path for the specified test class object. |
| 127 | + * |
| 128 | + * @param obj test class object |
| 129 | + * @return reports directory path |
| 130 | + */ |
| 131 | + public static Path getPathForObject(Object obj) { |
| 132 | + ReportsDirectory constant = fromObject(obj); |
| 133 | + return getTargetPath().resolve(constant.folder); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get the path for the 'target' folder of the current project. |
| 138 | + * |
| 139 | + * @return path for project 'target' folder |
| 140 | + */ |
| 141 | + private static Path getTargetPath() { |
| 142 | + return Paths.get(getBaseDir(), "target"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
56 | 146 | /** |
57 | 147 | * Get the next available path in sequence for the specified base name and extension in the specified folder. |
58 | 148 | * |
@@ -110,4 +200,13 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio |
110 | 200 | return targetPath.resolve(newName); |
111 | 201 | } |
112 | 202 |
|
| 203 | + /** |
| 204 | + * Get project base directory. |
| 205 | + * |
| 206 | + * @return project base directory |
| 207 | + */ |
| 208 | + public static String getBaseDir() { |
| 209 | + Path currentRelativePath = Paths.get(System.getProperty("user.dir")); |
| 210 | + return currentRelativePath.toAbsolutePath().toString(); |
| 211 | + } |
113 | 212 | } |
0 commit comments