MatcherUtilsImpl implements MatcherUtils { @Override public <T, M extends Matchable<T>> Optional<M> getMostSpecificMatch(@Nonnull Iterable<M> matchables, @Nonnull T target) { checkNotNull(matchables, "matchables is null"); checkNotNull(target, "target is null"); double maxSpecificity = -1; Optional<M> argMax = absent(); for (M matchable : matchables) { Optional<Double> specificity = getMatchSpecificity(matchable.getMatcher(), target); if (specificity.isPresent() && specificity.get() > maxSpecificity) { maxSpecificity = specificity.get(); argMax = of(matchable); } } return argMax; }  @Override @Nonnull Optional<Group> getMostSpecificMatchingGroup(@Nonnull Iterable<Group> groups, @Nonnull T target); @Override Optional<Double> getMostSpecificMatchSpecificity(@Nonnull Iterable<M> matchables, @Nonnull T target); @Override Optional<M> getMostSpecificMatch(@Nonnull Iterable<M> matchables, @Nonnull T target); @Override Optional<Double> getMatchSpecificity(@Nonnull Matcher<T> matcher, @Nonnull T target);  }
@Test(expected = NullPointerException.class) public void givenMatchersIsNull_whenGetMostSpecificMatch_thenThrowsNPE() { List<Matchable<String>> matchers = null; String target = "googlebot"; utilities.getMostSpecificMatch(matchers, target); }
@Test(expected = NullPointerException.class) public void givenTargetIsNull_whenGetMostSpecificMatch_thenThrowsNPE() { List<Matchable<String>> matchers = Collections.emptyList(); String target = null; utilities.getMostSpecificMatch(matchers, target); }
@Test public void givenMatchersAreEmpty_whenGetMostSpecificMatch_thenReturnsAbsent() { List<Matchable<String>> matchers = Collections.emptyList(); String target = "abc"; Optional<Matchable<String>> result = utilities.getMostSpecificMatch(matchers, target); assertFalse(result.isPresent()); }
@Test public void givenSingleNonMatchingMatcher_whenGetMostSpecificMatch_thenReturnsAbsent() { List<Matchable<String>> matchers = ImmutableList.of( NONMATCHING_MATCHABLE ); Optional<Matchable<String>> result = utilities.getMostSpecificMatch(matchers, TARGET); assertFalse(result.isPresent()); }
@Test public void givenSingleMatchingMatchable_whenGetMostSpecificMatch_thenReturnsMatchable() { List<Matchable<String>> matchers = ImmutableList.of( MATCHING_MATCHABLE ); Optional<Matchable<String>> result = utilities.getMostSpecificMatch(matchers, TARGET); assertTrue(result.isPresent()); assertThat(result.get(), equalTo(MATCHING_MATCHABLE)); }
@Test public void givenTwoMatchables_theFirstMatching__whenGetMostSpecificMatch_thenReturnsMatchable() { List<Matchable<String>> matchers = ImmutableList.of( MATCHING_MATCHABLE, NONMATCHING_MATCHABLE ); Optional<Matchable<String>> result = utilities.getMostSpecificMatch(matchers, TARGET); assertTrue(result.isPresent()); assertThat(result.get(), equalTo(MATCHING_MATCHABLE)); }
@Test public void givenTwoMatchables_theSecondMatching_whenGetMostSpecificMatch_thenReturnsMatchable() { List<Matchable<String>> matchers = ImmutableList.of( NONMATCHING_MATCHABLE, MATCHING_MATCHABLE ); Optional<Matchable<String>> result = utilities.getMostSpecificMatch(matchers, TARGET); assertTrue(result.isPresent()); assertThat(result.get(), equalTo(MATCHING_MATCHABLE)); }
@Test public void givenTwoMatchables_theFirstBeingMoreSpecific_whenGetMostSpecificMatch_thenReturnsMostSpecific() { List<Matchable<String>> matchers = ImmutableList.of( SPECIFIC_MATCHING_MATCHABLE, MATCHING_MATCHABLE ); Optional<Matchable<String>> result = utilities.getMostSpecificMatch(matchers, TARGET); assertTrue(result.isPresent()); assertThat(result.get(), equalTo(SPECIFIC_MATCHING_MATCHABLE)); }
@Test public void givenTwoMatchables_theSecondBeingMoreSpecific_whenGetMostSpecificMatch_thenReturnsMostSpecific() { List<Matchable<String>> matchers = ImmutableList.of( MATCHING_MATCHABLE, SPECIFIC_MATCHING_MATCHABLE ); Optional<Matchable<String>> result = utilities.getMostSpecificMatch(matchers, TARGET); assertTrue(result.isPresent()); assertThat(result.get(), equalTo(SPECIFIC_MATCHING_MATCHABLE)); }