LoggingProviderLoader { public LoggingProvider load() { LoggingProvider providerFromSystemProperty = checkSystemProperty(); if (providerFromSystemProperty != null) { logger.info("Using {} from System Property {} for service {}", providerFromSystemProperty.getClass().getName(), LOGGING_PROVIDER_NAME_PROPERTY, LoggingProvider.class.getName()); return providerFromSystemProperty; } SortedMap<Integer, LoggingProvider> loggingProviders = new TreeMap<>(); loadServiceProviders() .forEach(provider -> loggingProviders.put(provider.getPriority(), provider)); if (!loggingProviders.isEmpty()) { LoggingProvider providerFromServiceLoader = loggingProviders.get(loggingProviders.lastKey()); logger.info("Using {} from ServiceLoader for service {}", providerFromServiceLoader.getClass().getName(), LoggingProvider.class.getName()); return providerFromServiceLoader; } logger.info("Using {} for service {}", SimpleLoggingProvider.class.getName(), LoggingProvider.class.getName()); return new SimpleLoggingProvider(); }  LoggingProvider load(); @VisibleForTesting
  static final String LOGGING_PROVIDER_NAME_PROPERTY; }
@Test public void createProviderAgent_usesSystemPropertySetTo_NullProviderAgent() { System.setProperty(LOGGING_PROVIDER_NAME_PROPERTY, SimpleLoggingProvider.class.getName()); LoggingProvider value = loggingProviderLoader.load(); assertThat(value).isInstanceOf(SimpleLoggingProvider.class); }
@Test public void createProviderAgent_usesSystemPropertySetTo_SimpleProviderAgent() { System.setProperty(LOGGING_PROVIDER_NAME_PROPERTY, TestLoggingProvider.class.getName()); LoggingProvider value = loggingProviderLoader.load(); assertThat(value).isInstanceOf(TestLoggingProvider.class); }
@Test public void createProviderAgent_usesNullProviderAgent_whenClassNotFoundException() { System.setProperty(LOGGING_PROVIDER_NAME_PROPERTY, TestLoggingProvider.class.getSimpleName()); LoggingProvider value = loggingProviderLoader.load(); assertThat(value).isInstanceOf(SimpleLoggingProvider.class); }
@Test public void createProviderAgent_usesNullProviderAgent_whenClassCastException() { System.setProperty(LOGGING_PROVIDER_NAME_PROPERTY, NotProviderAgent.class.getName()); LoggingProvider value = loggingProviderLoader.load(); assertThat(value).isInstanceOf(SimpleLoggingProvider.class); }
@Test public void getLoggingProviderReturnsSimpleLoggingProviderByDefault() { LoggingProvider loggingProvider = new LoggingProviderLoader().load(); assertThat(loggingProvider).isInstanceOf(SimpleLoggingProvider.class); }