/** * Constructs a new UploaderApp instance. */ public DownloaderApp() { argParser = new ArgumentParser("herd-downloader-app"); }
/** * Constructs a new application instance. */ RetentionExpirationExporterApp() { argParser = new ArgumentParser(APPLICATION_NAME); }
RetentionExpirationDestroyerApp() { argParser = new ArgumentParser(APPLICATION_NAME); }
@Override public ArgumentParser getArgumentParser() { return new ArgumentParser(TEST_APPLICATION_NAME); }
/** * Constructs a new UploaderApp instance. */ public UploaderApp() { argParser = new ArgumentParser("herd-uploader-app"); // Create command line options specific to the uploader. Other common options will be handled by the base class. createNewVersionOpt = argParser .addArgument("V", "createNewVersion", false, "If not set, only initial version of the business object data is allowed to be created.", false); rrsOpt = argParser.addArgument("r", "rrs", false, "If set, the data will be saved in Reduced Redundancy Storage.", false); maxRetryAttemptsOpt = argParser.addArgument("R", "maxRetryAttempts", true, "The maximum number of the business object data registration retry attempts that uploader would perform before rolling back the upload.", false); retryDelaySecsOpt = argParser.addArgument("D", "retryDelaySecs", true, "The delay in seconds between the business object data registration retry attempts.", false); forceOpt = argParser.addArgument("f", "force", false, "If set, allows upload to proceed when the latest version of the business object data has UPLOADING status by invalidating that version.", false); }
@Test(expected = UnrecognizedOptionException.class) public void testParseArgumentsUnrecognizedOptionException() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument("a", "configured_option", false, "Some flag parameter", false); argParser.parseArguments(new String[] {"-b", "unrecognized_option"}); }
@Test(expected = MissingOptionException.class) public void testParseArguments() throws ParseException { // Create an argument parser with a required "a" option. ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument("a", "some_required_parameter", true, "Some argument", true); // Parse the arguments with a missing argument which should thrown an exception since we are using the method that WILL fail on missing arguments. argParser.parseArguments(new String[] {}); }
@Test public void testGetApplicationName() { final String testApplicationName = "MY_TEST_APP"; ArgumentParser argParser = new ArgumentParser(testApplicationName); assertEquals(testApplicationName, argParser.getApplicationName()); }
@Test(expected = MissingArgumentException.class) public void testParseArgumentsMissingArgumentException() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument("a", "some_optional_parameter", true, "Some optional parameter with an argument", false); argParser.parseArguments(new String[] {"-a"}); }
@Test public void testParseArgumentsFailOnMissingArgumentFalse() throws ParseException { // Create an argument parser with a required "a" option. ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument("a", "some_optional_parameter", false, "Some argument", true); // Parse the arguments with a missing argument, but this should not thrown an exception since we are passing the flag which WILL NOT fail on missing // arguments. argParser.parseArguments(new String[] {}, false); }
@Test(expected = MissingOptionException.class) public void testParseArgumentsFailOnMissingArgumentTrue() throws ParseException { // Create an argument parser with a required "a" option. ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument("a", "some_optional_parameter", false, "Some argument", true); // Parse the arguments with a missing argument which should thrown an exception since we are passing the flag which WILL fail on missing arguments. argParser.parseArguments(new String[] {}, true); }
@Test public void testGetUsageInformation() { List<Option> optionsIn = new ArrayList<>(); optionsIn.add(new Option("a", "some_flag", false, "Some flag parameter")); optionsIn.add(new Option("b", "some_parameter", true, "Some parameter with an argument")); ArgumentParser argParser = new ArgumentParser("TestApp"); argParser.addArgument(optionsIn.get(0), false); argParser.addArgument(optionsIn.get(1), true); String usage = argParser.getUsageInformation(); assertNotNull(usage); assertTrue(usage.contains(String.format("usage: %s", argParser.getApplicationName()))); for (Option option : optionsIn) { assertTrue(usage.contains(String.format("-%s,", option.getOpt()))); assertTrue(usage.contains(String.format("--%s", option.getLongOpt()))); assertTrue(usage.contains(option.getDescription())); assertTrue(!option.hasArg() || usage.contains("<arg>")); } }
@Test public void testAddArgument() { Option opt1 = new Option("a", "some_flag", false, "Some flag parameter"); Option opt2 = new Option("b", "some_parameter", true, "Some parameter with an argument"); List<String> optionsIn = Arrays.asList(optionToString(opt1), optionToString(opt2)); ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument(opt1, true); argParser.addArgument(opt2.getOpt(), opt2.getLongOpt(), opt2.hasArg(), opt2.getDescription(), false); Collection resultOptions = argParser.getConfiguredOptions(); List<String> optionsOut = new ArrayList<>(); for (Object obj : resultOptions) { assertTrue(obj instanceof Option); optionsOut.add(optionToString((Option) obj)); } optionsOut.containsAll(optionsIn); optionsIn.containsAll(optionsOut); }
@Test public void testGetIntegerNullValue() throws ParseException { // Create an argument parser with an optional "a" option. ArgumentParser argParser = new ArgumentParser(""); Option option = argParser.addArgument("a", "some_required_parameter", false, "Some required parameter with an argument", false); // Parse the arguments and get an integer value which wasn't specified which should return null. argParser.parseArguments(new String[] {}); Integer value = argParser.getIntegerValue(option, null, 0, 100); assertNull(value); }
@Test public void testGetFileValue() throws ParseException { File testDefaultValue = new File("default_file_name"); ArgumentParser argParser = new ArgumentParser(""); Option fileOpt = argParser.addArgument("f", "file", true, "Source file name", false); File inputValue; File resultValue; final String shortFileOpt = String.format("-%s", fileOpt.getOpt()); final String longFileOpt = String.format("--%s", fileOpt.getLongOpt()); argParser.parseArguments(new String[] {""}); assertNull(argParser.getFileValue(fileOpt)); assertEquals(testDefaultValue, argParser.getFileValue(fileOpt, testDefaultValue)); inputValue = new File("folder/file_name_1"); argParser.parseArguments(new String[] {shortFileOpt, inputValue.toString()}); resultValue = argParser.getFileValue(fileOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = new File("folder/file_name_2"); argParser.parseArguments(new String[] {shortFileOpt, inputValue.toString()}); resultValue = argParser.getFileValue(fileOpt, testDefaultValue); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = new File("file_name_3"); argParser.parseArguments(new String[] {longFileOpt, inputValue.toString()}); resultValue = argParser.getFileValue(fileOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); }
@Test public void testGetBooleanValue() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); Option boolOpt = argParser.addArgument("b", "bool", false, "Some optional configuration flag", false); Boolean resultValue; final String shortBoolOpt = String.format("-%s", boolOpt.getOpt()); final String longBoolOpt = String.format("--%s", boolOpt.getLongOpt()); argParser.parseArguments(new String[] {}); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertFalse(resultValue); argParser.parseArguments(new String[] {shortBoolOpt}); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertTrue(resultValue); argParser.parseArguments(new String[] {longBoolOpt}); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertTrue(resultValue); }
@Test public void testGetStringValueAsBoolean() throws ParseException ArgumentParser argParser = new ArgumentParser(""); Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter to have a boolean value", false);
@Test public void testGetStringValue() throws ParseException { final String testDefaultValue = "default_str_value"; ArgumentParser argParser = new ArgumentParser(""); Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter", false); String inputValue; String resultValue; final String shortStrOpt = String.format("-%s", strOpt.getOpt()); final String longStrOpt = String.format("--%s", strOpt.getLongOpt()); argParser.parseArguments(new String[] {}); assertNull(argParser.getStringValue(strOpt)); assertEquals(testDefaultValue, argParser.getStringValue(strOpt, testDefaultValue)); inputValue = "my_string_value_1"; argParser.parseArguments(new String[] {shortStrOpt, inputValue}); resultValue = argParser.getStringValue(strOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = "my_string_value_2"; argParser.parseArguments(new String[] {shortStrOpt, inputValue}); resultValue = argParser.getStringValue(strOpt, testDefaultValue); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = "my_string_value_3"; argParser.parseArguments(new String[] {longStrOpt, inputValue}); resultValue = argParser.getStringValue(strOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); }
final Integer testMaxValue = 1000; ArgumentParser argParser = new ArgumentParser(""); Option intOpt = argParser.addArgument("i", "int", true, "Some integer parameter", false); Integer inputValue;