### Question:
RegexMessageAnnotator extends AbstractMessageAnnotator { @Override public Collection<MessageAnnotation> annotate(String text) { Collection<MessageAnnotation> annotations = new ArrayList<>(); int start = 0; Matcher m = pattern.matcher(text); while (m.find()) { int mStart = m.start(); int mEnd = m.end(); if (mStart > start) { String previous = text.substring(start, mStart); annotations.add(MessageAnnotation.t(previous)); } String match = m.group(); MessageAnnotation annotation = annotationFactory.apply(match); annotations.add(annotation); start = mEnd; } if (start < text.length() - 1) { String reminder = text.substring(start); annotations.add(MessageAnnotation.t(reminder)); } return annotations; } RegexMessageAnnotator(String pattern, Function<String, MessageAnnotation> annotationFactory);  RegexMessageAnnotator(Pattern pattern, Function<String, MessageAnnotation> annotationFactory); @Override Collection<MessageAnnotation> annotate(String text);  }### Answer:
@Test public void no_match() { Collection<MessageAnnotation> annotations = new RegexMessageAnnotator(REGEX, annotationFunction).annotate("No match here."); assertNotNull(annotations); assertEquals( Arrays.asList( MessageAnnotation.t("No match here.") ), annotations); }
@Test public void one_match() { Collection<MessageAnnotation> annotations = new RegexMessageAnnotator(REGEX, annotationFunction).annotate("#177 match here."); assertNotNull(annotations); assertEquals( Arrays.asList( annotationFunction.apply("#177"), MessageAnnotation.t(" match here.") ), annotations); }
@Test public void several_matches() { Collection<MessageAnnotation> annotations = new RegexMessageAnnotator(REGEX, annotationFunction).annotate("#177 #178 matches."); assertNotNull(annotations); assertEquals( Arrays.asList( annotationFunction.apply("#177"), MessageAnnotation.t(" "), annotationFunction.apply("#178"), MessageAnnotation.t(" matches.") ), annotations); }
@Test public void identical_matches() { Collection<MessageAnnotation> annotations = new RegexMessageAnnotator(REGEX, annotationFunction).annotate("#177 #177 matches."); assertNotNull(annotations); assertEquals( Arrays.asList( annotationFunction.apply("#177"), MessageAnnotation.t(" "), annotationFunction.apply("#177"), MessageAnnotation.t(" matches.") ), annotations); }