Skip to content

Commit

Permalink
Handle more filename cases in the ExtensionValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-boardontrack committed Aug 27, 2024
1 parent 1f90546 commit a326e5f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private String getExtension(Object value) throws IllegalArgumentException {
fileName = ((File) value).getName();
} else if (value instanceof Path) {
fileName = ((Path) value).getFileName().toString();
} else if (value instanceof String) {
throw new ValidationException("Cannot get a extension from java.lang.String.");
} else {
throw new ValidationException("Input must be a File or Path.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,90 @@ void shouldThrownExceptionWhenNonExpectedObjectIsPassed() {
assertEquals(expectedMessage, actualMessage);
}

@Test
void shouldThrownExceptionForHiddenFilesInFile() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File(".hiddenfile");
var exception = assertThrows(ValidationException.class, () -> validator.isValid(file));
var actualMessage = exception.getMessage();
var expectedMessage = "No valid file extension found.";
assertEquals(expectedMessage, actualMessage);
}

@Test
void shouldThrownExceptionForHiddenFilesInPath() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File(".hiddenfile");
var exception = assertThrows(ValidationException.class, () -> validator.isValid(file.toPath()));
var actualMessage = exception.getMessage();
var expectedMessage = "No valid file extension found.";
assertEquals(expectedMessage, actualMessage);
}

@Test
void shouldThrownExceptionForEmptyFilenameInFile() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File("");
var exception = assertThrows(ValidationException.class, () -> validator.isValid(file));
var actualMessage = exception.getMessage();
var expectedMessage = "File name is null or empty.";
assertEquals(expectedMessage, actualMessage);
}

@Test
void shouldThrownExceptionForEmptyFilenameInPath() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File("");
var exception = assertThrows(ValidationException.class, () -> validator.isValid(file.toPath()));
var actualMessage = exception.getMessage();
var expectedMessage = "File name is null or empty.";
assertEquals(expectedMessage, actualMessage);
}

@Test
void shouldSucceedWithExtraPeriodsInFilename() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File("/a/b/c/foo.bar.baz.mp3");
assertEquals(Boolean.TRUE, validator.isValid(file));
}

@Test
void shouldSucceedWithExtraPeriodsInPath() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File("a/b/c/foo.bar.baz.mp3");
assertEquals(Boolean.TRUE, validator.isValid(file.toPath()));
}

@Test
void shouldReturnFalseWithExtraPeriodsInFile() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File("a/b/c/foo.mp3.wav.txt");
assertEquals(Boolean.FALSE, validator.isValid(file));
}

@Test
void shouldReturnFalseWithExtraPeriodsInPath() {
var validator = new ExtensionValidator();
var annotation = Sample.extension(new String[] { "mp3", "wav" });
validator.initialize(annotation);
File file = new File("a/b/c/foo.mp3.wav.txt");
assertEquals(Boolean.FALSE, validator.isValid(file.toPath()));
}

static class Sample {

static Extension extension(String[] value) {
Expand Down

0 comments on commit a326e5f

Please sign in to comment.