OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault implements
      Function<Iterable<ReferenceType>, ReferenceType> { @Override public ReferenceType apply(Iterable<ReferenceType> referenceTypes) { checkNotNull(referenceTypes, "referenceTypes"); checkArgument(Iterables.size(referenceTypes) > 0, "No referenceTypes corresponding to configuration key %s present", configurationKey); if (Iterables.size(referenceTypes) == 1) return Iterables.getLast(referenceTypes); String namingPattern = valueOfConfigurationKeyOrNull.apply(configurationKey); if (namingPattern != null) { return findReferenceTypeWithNameMatchingPattern(referenceTypes, namingPattern); } else { return defaultReferenceType(referenceTypes); } } OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(
         ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull, String configurationKey,
         Predicate<ReferenceType> defaultSelector); @Override ReferenceType apply(Iterable<ReferenceType> referenceTypes); ReferenceType defaultReferenceType(Iterable<ReferenceType> referenceTypes); ReferenceType findReferenceTypeWithNameMatchingPattern(Iterable<ReferenceType> referenceTypes,
         String namingPattern);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testIllegalArgumentWhenResourcesEmpty() { new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull, "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableSet.<ReferenceType> of()); }
@Test public void testReturnsOnlyResource() { ReferenceType reference = createMock(ReferenceType.class); assertEquals(new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull, "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableSet.<ReferenceType> of(reference)), reference); }
@Test public void testReturnsFirstResourceWhenConfigurationUnspecified() { ReferenceType reference1 = createMock(ReferenceType.class); ReferenceType reference2 = createMock(ReferenceType.class); assertEquals(new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull, "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableList.<ReferenceType> of(reference1, reference2)), reference1); }
@Test public void testReturnsResourceMatchingDefaultPredicateWhenConfigurationUnspecified() { ReferenceType reference1 = createMock(ReferenceType.class); ReferenceType reference2 = createMock(ReferenceType.class); assertEquals(new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull, "foo", Predicates.equalTo(reference2)).apply(ImmutableList.<ReferenceType> of(reference1, reference2)), reference2); }
@Test public void testReturnsResourceWithNameMatchingConfigurationKey() { ReferenceType reference1 = new ReferenceTypeImpl("travis tritt", null, null); ReferenceType reference2 = new ReferenceTypeImpl("hail mary", null, null); assertEquals( new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(Guice.createInjector( new AbstractModule() { protected void configure() { bindConstant().annotatedWith(Names.named("foo")).to(".*mary.*"); } }).getInstance(ValueOfConfigurationKeyOrNull.class), "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableList .<ReferenceType> of(reference1, reference2)), reference2); }
@Test(expectedExceptions = NoSuchElementException.class) public void testThrowsNoSuchElementWhenNoneMatchConfigurationKey() { ReferenceType reference1 = new ReferenceTypeImpl("travis tritt", null, null); ReferenceType reference2 = new ReferenceTypeImpl("hail mary", null, null); new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(Guice.createInjector(new AbstractModule() { protected void configure() { bindConstant().annotatedWith(Names.named("foo")).to(".*happy.*"); } }).getInstance(ValueOfConfigurationKeyOrNull.class), "foo", Predicates.<ReferenceType> alwaysTrue()) .apply(ImmutableList.<ReferenceType> of(reference1, reference2)); }