FileExtensionValidator implements ConstraintValidator<FileExtension, File> { @Override public boolean isValid(File value, ConstraintValidatorContext context) { if (value != null && value.isFile()) { String extension = FilenameUtils.getExtension(value.getName()); return equalsIgnoreCase(expectedExtension, extension) && indexOfExtension(value.getName()) > 0; } return true; }  @Override void initialize(FileExtension constraintAnnotation); @Override boolean isValid(File value, ConstraintValidatorContext context);  }
@Test public void testEmptyExtension() { when(mockFile.getName()).thenReturn("noExtension"); assertFalse(victim.isValid(mockFile, null)); }
@Test public void testOnlyExtension() { when(mockFile.getName()).thenReturn(".pdf"); assertFalse(victim.isValid(mockFile, null)); }
@Test public void testWrongExtension() { when(mockFile.getName()).thenReturn("Bla.txt"); assertFalse(victim.isValid(mockFile, null)); }
@Test public void testCaseInsensitiveExtension() { when(mockFile.getName()).thenReturn("bla.PdF"); assertTrue(victim.isValid(mockFile, null)); }
@Test public void testExtension() { when(mockFile.getName()).thenReturn("bla.pdf"); assertTrue(victim.isValid(mockFile, null)); }