@Test public void testIsValid() throws Exception { final String existingFile = new File(this.getClass().getResource("/i-exists.txt").getPath()).getAbsolutePath(); Set<ConstraintViolation<PathsWrapper>> constraintViolations = validator.validate(new PathsWrapper("c:/test", "c:/test/jdk.zip", existingFile)); assertTrue(constraintViolations.isEmpty()); constraintViolations = validator.validate(new PathsWrapper("c:\\test", "c:/test/jdk.jar", existingFile)); assertTrue(constraintViolations.isEmpty()); constraintViolations = validator.validate(new PathsWrapper("c:\\test/any", "c:\\jdk.war", existingFile)); assertTrue(constraintViolations.isEmpty()); constraintViolations = validator.validate(new PathsWrapper("c:\\test/any", "c:\\jdk", existingFile)); assertTrue(constraintViolations.size() == 1); assertEquals("invalid dirAndFilename", constraintViolations.iterator().next().getMessage()); constraintViolations = validator.validate(new PathsWrapper("c:\\test/any", "c:\\jdk.peanuts", existingFile)); assertEquals("invalid dirAndFilename", constraintViolations.iterator().next().getMessage()); assertTrue(constraintViolations.size() == 1); if (SystemUtils.IS_OS_WINDOWS) { constraintViolations = validator.validate(new PathsWrapper("c::\\test/any", "c:\\jdk.zip", existingFile)); assertEquals("invalid dir", constraintViolations.iterator().next().getMessage()); assertTrue(constraintViolations.size() == 1); } constraintViolations = validator.validate(new PathsWrapper("/unix/path", "/jdk.zip", existingFile)); assertTrue(constraintViolations.isEmpty()); constraintViolations = validator.validate(new PathsWrapper("/unix/path", "/jdk.zip", "/this/should/not/point/to/an/existing/file")); assertTrue(constraintViolations.size() == 1); assertEquals("invalid existingPath", constraintViolations.iterator().next().getMessage()); }
@Override public boolean isValid(final String value, final ConstraintValidatorContext context) { try { if (StringUtils.isEmpty(value)) { return true; } final Path path = Paths.get(value); if (checkIfExists && !path.toFile().exists()) { return false; } final String fileExt = FilenameUtils.getExtension(value); return allowableFileExtensions.isEmpty() || !allowableFileExtensions.stream().filter(fileExt::equalsIgnoreCase).findFirst().orElse(StringUtils.EMPTY).isEmpty(); } catch (final InvalidPathException e) { LOGGER.error(e.getMessage(), e); return false; } }
PathValidator implements ConstraintValidator<ValidPath, String> { @Override public boolean isValid(final String value, final ConstraintValidatorContext context) { try { if (StringUtils.isEmpty(value)) { return true; } final Path path = Paths.get(value); if (checkIfExists && !path.toFile().exists()) { return false; } final String fileExt = FilenameUtils.getExtension(value); return allowableFileExtensions.isEmpty() || !allowableFileExtensions.stream().filter(fileExt::equalsIgnoreCase).findFirst().orElse(StringUtils.EMPTY).isEmpty(); } catch (final InvalidPathException e) { LOGGER.error(e.getMessage(), e); return false; } } }
PathValidator implements ConstraintValidator<ValidPath, String> { @Override public boolean isValid(final String value, final ConstraintValidatorContext context) { try { if (StringUtils.isEmpty(value)) { return true; } final Path path = Paths.get(value); if (checkIfExists && !path.toFile().exists()) { return false; } final String fileExt = FilenameUtils.getExtension(value); return allowableFileExtensions.isEmpty() || !allowableFileExtensions.stream().filter(fileExt::equalsIgnoreCase).findFirst().orElse(StringUtils.EMPTY).isEmpty(); } catch (final InvalidPathException e) { LOGGER.error(e.getMessage(), e); return false; } }  }
PathValidator implements ConstraintValidator<ValidPath, String> { @Override public boolean isValid(final String value, final ConstraintValidatorContext context) { try { if (StringUtils.isEmpty(value)) { return true; } final Path path = Paths.get(value); if (checkIfExists && !path.toFile().exists()) { return false; } final String fileExt = FilenameUtils.getExtension(value); return allowableFileExtensions.isEmpty() || !allowableFileExtensions.stream().filter(fileExt::equalsIgnoreCase).findFirst().orElse(StringUtils.EMPTY).isEmpty(); } catch (final InvalidPathException e) { LOGGER.error(e.getMessage(), e); return false; } }  @Override void initialize(final ValidPath constraintAnnotation); @Override boolean isValid(final String value, final ConstraintValidatorContext context); }
PathValidator implements ConstraintValidator<ValidPath, String> { @Override public boolean isValid(final String value, final ConstraintValidatorContext context) { try { if (StringUtils.isEmpty(value)) { return true; } final Path path = Paths.get(value); if (checkIfExists && !path.toFile().exists()) { return false; } final String fileExt = FilenameUtils.getExtension(value); return allowableFileExtensions.isEmpty() || !allowableFileExtensions.stream().filter(fileExt::equalsIgnoreCase).findFirst().orElse(StringUtils.EMPTY).isEmpty(); } catch (final InvalidPathException e) { LOGGER.error(e.getMessage(), e); return false; } }  @Override void initialize(final ValidPath constraintAnnotation); @Override boolean isValid(final String value, final ConstraintValidatorContext context);  }