Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.persist.validation

File ModelValidator.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
43% of files have more coverage

Code metrics

0
10
6
1
56
46
6
0.6
1.67
6
1

Classes

Class Line # Actions
ModelValidator 12 10 6
0.312531.2%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.persist.validation;
2   
3    import java.util.Set;
4    import javax.validation.*;
5    import org.springframework.beans.BeansException;
6    import org.springframework.beans.factory.InitializingBean;
7    import org.springframework.context.ApplicationContext;
8    import org.springframework.context.ApplicationContextAware;
9    import org.springframework.validation.Errors;
10    import org.springframework.validation.Validator;
11   
 
12    public class ModelValidator
13    implements Validator, ConstraintValidatorFactory, InitializingBean, ApplicationContextAware {
14   
15    private ApplicationContext applicationContext;
16    private javax.validation.Validator validator;
17   
 
18  0 toggle @Override
19    public boolean supports(Class<?> clazz) {
20  0 return true;
21    }
22   
 
23  0 toggle @Override
24    public void validate(Object target, Errors errors) {
25   
26  0 Set<ConstraintViolation<Object>> validate = validator.validate(target);
27  0 for (ConstraintViolation<Object> violation : validate) {
28  0 final String path = violation.getPropertyPath().toString();
29  0 final String message = violation.getMessage();
30  0 errors.rejectValue(path, message);
31    }
32    }
33   
 
34  4 toggle @Override
35    public void afterPropertiesSet() throws Exception {
36  4 ValidatorFactory validatorFactory =
37    Validation.byDefaultProvider()
38    .configure()
39    .constraintValidatorFactory(this)
40    .buildValidatorFactory();
41  4 validator = validatorFactory.usingContext().getValidator();
42    }
43   
 
44  4 toggle @Override
45    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
46  4 this.applicationContext = applicationContext;
47    }
48   
 
49  0 toggle @Override
50    public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
51  0 return applicationContext.getBean(key);
52    }
53   
 
54  0 toggle @Override
55    public void releaseInstance(ConstraintValidator<?, ?> instance) {}
56    }