Skip to content

Commit 4918f00

Browse files
Babcock, ScottBabcock, Scott
authored andcommitted
Merge pull request #7 in MFATT/common from pr/add-exception-utils to master
* commit '4e4e218fb89fc7d01ce8416b1b18718f6fe1c204': Remove extra blank line Add JavaDoc Add path methods for selecting reports directories and subclass names
2 parents a60cdc2 + 4e4e218 commit 4918f00

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

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

Lines changed: 100 additions & 1 deletion
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;
@@ -47,12 +48,101 @@
4748
* ...
4849
* </pre>
4950
*/
50-
public class PathUtils {
51+
public final class PathUtils {
5152

5253
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+
/**
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+
56146
/**
57147
* Get the next available path in sequence for the specified base name and extension in the specified folder.
58148
*
@@ -110,4 +200,13 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
110200
return targetPath.resolve(newName);
111201
}
112202

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

0 commit comments

Comments
 (0)