ServiceLoader { public T create(final String name, final Object... arguments) { if (name.indexOf('.') == -1) { String expectingClassName = toSimpleClassName(name); for (String className : classes) { int split = className.lastIndexOf('.'); String simpleClassName = split == -1 ? className : className.substring(split + 1); if (expectingClassName.equals(simpleClassName)) { return createInstance(className, arguments); } } InternalLogger.log(Level.ERROR, "Service implementation '" + name + "' not found"); return null; } else { return createInstance(name, arguments); } } ServiceLoader(final Class<? extends T> service, final Class<?>... argumentTypes); T create(final String name, final Object... arguments); List<T> createList(final String list); Collection<T> createAll(final Object... arguments); String toSimpleClassName(final String name);  }
@Test public void getServiceByAcronym() { ServiceLoader<?> loader = new ServiceLoader<>(List.class); assertThat(loader.create("array")).isInstanceOf(ArrayList.class); assertThat(loader.create("linked")).isInstanceOf(LinkedList.class); }
@Test public void getServiceByClassName() { ServiceLoader<?> loader = new ServiceLoader<>(List.class); assertThat(loader.create(ArrayList.class.getName())).isInstanceOf(ArrayList.class); assertThat(loader.create(LinkedList.class.getName())).isInstanceOf(LinkedList.class); }
@Test public void invalidAcronym() { ServiceLoader<?> loader = new ServiceLoader<>(List.class); assertThat(loader.create("test")).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce("test"); }
@Test public void nonExistentClassName() { ServiceLoader<?> loader = new ServiceLoader<>(List.class); assertThat(loader.create("my.package.Test")).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce("my.package.Test"); }
@Test public void notImplementingInterface() { ServiceLoader<?> loader = new ServiceLoader<>(List.class); assertThat(loader.create(HashSet.class.getName())).isNull(); assertThat(systemStream.consumeErrorOutput()) .contains("ERROR") .containsOnlyOnce(HashSet.class.getName()) .containsOnlyOnce(List.class.getName()); }
@Test public void abstractServiceClass() { String className = AbstractListWithPublicConstructor.class.getName(); ServiceLoader<?> loader = new ServiceLoader<>(List.class); assertThat(loader.create(className)).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce(className); }
@Test public void withoutMatchingConstructor() { ServiceLoader<?> loader = new ServiceLoader<>(List.class, int.class); assertThat(loader.create(LinkedList.class.getName(), 1)).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce(LinkedList.class.getName()); }
@Test public void invisibleConstructor() { ServiceLoader<?> loader = new ServiceLoader<>(List.class); assertThat(loader.create(Collections.EMPTY_LIST.getClass().getName())).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce(Collections.EMPTY_LIST.getClass().getName()); }
@Test public void wrongNumberOfArguments() { ServiceLoader<?> loader = new ServiceLoader<>(List.class, int.class); assertThat(loader.create(ArrayList.class.getName())).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce(ArrayList.class.getName()); }
@Test public void invalidArgumentType() { ServiceLoader<?> loader = new ServiceLoader<>(List.class, int.class); assertThat(loader.create(ArrayList.class.getName(), "test")).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce(ArrayList.class.getName()); }
@Test public void invalidArgumentValue() { ServiceLoader<?> loader = new ServiceLoader<>(List.class, int.class); assertThat(loader.create(ArrayList.class.getName(), -1)).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce(ArrayList.class.getName()); }