DefaultConnectionFactoryCreator implements ConnectionFactoryCreator { @Override public ConnectionFactory create(Map<String, String> properties) { String className = properties.remove("type"); Assert.notNull(className, "Missing connection factory type information"); try { Class<?> type = Class.forName(className); if (!(ConnectionFactory.class.isAssignableFrom(type))) { throw new IllegalStateException(String.format("Unsupported type information %s - must be of type %s", type.getName(), ConnectionFactory.class.getName())); } Collection<String> constructorArgs = properties.values(); Constructor<? extends ConnectionFactory> constructor = Arrays.stream(type.getConstructors()) .filter(c -> c.getParameterCount() == constructorArgs.size() && Arrays.stream(c.getParameterTypes()).allMatch(paramType -> paramType.equals(String.class))) .map(c -> (Constructor<? extends ConnectionFactory>) c) .findFirst() .orElseThrow(() -> new IllegalStateException("Unable to find matching constructor on connection factory")); return constructor.newInstance(constructorArgs.toArray(new Object[0])); } catch (InstantiationException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) { throw new CitrusRuntimeException(e); } }  @Override ConnectionFactory create(Map<String, String> properties); @Override boolean supports(Class<?> type);  }
@Test public void shouldCreate() { Map<String, String> connectionSettings = new HashMap<>(); connectionSettings.put("type", ActiveMQConnectionFactory.class.getName()); ConnectionFactory connectionFactory = connectionFactoryCreator.create(connectionSettings); Assertions.assertThat(ActiveMQConnectionFactory.class).isEqualTo(connectionFactory.getClass()); Assertions.assertThat(ActiveMQConnectionFactory.DEFAULT_BROKER_URL).isEqualTo(((ActiveMQConnectionFactory)connectionFactory).getBrokerURL()); }
@Test public void shouldCreateWithConstructorArgs() { Map<String, String> connectionSettings = new LinkedHashMap<>(); connectionSettings.put("type", ActiveMQConnectionFactory.class.getName()); connectionSettings.put("username", "foo"); connectionSettings.put("password", "secret"); connectionSettings.put("brokerUrl", "typ: ConnectionFactory connectionFactory = connectionFactoryCreator.create(connectionSettings); Assertions.assertThat(ActiveMQConnectionFactory.class).isEqualTo(connectionFactory.getClass()); Assertions.assertThat("typ: Assertions.assertThat("foo").isEqualTo(((ActiveMQConnectionFactory)connectionFactory).getUserName()); Assertions.assertThat("secret").isEqualTo(((ActiveMQConnectionFactory)connectionFactory).getPassword()); }
@Test(expected = CitrusRuntimeException.class) public void shouldHandleUnsupportedTypeInformation() { Map<String, String> connectionSettings = new LinkedHashMap<>(); connectionSettings.put("type", "org.unknown.Type"); connectionFactoryCreator.create(connectionSettings); }
@Test(expected = IllegalArgumentException.class) public void shouldHandleMissingTypeInformation() { connectionFactoryCreator.create(Collections.emptyMap()); }