CollectingFailureReporter extends ValidationFailureReporter { public ValidationException inContextOfSchema(Schema schema, Runnable task) { int failureCountBefore = failures.size(); super.inContextOfSchema(schema, task); int failureCountAfter = failures.size(), newFailureCount = failureCountAfter - failureCountBefore; if (newFailureCount == 0) { return null; } else if (newFailureCount == 1) { return failures.remove(failures.size() - 1); } else { List<ValidationException> newFailures = new ArrayList<>(failures.subList(failureCountBefore, failures.size())); int toBeRemoved = newFailureCount, lastIndex = failureCountAfter; while (toBeRemoved-- > 0) { failures.remove(--lastIndex); } return ValidationException.createWrappingException(schema, newFailures); } } CollectingFailureReporter(Schema schema); @Override void failure(ValidationException exc); void validationFinished(); ValidationException inContextOfSchema(Schema schema, Runnable task);  }
@Test public void noNewExceptions_returnsNull() { ValidationException actual = createSubject().inContextOfSchema(NullSchema.INSTANCE, NOOP); assertNull(actual); }
@Test(expected = NullPointerException.class) public void subSchemaIsNull() { createSubject().inContextOfSchema(null, () -> { }); }