UrlFinder { List<MatchedUrl> findUrlsIn(CharSequence charSequence) { List<MatchedUrl> urls = new ArrayList<>(); if (isEmpty(charSequence)) { return urls; } Matcher matcher = urlPattern.matcher(charSequence); while (matcher.find()) { urls.add(new MatchedUrl(matcher.group(), matcher.start(), matcher.end())); } return urls; } UrlFinder(Pattern urlPattern);   }
@Test public void passingNullReturnsEmptyList() { List<MatchedUrl> urls = finder.findUrlsIn(null); assertThat(urls.size()).isEqualTo(0); }
@Test public void passingEmptyStringReturnsEmptyList() { List<MatchedUrl> urls = finder.findUrlsIn(""); assertThat(urls.size()).isEqualTo(0); }
@Test public void urlIsCorrect() { String url = "http: int start = 0; int end = url.length(); MatchedUrl expectedMatchedUrl = new MatchedUrl(url, start, end); String fullString = url + " is a great url!"; List<MatchedUrl> urls = finder.findUrlsIn(fullString); MatchedUrl matchedUrl = urls.get(0); assertThat(matchedUrl).isEqualTo(expectedMatchedUrl); }
@Test public void finderFindsTheCorrectNumberOfUrls() { String url = "List of URLs\n 1) http: List<MatchedUrl> urls = finder.findUrlsIn(url); assertThat(urls.size()).isEqualTo(2); }