ArgumentParser { public Integer getIntegerValue(Option option) throws NumberFormatException { String value = getStringValue(option); return (value != null) ? Integer.parseInt(value) : null; } ArgumentParser(String applicationName); String getApplicationName(); Option addArgument(Option option, boolean required); Option addArgument(String opt, String longOpt, boolean hasArg, String description, boolean required); Collection getConfiguredOptions(); void parseArguments(String[] args, boolean failOnMissingRequiredOptions); void parseArguments(String[] args); String getUsageInformation(); @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") Boolean getBooleanValue(Option option); @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") Boolean getStringValueAsBoolean(Option option, Boolean defaultValue); @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") String getStringValue(Option option); String getStringValue(Option option, String defaultValue); Integer getIntegerValue(Option option); Integer getIntegerValue(Option option, Integer defaultValue); Integer getIntegerValue(Option option, Integer defaultValue, Integer minValue, Integer maxValue); File getFileValue(Option option); File getFileValue(Option option, File defaultValue);  }
@Test public void testGetIntegerValue() throws ParseException { final Integer testDefaultValue = 500; final Integer testMinValue = 0; final Integer testMaxValue = 1000; ArgumentParser argParser = new ArgumentParser(""); Option intOpt = argParser.addArgument("i", "int", true, "Some integer parameter", false); Integer inputValue; Integer resultValue; final String shortIntOpt = String.format("-%s", intOpt.getOpt()); final String longIntOpt = String.format("--%s", intOpt.getLongOpt()); argParser.parseArguments(new String[] {}); assertNull(argParser.getIntegerValue(intOpt)); assertEquals(testDefaultValue, argParser.getIntegerValue(intOpt, testDefaultValue)); inputValue = 123; argParser.parseArguments(new String[] {shortIntOpt, inputValue.toString()}); resultValue = argParser.getIntegerValue(intOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = 456; argParser.parseArguments(new String[] {shortIntOpt, inputValue.toString()}); resultValue = argParser.getIntegerValue(intOpt, testDefaultValue); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = 789; argParser.parseArguments(new String[] {longIntOpt, inputValue.toString()}); resultValue = argParser.getIntegerValue(intOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = 234; argParser.parseArguments(new String[] {longIntOpt, inputValue.toString()}); resultValue = argParser.getIntegerValue(intOpt, testDefaultValue, testMinValue, testMaxValue); assertNotNull(resultValue); assertEquals(inputValue, resultValue); argParser.parseArguments(new String[] {}); resultValue = argParser.getIntegerValue(intOpt, testDefaultValue, testMinValue, testMaxValue); assertNotNull(resultValue); assertEquals(testDefaultValue, resultValue); argParser.parseArguments(new String[] {longIntOpt, testMinValue.toString()}); resultValue = argParser.getIntegerValue(intOpt, testDefaultValue, testMinValue, testMaxValue); assertNotNull(resultValue); assertEquals(testMinValue, resultValue); argParser.parseArguments(new String[] {longIntOpt, testMaxValue.toString()}); resultValue = argParser.getIntegerValue(intOpt, testDefaultValue, testMinValue, testMaxValue); assertNotNull(resultValue); assertEquals(testMaxValue, resultValue); argParser.parseArguments(new String[] {longIntOpt, testDefaultValue.toString()}); resultValue = argParser.getIntegerValue(intOpt, testDefaultValue, testDefaultValue, testDefaultValue); assertNotNull(resultValue); assertEquals(testDefaultValue, resultValue); inputValue = testMinValue - 1; argParser.parseArguments(new String[] {longIntOpt, inputValue.toString()}); try { argParser.getIntegerValue(intOpt, testDefaultValue, testMinValue, testMaxValue); fail("Suppose to throw an IllegalArgumentException when option value is less than the minimum allowed value."); } catch (IllegalArgumentException e) { assertEquals(String.format("The %s option value %d is less than the minimum allowed value of %d.", intOpt.getLongOpt(), inputValue, testMinValue), e.getMessage()); } inputValue = testMaxValue + 1; argParser.parseArguments(new String[] {longIntOpt, inputValue.toString()}); try { argParser.getIntegerValue(intOpt, testDefaultValue, testMinValue, testMaxValue); fail("Suppose to throw an IllegalArgumentException when option value is greater than the maximum allowed value."); } catch (IllegalArgumentException e) { assertEquals(String.format("The %s option value %d is bigger than maximum allowed value of %d.", intOpt.getLongOpt(), inputValue, testMaxValue), e.getMessage()); } }