DefaultServiceContext implements ServiceContext { @Override public <T> T getService(Class<T> serviceType, Supplier<T> supplier) { T service = (T)singletons.get(serviceType); if(service!=null){ return service; } Collection<T> services = loadServices(serviceType, null); if (services.isEmpty() && supplier!=null){ T instance = supplier.get(); if(instance instanceof ClassloaderAware){ ((ClassloaderAware)instance).init(this.classLoader); } register(serviceType, instance, true); return instance; } T t = getServiceWithHighestPriority(services, serviceType); if(t!=null) { this.singletons.put(serviceType, t); } return t; }  @Override T getService(Class<T> serviceType, Supplier<T> supplier); @Override T create(Class<T> serviceType, Supplier<T> supplier); @Override List<T> getServices(final Class<T> serviceType, Supplier<List<T>> supplier); @Override T register(Class<T> serviceType, T instance, boolean force); @Override List<T> register(Class<T> type, List<T> instances, boolean force); @Override void reset(); @Override ClassLoader getClassLoader(); @Override void init(ClassLoader classLoader);  }
@Test public void testGetService() { ConfigurationProviderSpi providerSpi = context.getService(ConfigurationProviderSpi.class); assertThat(providerSpi).isNotNull(); assertThat(providerSpi instanceof TestConfigurationProvider).isTrue(); }
@Test(expected = ConfigException.class) public void testGetService_multipleServicesWithoutPriority_shouldThrowConfigException() { context.getService(InvalidPriorityInterface.class); }
@Test public void testGetService_multipleService_shouldReturnServiceWithHighestPriority() { MultiImplsInterface service = context.getService(MultiImplsInterface.class); assertThat(service).isNotNull(); assertThat(service instanceof MultiImpl2).isTrue(); }
@Test public void testGetService_noImpl_shouldReturnEmptyOpional() { NoImplInterface service = context.getService(NoImplInterface.class); assertThat(service).isNull(); }