BookSearchProcessor { @NonNull public Map<String, String> search() throws BookNotFoundException { if (query == null || query.isEmpty()) { return result; } String bookContent = repository.getBookContent(module, bookID); words = query.trim().split("\\s+"); for (String word : words) { BoyerMoorAlgorithm algorithm = new BoyerMoorAlgorithm(word); if (algorithm.indexOf(bookContent) == -1) { return result; } algoritms.put(word, algorithm); } int chapter = module.isChapterZero() ? 0 : 1; String chapterSign = module.getChapterSign(); SearchAlgorithm chapterAlgorithm = new BoyerMoorAlgorithm(chapterSign); int indexStart = chapterAlgorithm.indexOf(bookContent); while (indexStart != -1) { int indexEnd = chapterAlgorithm.indexOf(bookContent, indexStart + chapterSign.length()); searchInChapter(bookContent, indexStart, indexEnd, chapter); indexStart = indexEnd; chapter++; } return result; } BookSearchProcessor(IModuleRepository<D, T> repository, T module, String bookID, String query); @NonNull Map<String, String> search();  }
@Test public void searchOneWord() throws Exception { BookSearchProcessor<String, BaseModule> searchProcessor = new BookSearchProcessor<>(repository, module, "Gen", "авель"); Map<String, String> results = searchProcessor.search(); assertThat(results.size(), equalTo(3)); Set<String> references = results.keySet(); assertTrue(references.contains("RST.Gen.4.2")); assertTrue(references.contains("RST.Gen.4.4")); assertTrue(references.contains("RST.Gen.4.9")); }
@Test public void searchTwoWord() throws Exception { BookSearchProcessor<String, BaseModule> searchProcessor = new BookSearchProcessor<>(repository, module, "Gen", "авел каин"); Map<String, String> results = searchProcessor.search(); assertThat(results.size(), equalTo(3)); Set<String> references = results.keySet(); assertTrue(references.contains("RST.Gen.4.2")); assertTrue(references.contains("RST.Gen.4.8")); assertTrue(references.contains("RST.Gen.4.25")); }
@Test public void searchWithEmptyQuery() throws Exception { BookSearchProcessor<String, BaseModule> searchProcessor = new BookSearchProcessor<>(repository, module, "Gen", ""); Map<String, String> results = searchProcessor.search(); assertNotNull(results); assertThat(results.size(), equalTo(0)); }