StellarShellOptionsValidator { public static void validateOptions(CommandLine commandLine) throws IllegalArgumentException { if (commandLine.hasOption('z')) { validateZookeeperOption(commandLine.getOptionValue('z')); } if (commandLine.hasOption('v')) { validateFileOption("v", commandLine.getOptionValue('v')); } if (commandLine.hasOption("irc")) { validateFileOption("irc", commandLine.getOptionValue("irc")); } if (commandLine.hasOption('p')) { validateFileOption("p", commandLine.getOptionValue('p')); } }  static void validateOptions(CommandLine commandLine);  }
@Test public void validateOptions() throws Exception { String[] validZHostArg = new String[]{"-z", "localhost:8888"}; String[] validZHostArgNoPort = new String[]{"-z", "localhost"}; String[] validZIPArgNoPort = new String[]{"-z", "10.10.10.3"}; String[] validZHostArgList = new String[]{"-z", "localhost:8888,localhost:2181,localhost"}; String[] validZIPArg = new String[]{"-z", "10.10.10.3:9999"}; String[] invalidZNameArg = new String[]{"-z", "!!!@!!@!:8882"}; String[] invalidZIPArg = new String[]{"-z", "11111.22222.10.3:3332"}; String[] invalidZMissingNameArg = new String[]{"-z", ":8882"}; String[] invalidZZeroPortArg = new String[]{"-z", "youtube.com:0"}; String[] invalidZHugePortArg = new String[]{"-z", "youtube.com:75565"}; String existingFileName = "./target/existsFile"; String nonExistentFile = "./target/doesNotExist"; String[] validVFileArg = new String[]{"-v", existingFileName}; String[] validIrcFileArg = new String[]{"-irc", existingFileName}; String[] validPFileArg = new String[]{"-p", existingFileName}; String[] invalidVFileArg = new String[]{"-v", nonExistentFile}; String[] invalidIrcFileArg = new String[]{"-irc", nonExistentFile}; String[] invalidPFileArg = new String[]{"-p", nonExistentFile}; File existingFile = new File(existingFileName); if (!existingFile.exists()) { existingFile.createNewFile(); } Options options = new Options(); options.addOption("z", "zookeeper", true, "Zookeeper URL"); options.addOption("v", "variables", true, "File containing a JSON Map of variables"); options.addOption("irc", "inputrc", true, "File containing the inputrc if not the default ~/.inputrc"); options.addOption("na", "no_ansi", false, "Make the input prompt not use ANSI colors."); options.addOption("h", "help", false, "Print help"); options.addOption("p", "properties", true, "File containing Stellar properties"); CommandLineParser parser = new PosixParser(); CommandLine commandLine = parser.parse(options, validZHostArg); StellarShellOptionsValidator.validateOptions(commandLine); commandLine = parser.parse(options, validZIPArg); StellarShellOptionsValidator.validateOptions(commandLine); commandLine = parser.parse(options, validVFileArg); StellarShellOptionsValidator.validateOptions(commandLine); commandLine = parser.parse(options, validIrcFileArg); StellarShellOptionsValidator.validateOptions(commandLine); commandLine = parser.parse(options, validPFileArg); StellarShellOptionsValidator.validateOptions(commandLine); commandLine = parser.parse(options, validZHostArgNoPort); StellarShellOptionsValidator.validateOptions(commandLine); commandLine = parser.parse(options, validZHostArgList); StellarShellOptionsValidator.validateOptions(commandLine); commandLine = parser.parse(options, validZIPArgNoPort); StellarShellOptionsValidator.validateOptions(commandLine); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidZNameArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for providing invalid host name "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidZIPArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for providing invalid ip address "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidZMissingNameArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for only providing port "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidZZeroPortArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for 0 port "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidZHugePortArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for port out of range "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidVFileArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for passing non-existant file to -v "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidVFileArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for passing non-existant file to -v "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidIrcFileArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for passing non-existant file to -irc "); assertThrows( IllegalArgumentException.class, () -> { CommandLine cl = parser.parse(options, invalidPFileArg); StellarShellOptionsValidator.validateOptions(cl); }, "Did not catch failure for passing non-existant file to -p "); }