ServiceFacadeUtils { public static <T> T instantiate(Map<String, String> parameters) throws ServiceFacadeException { String serviceFactoryClassName = parameters.get(IMPORT_FACADE_FACTORY_CLASS); if (StringUtils.isBlank(serviceFactoryClassName)) { throw new ServiceFacadeException("unknown service facade factory, no " + IMPORT_FACADE_FACTORY_CLASS + " parameter provided!"); } try { Class<?> clazz = Class.forName(serviceFactoryClassName); Constructor<?> constructor = clazz.getConstructor(); @SuppressWarnings("unchecked") ServiceFacadeFactory<T> serviceFactory = (ServiceFacadeFactory<T>) constructor.newInstance(); return serviceFactory.instantiate(parameters); } catch (Exception e) { throw new ServiceFacadeException("exception occurred while instantiating service by facade factory: " + IMPORT_FACADE_FACTORY_CLASS, e); } } private  ServiceFacadeUtils(); static T instantiate(Map<String, String> parameters); static T instantiate(Configuration config);  }
@Test(expected = ServiceFacadeException.class) public void instantiate_no_factory_classname() throws Exception { Map<String, String> parameters = Maps.newHashMap(); ServiceFacadeUtils.instantiate(parameters); }
@Test(expected = ServiceFacadeException.class) public void instantiate_invalid_factory_classname() throws Exception { Map<String, String> parameters = Maps.newHashMap(); parameters.put(IMPORT_FACADE_FACTORY_CLASS, "non.existing.Factory"); ServiceFacadeUtils.instantiate(parameters); }
@Test(expected = ServiceFacadeException.class) public void instantiate_invalid_factory() throws Exception { Map<String, String> parameters = Maps.newHashMap(); parameters.put(IMPORT_FACADE_FACTORY_CLASS, "java.util.Map"); ServiceFacadeUtils.instantiate(parameters); }
@Test(expected = ServiceFacadeException.class) public void instantiate_factory_without_noarg_constructor() throws Exception { Map<String, String> parameters = Maps.newHashMap(); parameters.put(IMPORT_FACADE_FACTORY_CLASS, "eu.dnetlib.iis.wf.importer.facade.ServiceFacadeUtilsTest$FactoryWithoutNoArgConstructor"); ServiceFacadeUtils.instantiate(parameters); }
@Test public void instantiate() throws Exception { Map<String, String> parameters = Maps.newHashMap(); parameters.put(IMPORT_FACADE_FACTORY_CLASS, "eu.dnetlib.iis.wf.importer.facade.ServiceFacadeUtilsTest$StringFactory"); String instantiated = ServiceFacadeUtils.instantiate(parameters); assertEquals(instantiated, StringFactory.class.getName()); }