DefaultServiceContext implements ServiceContext { @Override public <T> List<T> getServices(final Class<T> serviceType, Supplier<List<T>> supplier) { @SuppressWarnings("unchecked") List<T> found = (List<T>) servicesLoaded.get(serviceType); if (found != null) { return found; } List<T> services = loadServices(serviceType, supplier); @SuppressWarnings("unchecked") final List<T> previousServices = List.class.cast(servicesLoaded.putIfAbsent(serviceType, (List<Object>) services)); return previousServices != null ? previousServices : services; }  @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 testGetServices_shouldReturnServices() { { Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class); assertThat(services).isNotNull(); assertThat(services).hasSize(2); for (InvalidPriorityInterface service : services) { assertThat(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2).isTrue(); } } { List<MultiImplsInterface> services = context.getServices(MultiImplsInterface.class); assertThat(services).isNotNull(); assertThat(services).hasSize(3); assertThat(services.get(0) instanceof MultiImpl2).isTrue(); assertThat(services.get(1) instanceof MultiImpl1).isTrue(); assertThat(services.get(2) instanceof MultiImpl3).isTrue(); } }
@Test public void testGetServices_redundantAccessToServices() { for(int i=0;i<10;i++){ Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class); assertThat(services).isNotNull(); assertThat(services).hasSize(2); for (InvalidPriorityInterface service : services) { assertThat(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2).isTrue(); } } }
@Test public void testGetServices_noImpl_shouldReturnEmptyList() { Collection<NoImplInterface> services = context.getServices(NoImplInterface.class); assertThat(services).isNotNull(); assertThat(services).isEmpty(); }