@Test public void testValidateInvalidFile() { MockMultipartFile file = new MockMultipartFile("image1", "image1", "application/doc", "image".getBytes()); AjaxUpload obj = new AjaxUpload(); obj.setAttachment(file); BindException errors = new BindException(obj, "ajaxUpload"); imageValidator.validate(obj, errors); assertTrue(errors.hasErrors()); ObjectError error = errors.getFieldError("attachment"); assertNotNull(error); }
@Override public void validate(Object obj, Errors errors) { AjaxUpload ajaxUpload = (AjaxUpload) obj; MultipartFile attachment = ajaxUpload.getAttachment(); ValidationUtils.rejectIfEmpty(errors, "attachment", "photo.image-required"); if (attachment != null && !attachment.isEmpty()) { String contentType = attachment.getContentType().substring(0, 6); if (!"image/".equals(contentType)) { errors.rejectValue("attachment", "photo.invalid-file-type", "Invalid file. Profile Image must be in PNG, JPEG, GIF or BMP format."); } else if (attachment.getSize() > 1024 * 1024 * 10) { errors.rejectValue("attachment", "photo.invalid-file-size", "Invalid file. Maximum file size is 10 Megabytes"); } } }
ImageValidator implements Validator { @Override public void validate(Object obj, Errors errors) { AjaxUpload ajaxUpload = (AjaxUpload) obj; MultipartFile attachment = ajaxUpload.getAttachment(); ValidationUtils.rejectIfEmpty(errors, "attachment", "photo.image-required"); if (attachment != null && !attachment.isEmpty()) { String contentType = attachment.getContentType().substring(0, 6); if (!"image/".equals(contentType)) { errors.rejectValue("attachment", "photo.invalid-file-type", "Invalid file. Profile Image must be in PNG, JPEG, GIF or BMP format."); } else if (attachment.getSize() > 1024 * 1024 * 10) { errors.rejectValue("attachment", "photo.invalid-file-size", "Invalid file. Maximum file size is 10 Megabytes"); } } } }
ImageValidator implements Validator { @Override public void validate(Object obj, Errors errors) { AjaxUpload ajaxUpload = (AjaxUpload) obj; MultipartFile attachment = ajaxUpload.getAttachment(); ValidationUtils.rejectIfEmpty(errors, "attachment", "photo.image-required"); if (attachment != null && !attachment.isEmpty()) { String contentType = attachment.getContentType().substring(0, 6); if (!"image/".equals(contentType)) { errors.rejectValue("attachment", "photo.invalid-file-type", "Invalid file. Profile Image must be in PNG, JPEG, GIF or BMP format."); } else if (attachment.getSize() > 1024 * 1024 * 10) { errors.rejectValue("attachment", "photo.invalid-file-size", "Invalid file. Maximum file size is 10 Megabytes"); } } }  }
ImageValidator implements Validator { @Override public void validate(Object obj, Errors errors) { AjaxUpload ajaxUpload = (AjaxUpload) obj; MultipartFile attachment = ajaxUpload.getAttachment(); ValidationUtils.rejectIfEmpty(errors, "attachment", "photo.image-required"); if (attachment != null && !attachment.isEmpty()) { String contentType = attachment.getContentType().substring(0, 6); if (!"image/".equals(contentType)) { errors.rejectValue("attachment", "photo.invalid-file-type", "Invalid file. Profile Image must be in PNG, JPEG, GIF or BMP format."); } else if (attachment.getSize() > 1024 * 1024 * 10) { errors.rejectValue("attachment", "photo.invalid-file-size", "Invalid file. Maximum file size is 10 Megabytes"); } } }  boolean supports(Class<?> clazz); @Override void validate(Object obj, Errors errors); }
ImageValidator implements Validator { @Override public void validate(Object obj, Errors errors) { AjaxUpload ajaxUpload = (AjaxUpload) obj; MultipartFile attachment = ajaxUpload.getAttachment(); ValidationUtils.rejectIfEmpty(errors, "attachment", "photo.image-required"); if (attachment != null && !attachment.isEmpty()) { String contentType = attachment.getContentType().substring(0, 6); if (!"image/".equals(contentType)) { errors.rejectValue("attachment", "photo.invalid-file-type", "Invalid file. Profile Image must be in PNG, JPEG, GIF or BMP format."); } else if (attachment.getSize() > 1024 * 1024 * 10) { errors.rejectValue("attachment", "photo.invalid-file-size", "Invalid file. Maximum file size is 10 Megabytes"); } } }  boolean supports(Class<?> clazz); @Override void validate(Object obj, Errors errors);  }