VocabularySearchExprFactory { public SearchExpr parseExpression(String expression) { NavigableMap<Span, String> replacements = new TreeMap<>(); findReplacements(replacements, WORD_REPLACE.matcher(expression), wordsIndex); findReplacements(replacements, TERM_REPLACE.matcher(expression), termsIndex); findReplacements(replacements, NORM_REPLACE.matcher(expression), normsIndex); if (replacements.size() > 0) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(expression); for (Entry<Span, String> entry : replacements.descendingMap().entrySet()) { stringBuilder.replace(entry.getKey().getStartIndex(), entry.getKey().getEndIndex(), entry.getValue()); } expression = stringBuilder.toString(); } return searchExprFactory.parse(expression); } @Inject  VocabularySearchExprFactory(Vocabulary vocabulary, SearchExprFactory searchExprFactory); SearchExpr parseExpression(String expression);  }
@Test public void testNoReplace() { String expr = "someExpr"; factory.parseExpression(expr); verify(original).parse(expr); }
@Test public void testReplace() { String expr = "abc$words\"blah\"123$norms\"foo\"xyz$terms\"aTerm\""; when(dictionary.getTermIdentifier("blah")).thenReturn(StringIdentifier.withValue(1)); when(dictionary.getTermIdentifier("foo")).thenReturn(StringIdentifier.withValue(2)); when(dictionary.getTermIdentifier("aTerm")).thenReturn(StringIdentifier.withValue(3)); factory.parseExpression(expr); verify(original).parse("abc11232xyz3"); }