Skip to content

Commit

Permalink
add null checks to resource helper (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
bischoffz authored Feb 5, 2025
1 parent 9b1949f commit 1b19824
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
public enum ResourceError implements ContractError {
UNKNOWN_FILE("Provided file does not exist"),
FILE_PATH_IS_DIRECTORY("The provided file path points to a directory and not a file"),
DIRECTORY_PATH_IS_FILE("The provided directory path points to a file and not a directory");
DIRECTORY_PATH_IS_FILE("The provided directory path points to a file and not a directory"),
NULL_CLASS_REF("The given class ref is null"),
NULL_DIRECTORY_PATH("The given directory path is null"),
NULL_DIRECTORY_STRING("The given directory string is null"),
NULL_FILE_PATH("The given file path is null"),
NULL_FILE_STRING("The given file string is null"),
;

private final String description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* This solves the issues by obtaining an absolute reference to the resource
* directory by using the class loader and an empty resource.
* <p>
* In addition to the above, this class also provides convience methods to
* In addition to the above, this class also provides convenience methods to
* validate file paths and directory paths, and create directories and files.
*/
public class ResourceHelper {
Expand All @@ -44,6 +44,9 @@ public class ResourceHelper {
* {@link URISyntaxException}
*/
public static Path getResourceDir(Class<?> classRef) {
if (classRef == null) {
throw new ContractException(ResourceError.NULL_CLASS_REF);
}
return Path.of(getURI(classRef.getClassLoader().getResource("")));
}

Expand All @@ -52,6 +55,10 @@ public static Path getResourceDir(Class<?> classRef) {
* dirPath.toFile().mkdirs().
*/
public static Path createDirectory(Path dirPath) {
if (dirPath == null) {
throw new ContractException(ResourceError.NULL_DIRECTORY_PATH);
}

if (dirPath.toFile().exists()) {
return dirPath;
}
Expand All @@ -67,6 +74,10 @@ public static Path createDirectory(Path dirPath) {
* calls {@link ResourceHelper#createDirectory(Path)}
*/
public static Path createDirectory(String directory) {
if (directory == null || directory.isEmpty()) {
throw new ContractException(ResourceError.NULL_DIRECTORY_STRING);
}

Path dirPath = Path.of(directory);

return createDirectory(dirPath);
Expand All @@ -79,6 +90,14 @@ public static Path createDirectory(String directory) {
* returns the resolved path
*/
public static Path createDirectory(Path baseDirPath, String subDir) {
if (baseDirPath == null) {
throw new ContractException(ResourceError.NULL_DIRECTORY_PATH);
}

if (subDir == null || subDir.isEmpty()) {
throw new ContractException(ResourceError.NULL_DIRECTORY_STRING);
}

Path dirPath = baseDirPath.resolve(subDir);

return createDirectory(dirPath);
Expand All @@ -91,6 +110,14 @@ public static Path createDirectory(Path baseDirPath, String subDir) {
* returns the resolved path
*/
public static Path createDirectory(String baseDir, String subDir) {
if (baseDir == null || baseDir.isEmpty()) {
throw new ContractException(ResourceError.NULL_DIRECTORY_STRING);
}

if (subDir == null || subDir.isEmpty()) {
throw new ContractException(ResourceError.NULL_DIRECTORY_STRING);
}

Path dirPath = Path.of(baseDir, subDir);

return createDirectory(dirPath);
Expand All @@ -105,6 +132,13 @@ public static Path createDirectory(String baseDir, String subDir) {
* {@link IOException}
*/
public static void createFile(Path directory, String fileName) {
if (directory == null) {
throw new ContractException(ResourceError.NULL_DIRECTORY_PATH);
}

if (fileName == null || fileName.isEmpty()) {
throw new ContractException(ResourceError.NULL_FILE_STRING);
}

File file = directory.resolve(fileName).toFile();

Expand All @@ -126,6 +160,13 @@ public static void createFile(Path directory, String fileName) {
* {@link IOException}
*/
public static void createNewFile(Path directory, String fileName) {
if (directory == null) {
throw new ContractException(ResourceError.NULL_DIRECTORY_PATH);
}

if (fileName == null || fileName.isEmpty()) {
throw new ContractException(ResourceError.NULL_FILE_STRING);
}

File file = directory.resolve(fileName).toFile();

Expand All @@ -150,6 +191,10 @@ public static void createNewFile(Path directory, String fileName) {
* </ul>
*/
public static Path validateFile(String file) {
if (file == null || file.isEmpty()) {
throw new ContractException(ResourceError.NULL_FILE_STRING);
}

Path filePath = Path.of(file);

validateFile(filePath);
Expand All @@ -169,6 +214,10 @@ public static Path validateFile(String file) {
* </ul>
*/
public static Path validateFile(Path filePath) {
if (filePath == null) {
throw new ContractException(ResourceError.NULL_FILE_PATH);
}

File file = filePath.toFile();

if (file.isDirectory()) {
Expand All @@ -191,6 +240,10 @@ public static Path validateFile(Path filePath) {
* file path refers to a directory
*/
public static Path validateFilePath(String file) {
if (file == null || file.isEmpty()) {
throw new ContractException(ResourceError.NULL_FILE_STRING);
}

Path filePath = Path.of(file);

validateFilePath(filePath);
Expand All @@ -205,6 +258,10 @@ public static Path validateFilePath(String file) {
* file path refers to a directory
*/
public static Path validateFilePath(Path filePath) {
if (filePath == null) {
throw new ContractException(ResourceError.NULL_FILE_PATH);
}

File file = filePath.toFile();

if (file.isDirectory()) {
Expand All @@ -227,6 +284,10 @@ public static Path validateFilePath(Path filePath) {
* directory path refers to a file
*/
public static Path validateDirectoryPath(String directory) {
if (directory == null || directory.isEmpty()) {
throw new ContractException(ResourceError.NULL_DIRECTORY_STRING);
}

Path maybePath = Path.of(directory);

validateDirectoryPath(maybePath);
Expand All @@ -243,6 +304,10 @@ public static Path validateDirectoryPath(String directory) {
* directory path refers to a file
*/
public static Path validateDirectoryPath(Path directoryPath) {
if (directoryPath == null) {
throw new ContractException(ResourceError.NULL_DIRECTORY_PATH);
}

File maybeFile = directoryPath.toFile();

if (maybeFile.isFile()) {
Expand Down
Loading

0 comments on commit 1b19824

Please sign in to comment.