-
Notifications
You must be signed in to change notification settings - Fork 41
Add util classes for data loader CLI #2616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 25 commits
be9ee23
89b9f05
49c83b6
b2871fb
c5c9c0a
4964e8d
ff81f5f
3934c2a
9958f95
1afbc21
8c5114d
ffab395
79df1ed
cf31672
6dd213e
6542177
603e46e
eaf9d88
0a2518a
378effb
8a71c75
df32a6f
e76e27a
bed04c6
8dc0d8a
872412f
f78de2d
0d55aa8
8ad50d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.scalar.db.dataloader.cli.util; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| public class FileUtils { | ||
|
|
||
| /** | ||
| * Validates the provided file path. | ||
| * | ||
| * @param filePath the file path to validate | ||
| * @throws InvalidFilePathException if the file path is invalid | ||
| */ | ||
| public static void validateFilePath(String filePath) throws InvalidFilePathException { | ||
| if (StringUtils.isBlank(filePath)) { | ||
| throw new IllegalArgumentException("File path must not be blank."); | ||
| } | ||
| Path pathToCheck = Paths.get(filePath); | ||
|
|
||
| if (!pathToCheck.isAbsolute()) { | ||
| // If the path is not absolute, it's either a file name or a relative path | ||
| Path currentDirectory = Paths.get("").toAbsolutePath(); | ||
| Path fileInCurrentDirectory = currentDirectory.resolve(pathToCheck); | ||
|
||
|
|
||
| if (!fileInCurrentDirectory.toFile().exists()) { | ||
| throw new InvalidFilePathException("File not found: " + pathToCheck); | ||
|
||
| } | ||
| return; | ||
| } | ||
|
|
||
| // It's an absolute path | ||
| if (!pathToCheck.toFile().exists()) { | ||
| throw new InvalidFilePathException("File not found: " + pathToCheck); | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.scalar.db.dataloader.cli.util; | ||
|
|
||
| public class InvalidFilePathException extends Exception { | ||
|
|
||
| public InvalidFilePathException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public InvalidFilePathException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.scalar.db.dataloader.cli.util; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.nio.file.Paths; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class FileUtilsTest { | ||
|
|
||
| private static final String currentPath = Paths.get("").toAbsolutePath().toString(); | ||
|
|
||
| @Test | ||
| void validateFilePath_withValidFilePath_shouldNotThrowException() | ||
| throws InvalidFilePathException { | ||
| // Test and confirm no exception is thrown when a valid path is provided | ||
| FileUtils.validateFilePath(currentPath); | ||
| } | ||
|
|
||
| @Test | ||
| void validateFilePath_withInvalidFilePath_shouldThrowException() { | ||
| assertThatThrownBy(() -> FileUtils.validateFilePath(currentPath + "/demo")) | ||
| .isInstanceOf(InvalidFilePathException.class) | ||
| .hasMessage("File not found: " + currentPath + "/demo"); | ||
| } | ||
|
|
||
| @Test | ||
| void validateFilePath_withBlankFilePath_shouldThrowException() { | ||
| assertThatThrownBy(() -> FileUtils.validateFilePath("")) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("File path must not be blank."); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this message to
CoreError.