PluginServiceFactory { public <T> List<T> createServiceObjects(String... descriptorPaths) { try { ServiceEntry.initDefaultOrder(); TreeMap<ServiceEntry,T> serviceMap = new TreeMap<ServiceEntry,T>(); for (String descriptor : descriptorPaths) { readServiceDefinitions(serviceMap, descriptor); } ArrayList<T> ret = new ArrayList<T>(); for (T service : serviceMap.values()) { ret.add(service); } return ret; } finally { ServiceEntry.removeDefaultOrder(); } } PluginServiceFactory(C context, ClassLoader ... loaders); List<T> createServiceObjects(String... descriptorPaths); void addAdditionalClassLoader(ClassLoader classLoader);  }
@Test public void testOrder() { List<TestService> services = pluginServiceFactory.createServiceObjects("service/test-services-default", "service/test-services"); String[] orderExpected = new String[] { "three", "two", "five", "one"}; assertEquals(services.size(), 4); Iterator<TestService> it = services.iterator(); for (String val : orderExpected) { assertEquals(it.next().getName(),val); } }
@Test public void errorHandling() { try { pluginServiceFactory.createServiceObjects("service/error-services"); fail(); } catch (IllegalStateException exp) { assertTrue(exp.getMessage().matches(".*bla\\.blub\\.NotExist.*")); } }
@Test(expected = ClassCastException.class) public void classCastException() { List<String> services = pluginServiceFactory.createServiceObjects("service/test-services"); String bla = services.get(0); }