protected ConfigurationProperty getProperty(String s) { ConfigurationProperty result = null; for (ConfigurationProperty p : properties) { if ( p.matchesSwitch(s)) { result = p; break; } } return result; } }
/** * Create a configuration using process arguments, System Properties and defaults */ public ConfigReader(List<ConfigurationProperty> properties, String[] args) { this.properties = properties; this.args = args; //where a property is in PropertySourceMode.OVERRIDE the ordering of sources here is important //sys props are last since it is useful to be able to use a sys prop to override command line //parameters for test suites which run as part of a component build which is checked in to source control - //otherwise it is necessary to commit changes to files to achieve something simple like increasing logging //to debug level. Continuous integration tools such as team city let you set a sys prop easily to do this. propertySources = new ConfigSource[] { new DefaultsConfigSource(properties), new CommandLineParser(properties), new SystemPropertyParser(properties) }; }
private void checkIfMandatory(Map<ConfigurationProperty, List<String>> results, ConfigurationProperty p) throws InterpreterPropertyException { if ( p.isMandatory() && ! results.containsKey(p)) { throw new InterpreterPropertyException( "Mandatory property " + p + " was not set. " + "You can set this property with the -" + p.getSwitchName() + " switch, " + "the -" + p.getSwitchShortName() + " switch or the " + p.getSystemProperty() + " system property" ); } } }
@Test public void testBooleanSwitchUsingShortName() throws InterpreterPropertyException { String[] switches = new String[] { "-f", "./features", "-h", "org.chorusbdd.chorus", "-d" }; ConfigReader c = new ConfigReader(TestConfigProperty.getAll(), switches); c.readConfiguration(); assertTrue(c.isTrue(TestConfigProperty.DRY_RUN)); assertTrue(c.isSet(TestConfigProperty.DRY_RUN)); }
@Test public void mandatoryPropertyMustBeSet() { System.clearProperty(TestConfigProperty.FEATURE_PATHS.getSystemProperty()); //in case set String[] switches = new String[] { "-d" }; ConfigReader c = new ConfigReader(TestConfigProperty.getAll(), switches); try { c.readConfiguration(); } catch (InterpreterPropertyException e) { assertTrue(e.getMessage().contains("Mandatory property featurePaths was not set")); return; } fail("Must require mandatory -f property value"); }
public Map<ConfigurationProperty, List<String>> parseProperties(Map<ConfigurationProperty, List<String>> propertyMap, String... args) throws InterpreterPropertyException { for ( ConfigurationProperty p : getProperties()) { if ( p.hasDefaults()) { List<String> properties = getOrCreatePropertyList(propertyMap, p); Collections.addAll(properties, p.getDefaults()); } } return propertyMap; } }
private void checkValueCount(ConfigurationProperty p, List<String> values) throws InterpreterPropertyException { if ( values.size() < p.getMinValueCount()) { throw new InterpreterPropertyException("At least " + p.getMinValueCount() + " value(s) must be supplied for the property " + p); } else if ( values.size() > p.getMaxValueCount()) { throw new InterpreterPropertyException("At most " + p.getMaxValueCount() + " value(s) must be supplied for the property " + p); } }
public static ConfigurationProperty getConfigPropertyForSysProp(String systemProperty) { ConfigurationProperty result = null; for ( ConfigurationProperty p : values()) { if ( p.getSystemProperty().equals(systemProperty)) { result = p; break; } } return result; }
private ConfigurationProperty getProperty(String parameterList, StringTokenizer st) throws InterpreterPropertyException { String switchProperty = st.nextToken(); ConfigurationProperty property = getProperty(switchProperty); if (property == null ) { throw new InterpreterPropertyException("Unsupported parameter " + parameterList); } return property; } }
public int getMinValueCount() { return delegate.getMinValueCount(); }
public String getValidatingExpression() { return delegate.getValidatingExpression(); }
public PropertySourceMode getPropertySourceMode() { return delegate.getPropertySourceMode(); } }
public String getDescription() { return delegate.getDescription(); }
public boolean hasDefaults() { return getDefaults().length > 0; }
public String toString() { return getSwitchName(); }
@Test public void testDefaultValueGetsSetIfAvailable() throws InterpreterPropertyException { String[] switches = new String[] { "-f", "./features", "-h", "org.chorusbdd.chorus" }; ConfigReader c = new ConfigReader(TestConfigProperty.getAll(), switches); c.readConfiguration(); assertTrue(! c.isTrue(TestConfigProperty.DRY_RUN)); assertTrue(c.isSet(TestConfigProperty.DRY_RUN)); }
@Test public void testBooleanSwitchWithValue() throws InterpreterPropertyException { String[] switches = new String[] { "-f", "./features", "-h", "org.chorusbdd.chorus", "-dryrun", "true" }; ConfigReader c = new ConfigReader(TestConfigProperty.getAll(), switches); c.readConfiguration(); assertTrue(c.isTrue(TestConfigProperty.DRY_RUN)); assertTrue(c.isSet(TestConfigProperty.DRY_RUN)); }
@Test public void testBooleanSwitchCanBeSetFalse() throws InterpreterPropertyException { String[] switches = new String[] { "-f", "./features", "-h", "org.chorusbdd.chorus", "-dryrun", "false" }; ConfigReader c = new ConfigReader(TestConfigProperty.getAll(), switches); c.readConfiguration(); assertTrue(! c.isTrue(TestConfigProperty.DRY_RUN)); assertTrue(c.isSet(TestConfigProperty.DRY_RUN)); }
@Test public void testBooleanSwitchWithoutValue() throws InterpreterPropertyException { String[] switches = new String[] { "-f", "./features", "-h", "org.chorusbdd.chorus", "-dryrun" }; ConfigReader c = new ConfigReader(TestConfigProperty.getAll(), switches); c.readConfiguration(); assertTrue(c.isTrue(TestConfigProperty.DRY_RUN)); assertTrue(c.isSet(TestConfigProperty.DRY_RUN)); }
@Test public void testADefaultValueDoesNotGetSetIfNoDefaultDefined() throws InterpreterPropertyException { String[] switches = new String[] { "-f", "./features", "-h", "org.chorusbdd.chorus" }; ConfigReader c = new ConfigReader(TestConfigProperty.getAll(), switches); c.readConfiguration(); assertTrue(! c.isSet(TestConfigProperty.TAG_EXPRESSION)); }