### Question:
InvalidWordValidator extends DictionaryValidator { @Override public void validate(Sentence sentence) { for (TokenElement token : sentence.getTokens()) { String word = token.getSurface().toLowerCase(); if (inDictionary(word)) { addLocalizedErrorFromToken(sentence, token); } } } InvalidWordValidator(); @Override List<String> getSupportedLanguages(); @Override void validate(Sentence sentence);  }### Answer:
@Test void testSimpleRun() throws RedPenException { List<Document> documents = new ArrayList<>();documents.add( Document.builder() .addSection(1) .addParagraph() .addSentence(new Sentence("He is a foolish guy.", 1)) .build()); Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("InvalidWord").addProperty("list", "foolish")) .build(); Validator validator = ValidatorFactory.getInstance(config.getValidatorConfigs().get(0), config); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(documents.get(0).getLastSection().getParagraph(0).getSentence(0)); assertEquals(1, errors.size()); }
@Test void testVoid() throws RedPenException { List<Document> documents = new ArrayList<>();documents.add( Document.builder() .addSection(1) .addParagraph() .addSentence(new Sentence("", 1)) .build()); Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("InvalidWord").addProperty("list", "foolish")) .build(); Validator validator = ValidatorFactory.getInstance(config.getValidatorConfigs().get(0), config); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(documents.get(0).getLastSection().getParagraph(0).getSentence(0)); assertEquals(0, errors.size()); }
@Test void testLoadDefaultDictionary() throws RedPenException { Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("InvalidWord")) .build(); List<Document> documents = new ArrayList<>();documents.add( Document.builder() .addSection(1) .addParagraph() .addSentence(new Sentence("he is a foolish man.", 1)) .build()); RedPen redPen = new RedPen(config); Map<Document, List<ValidationError>> errors = redPen.validate(documents); assertEquals(1, errors.get(documents.get(0)).size()); assertTrue(errors.get(documents.get(0)).get(0).getMessage().contains("foolish")); }
@Test void testLoadUserDictionary() throws RedPenException { Configuration config = Configuration.builder() .addValidatorConfig(new ValidatorConfiguration("InvalidWord").addProperty("list", "boom,domo")) .build(); List<Document> documents = new ArrayList<>();documents.add( Document.builder() .addSection(1) .addParagraph() .addSentence(new Sentence("Domo is a greeting word in Japan.", 1)) .build()); RedPen redPen = new RedPen(config); Map<Document, List<ValidationError>> errors = redPen.validate(documents); assertEquals(1, errors.get(documents.get(0)).size()); }
@Test void testLoadNotExistingDefaultDictionary() throws RedPenException { Configuration config = Configuration.builder("ja") .addValidatorConfig(new ValidatorConfiguration("InvalidWord")) .build(); List<Document> documents = new ArrayList<>();documents.add( Document.builder(new NeologdJapaneseTokenizer()) .addSection(1) .addParagraph() .addSentence(new Sentence("こんにちは、群馬にきました。", 1)) .build()); RedPen redPen = new RedPen(config); Map<Document, List<ValidationError>> errors = redPen.validate(documents); assertEquals(0, errors.get(documents.get(0)).size()); }