@Test public void destinationExistsPolicySkip() throws IOException { ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); SetMetadataParameters params = new SetMetadataParameters(); params.setOutput(new FileTaskOutput(folder.newFile())); params.setExistingOutputPolicy(ExistingOutputPolicy.SKIP); assertFalse(victim.isValid(params, context)); verify(context).buildConstraintViolationWithTemplate(contains("File destination already exists")); }
@Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value)) { if (isNull(value.getOutput())) { return false; } if (isNull(value.getOutput().getDestination()) || (value.getOutput().getDestination().exists() && (value.getExistingOutputPolicy() == ExistingOutputPolicy.FAIL || value.getExistingOutputPolicy() == ExistingOutputPolicy.SKIP))) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("File destination already exists: %s.", value.getOutput().getDestination())) .addConstraintViolation(); return false; } } return true; }
SingleOutputValidator implements ConstraintValidator<ValidSingleOutput, SingleOutputTaskParameters> { @Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value)) { if (isNull(value.getOutput())) { return false; } if (isNull(value.getOutput().getDestination()) || (value.getOutput().getDestination().exists() && (value.getExistingOutputPolicy() == ExistingOutputPolicy.FAIL || value.getExistingOutputPolicy() == ExistingOutputPolicy.SKIP))) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("File destination already exists: %s.", value.getOutput().getDestination())) .addConstraintViolation(); return false; } } return true; } }
SingleOutputValidator implements ConstraintValidator<ValidSingleOutput, SingleOutputTaskParameters> { @Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value)) { if (isNull(value.getOutput())) { return false; } if (isNull(value.getOutput().getDestination()) || (value.getOutput().getDestination().exists() && (value.getExistingOutputPolicy() == ExistingOutputPolicy.FAIL || value.getExistingOutputPolicy() == ExistingOutputPolicy.SKIP))) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("File destination already exists: %s.", value.getOutput().getDestination())) .addConstraintViolation(); return false; } } return true; }  }
SingleOutputValidator implements ConstraintValidator<ValidSingleOutput, SingleOutputTaskParameters> { @Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value)) { if (isNull(value.getOutput())) { return false; } if (isNull(value.getOutput().getDestination()) || (value.getOutput().getDestination().exists() && (value.getExistingOutputPolicy() == ExistingOutputPolicy.FAIL || value.getExistingOutputPolicy() == ExistingOutputPolicy.SKIP))) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("File destination already exists: %s.", value.getOutput().getDestination())) .addConstraintViolation(); return false; } } return true; }  @Override void initialize(ValidSingleOutput constraintAnnotation); @Override boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context); }
SingleOutputValidator implements ConstraintValidator<ValidSingleOutput, SingleOutputTaskParameters> { @Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value)) { if (isNull(value.getOutput())) { return false; } if (isNull(value.getOutput().getDestination()) || (value.getOutput().getDestination().exists() && (value.getExistingOutputPolicy() == ExistingOutputPolicy.FAIL || value.getExistingOutputPolicy() == ExistingOutputPolicy.SKIP))) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("File destination already exists: %s.", value.getOutput().getDestination())) .addConstraintViolation(); return false; } } return true; }  @Override void initialize(ValidSingleOutput constraintAnnotation); @Override boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context);  }