/** The Constant VALIDATOR_FACTORY. */
static final ValidatorFactory VALIDATOR_FACTORY = Validation.byDefaultProvider()
.configure()
.buildValidatorFactory();
/** The Constant VALIDATOR. */
static final Validator VALIDATOR = VALIDATOR_FACTORY.getValidator();
public static class WithMap {
@Valid
final private Map<String, @Size(min = 1) List<String>> lists;
public WithMap(Map<String, List<String>> lists) {
super();
this.lists = lists;
}
public Map<String, List<String>> getLists() {
return this.lists;
}
}
/**
* Test fails as it does not consider the property path when checking if already validated.
*/
@Test
public void testSameInstance() {
List<String> emptyList = new ArrayList<>();
String[] stringArray = { "A" };
List<String> populatedList = Arrays.asList(stringArray);
HashMap<String, List<String>> map = new HashMap<>();
map.put("POPULATED", populatedList);
map.put("EMPTY_LIST1", emptyList);
map.put("EMPTY_LIST2", emptyList);
WithMap withMap = new WithMap(map);
Set<ConstraintViolation<WithMap>> constraintViolations = VALIDATOR.validate(withMap);
assertEquals(2, constraintViolations.size());
}
/**
* Test passes since the lists are different instances.
*/
@Test
public void testDifferentInstance() {
String[] stringArray = { "A" };
List<String> populatedList = Arrays.asList(stringArray);
HashMap<String, List<String>> map = new HashMap<>();
map.put("POPULATED", populatedList);
map.put("EMPTY_LIST1", new ArrayList<>());
map.put("EMPTY_LIST2", new ArrayList<>());
WithMap withMap = new WithMap(map);
Set<ConstraintViolation<WithMap>> constraintViolations = VALIDATOR.validate(withMap);
assertEquals(2, constraintViolations.size());
}