private WebSocketsConfig getWebSocketsConfig(String configName, Properties webSocketsProperties) { WebSocketsConfigBean config; try { config = new ConfigBuilder().buildConfig(WebSocketsConfigBean.class, webSocketsProperties); } catch (ConfigBuilderException e) { throw new ChorusException(String.format("Invalid config '%s'. %s", configName, e.getMessage())); } config.setConfigName(configName); return config; }
public <C> C buildConfig(Class<C> configClass, Properties properties) throws ConfigBuilderException { Map<String, HandlerConfigProperty> configPropertiesByName = configPropertyParser.getConfigPropertiesByName(configClass); C configInstance; try { configInstance = configClass.newInstance(); } catch (Exception e) { throw new ConfigBuilderException("Failed to instantiate config class " + configClass.getSimpleName() + " - " + e.getClass().getSimpleName(), e); } //iterate sorted by field name for consistent/deterministic behaviour List<String> props = configPropertiesByName.keySet().stream().sorted().collect(Collectors.toList()); for ( String configPropertyName : props) { log.debug("Processing config property " + configPropertyName); HandlerConfigPropertyImpl configProperty = (HandlerConfigPropertyImpl)configPropertiesByName.get(configPropertyName); String propertyValue = properties.getProperty(configPropertyName); setValueForConfigProperty(configInstance, configPropertyName, configProperty, propertyValue); } warnOnUnusedProperties(properties, configPropertiesByName); runClassLevelValidation(configClass, configInstance); return configInstance; }
@Test public void testUnvalidatedConfigSettersCanAcceptEmptyStringAsValidPropertyValue() throws ConfigBuilderException { Properties p = new Properties(); p.setProperty("stringProperty", ""); ConfigClassWithUnvalidatedStringProperty c = configBuilder.buildConfig(ConfigClassWithUnvalidatedStringProperty.class, p); assertEquals("", c.stringProperty); }
validate(configPropertyName, propertyValue, configProperty.getValidationPattern()); convertedValue = applyConverterFunction(configPropertyName, configProperty, propertyValue);
@Test public void testConfigPropertiesWithEnumField() throws ConfigBuilderException { Properties p = new Properties(); p.setProperty("enumField", "feature"); p.setProperty("enumFieldCaseInsensitive", "ScEnArIo"); ConfigClassWithEnumTypes c = configBuilder.buildConfig(ConfigClassWithEnumTypes.class, p); assertEquals(Scope.FEATURE, c.scope); assertEquals(Scope.SCENARIO, c.scopeCaseInsensitive); }
private SeleniumConfig getSeleniumConfig(String configName, Properties remotingProperties) { SeleniumConfigBean config; try { config = new ConfigBuilder().buildConfig(SeleniumConfigBean.class, remotingProperties); } catch (ConfigBuilderException e) { throw new ChorusException(String.format("Invalid config '%s'. %s", configName, e.getMessage())); } config.setConfigName(configName); return config; }
@Test public void testAnEnumValueWhichCantBeMappedThrowsException() { Properties p = new Properties(); p.setProperty("enumField", "rgioergergerg"); try { ConfigClassWithEnumTypes c = configBuilder.buildConfig(ConfigClassWithEnumTypes.class, p); fail("Should throw exception"); } catch (Exception e) { assertEquals("Property enumField value 'rgioergergerg' does not match pattern '(?i)SCENARIO|FEATURE'", e.getMessage()); } }
private RemotingManagerConfig buildRemotingConfig(String configName, Properties remotingProperties) { RemotingConfig config; try { config = new ConfigBuilder().buildConfig(RemotingConfig.class, remotingProperties); } catch (ConfigBuilderException e) { throw new ChorusException(String.format("Invalid config '%s'. %s", configName, e.getMessage())); } config.setConfigName(configName); return config; }
@Test public void testValidationRulesAreAppliedToMyProvidedValue() { Properties p = new Properties(); p.setProperty("stringProperty", "This value will not validate"); try { configBuilder.buildConfig(ConfigClassWithSimpleProperty.class, p); fail("Should not validate"); } catch (Exception e) { assertEquals("Property stringProperty value 'This value will not validate' does not match pattern 'My.*'", e.getMessage()); } }
private ProcessConfig buildProcessesConfig(String configName, Properties processProperties) { ProcessConfig config; try { config = new ConfigBuilder().buildConfig(ProcessConfig.class, processProperties); } catch (ConfigBuilderException e) { throw new ChorusException(String.format("Invalid config '%s'. %s", configName, e.getMessage())); } config.setConfigName(configName); return config; }
@Test public void testICanBuildABeanProvidingASimpleStringProperty() throws ConfigBuilderException { Properties p = new Properties(); p.setProperty("stringProperty", "My Provided Value"); ConfigClassWithSimpleProperty c = configBuilder.buildConfig(ConfigClassWithSimpleProperty.class, p); assertEquals("My Provided Value", c.getStringProperty()); }
private SqlConfig getSqlConfig(String configName, Properties processProperties) { SqlConfigBean config; try { config = new ConfigBuilder().buildConfig(SqlConfigBean.class, processProperties); } catch (ConfigBuilderException e) { throw new ChorusException(String.format("Invalid config '%s'. %s", configName, e.getMessage())); } config.setConfigName(configName); return config; }
@Test public void testAMandatoryPropertyMustBeProvidedIfADefaultDoesNotExist() { Properties p = new Properties(); try { configBuilder.buildConfig(ConfigClassPropertyWithNoDefault.class, p); fail("Should not validate"); } catch (Exception e) { assertEquals("Property stringProperty is mandatory but no value was provided", e.getMessage()); } }
properties.put(ProcessConfig.MAINCLASS_PROPERTY, mainClass); ProcessConfig config = new ConfigBuilder().buildConfig(ProcessConfig.class, properties); JavaProcessCommandLineBuilder javaProcessCommandLineBuilder = new JavaProcessCommandLineBuilder( new File(System.getProperty("user.dir")),
@Test public void testADefaultValueIsUsedIfIDoNotProvideAValue() throws ConfigBuilderException { Properties p = new Properties(); ConfigClassWithSimpleProperty c = configBuilder.buildConfig(ConfigClassWithSimpleProperty.class, p); assertEquals("My Default Value", c.getStringProperty()); }
@Test public void testConfigProperiesWithPrimitiveSetters() throws ConfigBuilderException { Properties p = new Properties(); p.setProperty("intProperty", "123"); p.setProperty("floatProperty", "234.5"); p.setProperty("longProperty", "345"); p.setProperty("doubleProperty", "456.7"); p.setProperty("booleanProperty", "true"); p.setProperty("charProperty", "X"); p.setProperty("shortProperty", "123"); ConfigClassPropertyWithPrimitivesSetters c = configBuilder.buildConfig(ConfigClassPropertyWithPrimitivesSetters.class, p); assertEquals( 123, c.intProperty); assertEquals( 234.5f, c.floatProperty, 0); assertEquals( 345, c.longProperty); assertEquals(456.7d, c.doubleProperty, 0); assertEquals( true, c.booleanProperty); assertEquals( 'X', c.charProperty); assertEquals( 123, c.shortProperty); }
@Test public void testConfigProperiesWithConversions() throws ConfigBuilderException { Properties p = new Properties(); p.setProperty("intProperty", "123"); p.setProperty("floatProperty", "234.5"); p.setProperty("longProperty", "345"); p.setProperty("doubleProperty", "456.7"); p.setProperty("booleanProperty", "true"); p.setProperty("charProperty", "X"); p.setProperty("shortProperty", "123"); ConfigClassPropertyWithConversions c = configBuilder.buildConfig(ConfigClassPropertyWithConversions.class, p); assertEquals( 123, c.intProperty); assertEquals( 234.5f, c.floatProperty, 0); assertEquals( 345, c.longProperty); assertEquals(456.7d, c.doubleProperty, 0); assertEquals( true, c.booleanProperty); assertEquals( 'X', c.charProperty); assertEquals( 123, c.shortProperty); }