ProviderFinder { public List<T> find() { final List<T> providers = new ArrayList<T>(); try { final Iterator<T> iterator = lookupProviders(type); for (; iterator.hasNext();) { final T provider = iterator.next(); LOG.debug("found provider: {}", provider); providers.add(provider); } collectConfigurableProviders(providers); } catch (final Exception e) { LOG.error("Failed to discover providers using ServiceRegistry. Cannot continue...", e); throw WroRuntimeException.wrap(e); } Collections.sort(providers, Ordered.DESCENDING_COMPARATOR); LOG.debug("found providers: {}", providers); return providers; } ProviderFinder(final Class<T> type); static ProviderFinder<T> of(final Class<T> type); List<T> find();  }
@Test public void shouldNotFindProviderWhenNoneIsAvailable() { victim = new ProviderFinder<ProcessorProvider>(ProcessorProvider.class) { @Override <F> Iterator<F> lookupProviders(final Class<F> clazz) { return new ArrayList<F>().iterator(); } }; assertTrue(victim.find().isEmpty()); }
@Test(expected = WroRuntimeException.class) public void cannotFindAnyProviderWhenLookupFails() { victim = new ProviderFinder<ProcessorProvider>(ProcessorProvider.class) { @Override <F> Iterator<F> lookupProviders(final Class<F> clazz) { throw new IllegalStateException("BOOM!"); } }; victim.find(); }
@Test public void shouldOrderProviders() { final OrderedProvider lowest = new OrderedProvider(Ordered.LOWEST); final Object defaultPriority = new Object(); final OrderedProvider highest = new OrderedProvider(Ordered.HIGHEST); victim = new ProviderFinder<Object>(Object.class) { @Override @SuppressWarnings("unchecked") <P> Iterator<P> lookupProviders(final Class<P> providerClass) { if (providerClass == Object.class) { return (Iterator<P>) Arrays.asList(defaultPriority, highest, lowest).iterator(); } return Collections.<P> emptyList().iterator(); } }; assertEquals(Arrays.asList(highest, defaultPriority, lowest), victim.find()); }