/** * 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); }
/** * Creates and adds an Option using the specified parameters. The parameters contain a short-name and a long-name. It may be specified as requiring an * argument and/or being mandatory. * * @param opt Short single-character name of the option. * @param longOpt Long multi-character name of the option. * @param hasArg flag signally if an argument is required after this option * @param description Self-documenting description * @param required specifies whether the option being added is mandatory * * @return the resulting option that was added */ public Option addArgument(String opt, String longOpt, boolean hasArg, String description, boolean required) { Option option = new Option(opt, longOpt, hasArg, description); return addArgument(option, required); }
/** * Creates and adds an Option using the specified parameters. The parameters contain a short-name and a long-name. It may be specified as requiring an * argument and/or being mandatory. * * @param opt Short single-character name of the option. * @param longOpt Long multi-character name of the option. * @param hasArg flag signally if an argument is required after this option * @param description Self-documenting description * @param required specifies whether the option being added is mandatory * * @return the resulting option that was added */ public Option addArgument(String opt, String longOpt, boolean hasArg, String description, boolean required) { Option option = new Option(opt, longOpt, hasArg, description); return addArgument(option, required); }
namespaceOpt = argParser.addArgument("n", "namespace", true, "Namespace.", true); businessObjectDefinitionNameOpt = argParser.addArgument("b", "businessObjectDefinitionName", true, "Business object definition.", true); localOutputFileOpt = argParser.addArgument("o", "localOutputFile", true, "The path to files on your local file system.", true); regServerHostOpt = argParser.addArgument("H", "regServerHost", true, "Registration Service hostname.", true); Option regServerPortOpt = argParser.addArgument("P", "regServerPort", true, "Registration Service port.", true); udcServerHostOpt = argParser.addArgument("c", "udcServerHost", true, "UDC application server hostname.", true); Option sslOpt = argParser.addArgument("s", "ssl", true, "Enable or disable SSL (HTTPS).", false); usernameOpt = argParser.addArgument("u", "username", true, "The username for HTTPS client authentication.", false); passwordOpt = argParser.addArgument("w", "password", true, "The password used for HTTPS client authentication.", false); trustSelfSignedCertificateOpt = argParser.addArgument("C", "trustSelfSignedCertificate", true, "If set to true, makes HTTPS client trust self-signed certificate.", false); disableHostnameVerificationOpt = argParser.addArgument("d", "disableHostnameVerification", true, "If set to true, turns off hostname verification.", false); Option helpOpt = argParser.addArgument("h", "help", false, "Display usage information and exit.", false); Option versionOpt = argParser.addArgument("v", "version", false, "Display version information and exit.", false);
localInputFileOpt = argParser.addArgument("i", "localInputFile", true, "The path to files on your local file system as input.", true); regServerHostOpt = argParser.addArgument("H", "regServerHost", true, "Registration Service hostname.", true); Option regServerPortOpt = argParser.addArgument("P", "regServerPort", true, "Registration Service port.", true); Option sslOpt = argParser.addArgument("s", "ssl", true, "Enable or disable SSL (HTTPS).", false); usernameOpt = argParser.addArgument("u", "username", true, "The username for HTTPS client authentication.", false); passwordOpt = argParser.addArgument("w", "password", true, "The password used for HTTPS client authentication.", false); trustSelfSignedCertificateOpt = argParser.addArgument("C", "trustSelfSignedCertificate", true, "If set to true, makes HTTPS client trust self-signed certificate.", false); disableHostnameVerificationOpt = argParser.addArgument("d", "disableHostnameVerification", true, "If set to true, turns off hostname verification.", false); Option helpOpt = argParser.addArgument("h", "help", false, "Display usage information and exit.", false); Option versionOpt = argParser.addArgument("v", "version", false, "Display version information and exit.", false);
s3AccessKeyOpt = argParser.addArgument("a", "s3AccessKey", true, "S3 access key.", false); s3SecretKeyOpt = argParser.addArgument("p", "s3SecretKey", true, "S3 secret key.", false); s3EndpointOpt = argParser.addArgument("e", "s3Endpoint", true, "S3 endpoint.", false); localPathOpt = argParser.addArgument("l", "localPath", true, "The path to files on your local file system.", true); manifestPathOpt = argParser.addArgument("m", "manifestPath", true, "Local path to the manifest file.", true); regServerHostOpt = argParser.addArgument("H", "regServerHost", true, "Registration Service hostname.", false); regServerPortOpt = argParser.addArgument("P", "regServerPort", true, "Registration Service port.", false); dmRegServerHostOpt = argParser.addArgument("Y", "dmRegServerHost", true, "Registration Service hostname (deprecated - use regServerHost).", false); dmRegServerPortOpt = argParser.addArgument("Z", "dmRegServerPort", true, "Registration Service port (deprecated - use regServerPort.", false); sslOpt = argParser.addArgument("s", "ssl", true, "Enable or disable SSL (HTTPS).", false); usernameOpt = argParser.addArgument("u", "username", true, "The username for HTTPS client authentication.", false); passwordOpt = argParser.addArgument("w", "password", true, "The password used for HTTPS client authentication.", false); trustSelfSignedCertificateOpt = argParser.addArgument("C", "trustSelfSignedCertificate", true, "If set to true, makes HTTPS client trust self-signed certificate.", false); disableHostnameVerificationOpt = argParser.addArgument("d", "disableHostnameVerification", true, "If set to true, turns off hostname verification.", false); helpOpt = argParser.addArgument("h", "help", false, "Display usage information and exit.", false); versionOpt = argParser.addArgument("v", "version", false, "Display version information and exit.", false); httpProxyHostOpt = argParser.addArgument("n", "httpProxyHost", true, "HTTP proxy host.", false); httpProxyPortOpt = argParser.addArgument("o", "httpProxyPort", true, "HTTP proxy port.", false); maxThreadsOpt = argParser.addArgument("t", "maxThreads", true, "Maximum number of threads.", false); socketTimeoutOpt = argParser.addArgument("c", "socketTimeout", true, "The socket timeout in milliseconds. 0 indicates no timeout. Default 50000.", false);
s3AccessKeyOpt = argParser.addArgument("a", "s3AccessKey", true, "S3 access key.", false); s3SecretKeyOpt = argParser.addArgument("p", "s3SecretKey", true, "S3 secret key.", false); s3EndpointOpt = argParser.addArgument("e", "s3Endpoint", true, "S3 endpoint.", false); localPathOpt = argParser.addArgument("l", "localPath", true, "The path to files on your local file system.", true); manifestPathOpt = argParser.addArgument("m", "manifestPath", true, "Local path to the manifest file.", true); regServerHostOpt = argParser.addArgument("H", "regServerHost", true, "Registration Service hostname.", false); regServerPortOpt = argParser.addArgument("P", "regServerPort", true, "Registration Service port.", false); dmRegServerHostOpt = argParser.addArgument("Y", "dmRegServerHost", true, "Registration Service hostname (deprecated - use regServerHost).", false); dmRegServerPortOpt = argParser.addArgument("Z", "dmRegServerPort", true, "Registration Service port (deprecated - use regServerPort.", false); sslOpt = argParser.addArgument("s", "ssl", true, "Enable or disable SSL (HTTPS).", false); usernameOpt = argParser.addArgument("u", "username", true, "The username for HTTPS client authentication.", false); passwordOpt = argParser.addArgument("w", "password", true, "The password used for HTTPS client authentication.", false); trustSelfSignedCertificateOpt = argParser.addArgument("C", "trustSelfSignedCertificate", true, "If set to true, makes HTTPS client trust self-signed certificate.", false); disableHostnameVerificationOpt = argParser.addArgument("d", "disableHostnameVerification", true, "If set to true, turns off hostname verification.", false); helpOpt = argParser.addArgument("h", "help", false, "Display usage information and exit.", false); versionOpt = argParser.addArgument("v", "version", false, "Display version information and exit.", false); httpProxyHostOpt = argParser.addArgument("n", "httpProxyHost", true, "HTTP proxy host.", false); httpProxyPortOpt = argParser.addArgument("o", "httpProxyPort", true, "HTTP proxy port.", false); maxThreadsOpt = argParser.addArgument("t", "maxThreads", true, "Maximum number of threads.", false); socketTimeoutOpt = argParser.addArgument("c", "socketTimeout", true, "The socket timeout in milliseconds. 0 indicates no timeout. Default 50000.", false);
@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 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 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(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(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 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); }
Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter to have a boolean value", false);
Option intOpt = argParser.addArgument("i", "int", true, "Some integer parameter", false); Integer inputValue; Integer resultValue;
@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); }