ConfigurationProperty property = getProperty(parameterList, st); addPropertyValues(propertyMap, st, property);
private void addPropertyValues(Map<ConfigurationProperty, List<String>> propertyMap, StringTokenizer st, ConfigurationProperty property) throws InterpreterPropertyException { List<String> l = getOrCreatePropertyList(propertyMap, property); if ( ! st.hasMoreTokens() ) { if (isBooleanSwitchProperty(property)) { l.add("true"); } else { throw new InterpreterPropertyException( String.format("No value was given for switch -%s (-%s), and Chorus cannot provide a default", property.getSwitchShortName(), property.getSwitchName() ) ); } } else { while(st.hasMoreTokens()) { l.add(st.nextToken()); } } }
@Test public void testArgWithSingleValue() throws InterpreterPropertyException { Map<ConfigurationProperty, List<String>> parsedArgs = new CommandLineParser(TestConfigProperty.getAll()).parseProperties(propertyMap, TEST_ARGS); List<String> fValues = parsedArgs.get(TestConfigProperty.FEATURE_PATHS); Assert.assertEquals("incorrect nunber of -f args found", 1, fValues.size()); }
/** * 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 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; } }
@Test public void testArgWithNoValues() throws InterpreterPropertyException { propertyMap = new HashMap<>(); Map<ConfigurationProperty, List<String>> parsedArgs = new CommandLineParser(TestConfigProperty.getAll()).parseProperties(propertyMap, TEST_ARGS); Assert.assertTrue("-showErrors flag not found", parsedArgs.containsKey(TestConfigProperty.SHOW_ERRORS)); }
@Test public void testValueOfWithSingleArg() throws InterpreterPropertyException { Map<ConfigurationProperty, List<String>> parsedArgs = new CommandLineParser(TestConfigProperty.getAll()).parseProperties(propertyMap, TEST_ARGS); List<String> fValues = parsedArgs.get(TestConfigProperty.FEATURE_PATHS); Assert.assertEquals("incorrect value for -f arg found", "file1", fValues.get(0)); }
@Test public void testArgWithMultipleValues() throws InterpreterPropertyException { Map<ConfigurationProperty, List<String>> parsedArgs = new CommandLineParser(TestConfigProperty.getAll()).parseProperties(propertyMap, TEST_ARGS); Assert.assertTrue("-h flag not found", parsedArgs.containsKey(TestConfigProperty.HANDLER_PACKAGES)); Assert.assertEquals("wrong number of values found for -h flag", 2, parsedArgs.get(TestConfigProperty.HANDLER_PACKAGES).size()); }
@Test public void testPathsWithHyphens() throws InterpreterPropertyException { Map<ConfigurationProperty, List<String>> parsedArgs = new CommandLineParser(TestConfigProperty.getAll()).parseProperties(propertyMap, TEST_PATH_ARG_WITH_INLINE_HYPHEN); Assert.assertTrue("-h flag not found", parsedArgs.containsKey(TestConfigProperty.HANDLER_PACKAGES)); Assert.assertEquals("Not equal to handlers.package-name-with-hyphens-", "handlers.package-name-with-hyphens-", parsedArgs.get(TestConfigProperty.HANDLER_PACKAGES).get(0)); Assert.assertTrue("-f flag not found", parsedArgs.containsKey(TestConfigProperty.FEATURE_PATHS)); Assert.assertEquals("Not equal to test.with-hyphen.dir", "test.with-hyphen.dir", parsedArgs.get(TestConfigProperty.FEATURE_PATHS).get(0)); }