AccessValidatorApp { @SuppressWarnings("PMD.SystemPrintln") @SuppressFBWarnings(value = "VA_FORMAT_STRING_USES_NEWLINE", justification = "We will use the standard carriage return character.") ToolsCommonConstants.ReturnValue parseCommandLineArguments(String[] args, ApplicationContext applicationContext) { try { propertiesFilePathOpt = argParser.addArgument("p", "properties", true, "Path to the properties file. Defaults to '.properties'.", 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); messageOpt = argParser.addArgument("m", "message", false, "Use an AWS SQS message", false); argParser.parseArguments(args, false); if (argParser.getBooleanValue(helpOpt)) { System.out.println(argParser.getUsageInformation()); return ToolsCommonConstants.ReturnValue.SUCCESS; } if (argParser.getBooleanValue(versionOpt)) { BuildInformation buildInformation = applicationContext.getBean(BuildInformation.class); System.out.println(String .format(ToolsCommonConstants.BUILD_INFO_STRING_FORMAT, buildInformation.getBuildDate(), buildInformation.getBuildNumber(), buildInformation.getBuildOs(), buildInformation.getBuildUser())); return ToolsCommonConstants.ReturnValue.SUCCESS; } argParser.parseArguments(args, true); } catch (ParseException e) { LOGGER.error("Error parsing command line arguments: {}%n{}", e.getMessage(), argParser.getUsageInformation()); return ToolsCommonConstants.ReturnValue.FAILURE; } return null; } AccessValidatorApp(); @SuppressWarnings("PMD.DoNotCallSystemExit") // Using System.exit is allowed for an actual application to exit. static void main(String[] args);  }
@Test public void testParseCommandLineArgumentsHelpOpt() { String output = runTestGetSystemOut(() -> { String[] arguments = {"--help"}; assertEquals(ToolsCommonConstants.ReturnValue.SUCCESS, accessValidatorApp.parseCommandLineArguments(arguments, applicationContext)); }); assertTrue("Incorrect usage information returned.", output.startsWith("usage: " + AccessValidatorApp.APPLICATION_NAME)); }
@Test public void testParseCommandLineArgumentsVersionOpt() { String output = runTestGetSystemOut(() -> { String[] arguments = {"--version"}; assertEquals(ToolsCommonConstants.ReturnValue.SUCCESS, accessValidatorApp.parseCommandLineArguments(arguments, applicationContext)); }); BuildInformation buildInformation = applicationContext.getBean(BuildInformation.class); assertEquals("output", String .format(DataBridgeApp.BUILD_INFO_STRING_FORMAT, buildInformation.getBuildDate(), buildInformation.getBuildNumber(), buildInformation.getBuildOs(), buildInformation.getBuildUser()), output); }
@Test public void testParseCommandLineArgumentsMessageOpt() { String[] arguments = {"--message"}; assertEquals(null, accessValidatorApp.parseCommandLineArguments(arguments, applicationContext)); }