### Question:
HeaderLengthValidator extends Validator { @Override public void validate(Section section) { if (section.getLevel() >= getInt("min_level")) { return; } List<Sentence> headerContents = section.getHeaderContents(); int headerLength = headerContents.stream().map(s -> s.getContent().length()).reduce(0, (s, r) -> s + r); if (headerLength > getInt("max_len")) { addLocalizedError(section.getJoinedHeaderContents(), getInt("max_len")); } } HeaderLengthValidator(); @Override void validate(Section section);  }### Answer:
@Test void testSectionWithLongHeader() throws RedPenException { Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("HeaderLength")).build(); Validator validator = ValidatorFactory.getInstance(config.getValidatorConfigs().get(0), config); Section section = new Section(0, "This is a long long long long" + " long long long long long long long long header"); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(section); assertEquals(1, errors.size()); }
@Test void testSpecifyTheMaxLength() throws RedPenException { Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("HeaderLength").addProperty("max_len", "10")).build(); HeaderLengthValidator validator = (HeaderLengthValidator) ValidatorFactory.getInstance(config.getValidatorConfigs().get(0), config); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(new Section(1, "This is not a header")); assertEquals(1, errors.size()); }
@Test void testSectionWithLongHeaderInLowLevelSection() throws RedPenException { Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("HeaderLength")).build(); Validator validator = ValidatorFactory.getInstance(config.getValidatorConfigs().get(0), config); Section section = new Section(5, "This is a long long long long" + " long long long long long long long long header"); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(section); assertEquals(0, errors.size()); }
@Test void testSpecifyTheMinimumLevel() throws RedPenException { Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("HeaderLength").addProperty("min_level", "2")).build(); HeaderLengthValidator validator = (HeaderLengthValidator) ValidatorFactory.getInstance(config.getValidatorConfigs().get(0), config); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(new Section(5, "This is a long long long long" + " long long long long long long long long header")); assertEquals(0, errors.size()); }