public static void main(String[] args) { boolean proceed = handleVersionOrHelpSwitches(args); if ( proceed ) { boolean success = false; try { Chorus chorus = new Chorus(args); success = chorus.run(); } catch (InterpreterPropertyException e) { ChorusOut.err.println(e.getMessage()); ChorusOut.err.print(ChorusConfigProperty.getHelpText()); } catch (Throwable t) { ChorusOut.err.println("Chorus encountered an error and had to exit"); ChorusOut.err.print(t.toString()); } //We should exit with a code between 0-255 since this is the valid range for unix exit statuses //(windows supports signed integer exit status, unix does not) //choosing the most obvious, 0 = success, 1 = failure, we could expand on this if needed int exitCode = success ? 0 : 1; System.exit(exitCode); } }
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; } }
chorus = new Chorus(args.split(" ")); } catch (InterpreterPropertyException e) { throw new RuntimeException("Error with Chorus command line " + e.getMessage());
private void checkValues(ConfigurationProperty p, List<String> values) throws InterpreterPropertyException { Pattern pattern = Pattern.compile(p.getValidatingExpression()); for (String value : values) { Matcher m = pattern.matcher(value); if ( ! m.matches()) { throw new InterpreterPropertyException( "Could not parse the value for interpreter property " + p + " expected to be in the form " + p.getExample() ); } } }
@Test public void testCannotSetMoreThanMaxValues() { ConfigurationProperty propertyWithMinValues = new TestProperty(TestConfigProperty.HANDLER_PACKAGES) { public int getMaxValueCount() { return 1; } }; ConfigReader c = new ConfigReader(Collections.singletonList(propertyWithMinValues), new String[] { "-h", "onevalue", "twovalues" }); try { c.readConfiguration(); } catch (InterpreterPropertyException e) { assertTrue("contains At Most 1", e.getMessage().contains("At most 1 value(s) must be supplied")); return; } fail("Must complain when more than max vals set"); }
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); } }
@Test public void testCannotSetLessThanMinimumValues() { ConfigurationProperty propertyWithMinValues = new TestProperty(TestConfigProperty.HANDLER_PACKAGES) { public int getMinValueCount() { return 2; } }; ConfigReader c = new ConfigReader(Collections.singletonList(propertyWithMinValues), new String[] { "-h", "onevalue" }); try { c.readConfiguration(); } catch (InterpreterPropertyException e) { assertTrue("contains At Least 2", e.getMessage().contains("At least 2 value(s) must be supplied")); return; } fail("Must complain when less than min vals set"); }
throw new InterpreterPropertyException("arguments must start with a switch, e.g. -f");
@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"); }
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()); } } }
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" ); } } }