Skip to content

Commit 844a0df

Browse files
committed
Add path methods for selecting reports directories and subclass names
1 parent a60cdc2 commit 844a0df

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/main/java/com/nordstrom/common/file/PathUtils.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.file.Files;
77
import java.nio.file.Path;
88
import java.nio.file.PathMatcher;
9+
import java.nio.file.Paths;
910
import java.util.Comparator;
1011
import java.util.Objects;
1112
import java.util.Optional;
@@ -53,6 +54,64 @@ private PathUtils() {
5354
throw new AssertionError("PathUtils is a static utility class that cannot be instantiated");
5455
}
5556

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+
56115
/**
57116
* Get the next available path in sequence for the specified base name and extension in the specified folder.
58117
*
@@ -110,4 +169,14 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
110169
return targetPath.resolve(newName);
111170
}
112171

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+
}
113182
}

0 commit comments

Comments
 (0)