MatcherUtilsImpl implements MatcherUtils { @Override public <T> Optional<Double> getMatchSpecificity(@Nonnull Matcher<T> matcher, @Nonnull T target) { checkNotNull(matcher, "matcher is null"); checkNotNull(target, "target is null"); if (matcher.matches(target)) { return of(matcher.getSpecificity()); } else { return absent(); } }  @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 public void givenPatternNotMatches_whenGetMatchSpecificity_thenReturnsAbsent() { Matcher<String> matcher = newPatternMatcher("yahoo"); String agent = "googlebot"; Optional<Double> specificity = utilities.getMatchSpecificity(matcher, agent); assertThat(specificity, equalTo(Optional.<Double>absent())); }
@Test public void givenPatternIsEmpty_whenGetMatchSpecificity_thenReturnsZero() { Matcher<String> matcher = newPatternMatcher(""); String agent = "googlebot"; Optional<Double> specificity = utilities.getMatchSpecificity(matcher, agent); assertThat(specificity.get(), equalTo(0.0)); }
@Test public void givenPatternAndAgentAreEmpty_whenGetMatchSpecificity_thenReturnsZero() { Matcher<String> matcher = newPatternMatcher(""); String agent = ""; Optional<Double> specificity = utilities.getMatchSpecificity(matcher, agent); assertThat(specificity.get(), equalTo(0.0)); }
@Test public void givenWildcardPattern_andAgentIsEmpty_whenGetMatchSpecificity_thenReturnsZero() { Matcher<String> matcher = newPatternMatcher("*"); String agent = ""; Optional<Double> specificity = utilities.getMatchSpecificity(matcher, agent); assertThat(specificity.get(), equalTo(0.0)); }
@Test(expected = NullPointerException.class) public void givenMatcherIsNull_whenGetMatchSpecificity_thenThrowsNPE() { Matcher<String> matcher = null; String agent = "googlebot"; utilities.getMatchSpecificity(matcher, agent); }
@Test(expected = NullPointerException.class) public void givenAgentIsNull_whenGetMatchSpecificity_thenThrowsNPE() { Matcher<String> matcher = newPatternMatcher("*"); String agent = null; utilities.getMatchSpecificity(matcher, agent); }
@Test public void givenPatternIsWildcard_whenGetMatchSpecificity_thenReturnsZero() { Matcher<String> matcher = newPatternMatcher("*"); String agent = "googlebot"; Optional<Double> specificity = utilities.getMatchSpecificity(matcher, agent); assertThat(specificity.get(), equalTo(0.0)); }
@Test public void givenPatternMatches_whenGetMatchSpecificity_thenReturnsMatcherSpecificity() { Matcher<String> matcher = newPatternMatcher("google"); String agent = "googlebot"; Optional<Double> specificity = utilities.getMatchSpecificity(matcher, agent); assertThat(specificity.get(), equalTo(matcher.getSpecificity())); }
@Test public void givenPatternMatchesIgnoringCase_whenGetMatchSpecificity_thenReturnsMatcherSpecificity() { Matcher<String> matcher = newPatternMatcher("gOoGlE"); String agent = "googlebot"; Optional<Double> specificity = utilities.getMatchSpecificity(matcher, agent); assertThat(specificity.get(), equalTo(matcher.getSpecificity())); }