CommandLineParser extends AbstractConfigSource { public Map<ExecutionProperty, List<String>> parseProperties(Map<ExecutionProperty, List<String>> propertyMap, String... args) throws InterpreterPropertyException { if ( args.length > 0 ) { StringBuilder allArgs = new StringBuilder(); for (String s : args) { allArgs.append(s).append(" "); } String allargs = allArgs.toString(); if ( allargs.trim().length() > 0 && ! allargs.startsWith("-")) { throw new InterpreterPropertyException("arguments must start with a switch, e.g. -f"); } allargs = allargs.substring(1); String[] splitParameterList = allargs.split(" -"); for ( String parameterList : splitParameterList) { StringTokenizer st = new StringTokenizer(parameterList, " "); ExecutionProperty property = getProperty(parameterList, st); addPropertyValues(propertyMap, st, property); } } return propertyMap; } CommandLineParser(List<ExecutionProperty> properties); Map<ExecutionProperty, List<String>> parseProperties(Map<ExecutionProperty, List<String>> propertyMap, String... args);  }
@Test public void testArgWithNoValues() throws InterpreterPropertyException { propertyMap = new HashMap<>(); Map<ExecutionProperty, 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 testArgWithSingleValue() throws InterpreterPropertyException { Map<ExecutionProperty, 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()); }
@Test public void testValueOfWithSingleArg() throws InterpreterPropertyException { Map<ExecutionProperty, 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<ExecutionProperty, 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<ExecutionProperty, 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)); }