public CryptoProcessor(HadoopShims.HdfsEncryptionShim encryptionShim, HiveConf conf) { this.encryptionShim = encryptionShim; this.conf = conf; CREATE_KEY_OPTIONS = new Options(); CREATE_KEY_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("keyName").isRequired().create()); CREATE_KEY_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("bitLength").create()); // optional DELETE_KEY_OPTIONS = new Options(); DELETE_KEY_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("keyName").isRequired().create()); CREATE_ZONE_OPTIONS = new Options(); CREATE_ZONE_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("keyName").isRequired().create()); CREATE_ZONE_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("path").isRequired().create()); }
public CryptoProcessor(HadoopShims.HdfsEncryptionShim encryptionShim, HiveConf conf) { this.encryptionShim = encryptionShim; this.conf = conf; CREATE_KEY_OPTIONS = new Options(); CREATE_KEY_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("keyName").isRequired().create()); CREATE_KEY_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("bitLength").create()); // optional DELETE_KEY_OPTIONS = new Options(); DELETE_KEY_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("keyName").isRequired().create()); CREATE_ZONE_OPTIONS = new Options(); CREATE_ZONE_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("keyName").isRequired().create()); CREATE_ZONE_OPTIONS.addOption(OptionBuilder.hasArg().withLongOpt("path").isRequired().create()); }
/** * Unsets an erasure coding policy on a directory at the specified path. * @param params Parameters passed to the command. * @throws Exception if command failed. */ private void unsetPolicy(String[] params) throws Exception { String command = "unsetPolicy"; try { // unsetPolicy -path <path> Options unsetPolicyOptions = new Options(); String pathOptionName = "path"; Option pathOption = OptionBuilder.hasArg() .isRequired() .withLongOpt(pathOptionName) .withDescription("Path to unset policy on") .create(); unsetPolicyOptions.addOption(pathOption); CommandLine args = parseCommandArgs(unsetPolicyOptions, params); String path = args.getOptionValue(pathOptionName); erasureCodingShim.unsetErasureCodingPolicy(new Path(path)); writeTestOutput("Unset EC policy"); } catch (ParseException pe) { writeTestOutput("Error parsing options for " + command + " " + pe.getMessage()); } catch (Exception e) { writeTestOutput("Caught exception running " + command + ": " + e.getMessage()); throw new Exception("Cannot run " + command + ": " + e.getMessage()); } }
.isRequired() .withLongOpt(pathOptionName) .withDescription("Path to set policy on")
/** * Remove an erasure coding policy. * @param params Parameters passed to the command. * @throws Exception if command failed. */ private void removePolicy(String[] params) throws Exception { String command = "removePolicy"; try { // removePolicy -policy <policyName> Options removePolicyOptions = new Options(); String policyOptionName = "policy"; Option policyOption = OptionBuilder.hasArg() .isRequired() .withLongOpt(policyOptionName) .withDescription("Policy to remove") .create(); removePolicyOptions.addOption(policyOption); CommandLine args = parseCommandArgs(removePolicyOptions, params); String policyName = args.getOptionValue(policyOptionName); erasureCodingShim.removeErasureCodingPolicy(policyName); writeTestOutput("Removed EC policy '" + policyName + "'"); } catch (ParseException pe) { writeTestOutput("Error parsing options for " + command + " " + pe.getMessage()); } catch (Exception e) { writeTestOutput("Caught exception running " + command + ": " + e.getMessage()); throw new Exception("Cannot run " + command + ": " + e.getMessage()); } }
/** * Enable an erasure coding policy. * @param params Parameters passed to the command. * @throws Exception If command failed. */ private void enablePolicy(String[] params) throws Exception { String command = "enablePolicy"; try { // enablePolicy -policy <policyName> Options enablePolicyOptions = new Options(); String policyOptionName = "policy"; Option policyOption = OptionBuilder.hasArg() .isRequired() .withLongOpt(policyOptionName) .withDescription("Policy to enable") .hasArg() .create(); enablePolicyOptions.addOption(policyOption); CommandLine args = parseCommandArgs(enablePolicyOptions, params); String policyName = args.getOptionValue(policyOptionName); erasureCodingShim.enableErasureCodingPolicy(policyName); writeTestOutput("Enabled EC policy '" + policyName + "'"); } catch (ParseException pe) { writeTestOutput("Error parsing options for " + command + " " + pe.getMessage()); } catch (Exception e) { writeTestOutput("Caught exception running " + command + ": " + e.getMessage()); throw new Exception("Cannot run " + command + ": " + e.getMessage()); } }
/** * Disable an erasure coding policy. * @param params Parameters passed to the command. * @throws Exception If command failed. */ private void disablePolicy(String[] params) throws Exception { String command = "disablePolicy"; try { // disablePolicy -policy <policyName> Options disablePolicyOptions = new Options(); String policyOptionName = "policy"; Option policyOption = OptionBuilder.hasArg() .isRequired() .withLongOpt(policyOptionName) .withDescription("Policy to disable") .create(); disablePolicyOptions.addOption(policyOption); CommandLine args = parseCommandArgs(disablePolicyOptions, params); String policyName = args.getOptionValue(policyOptionName); erasureCodingShim.disableErasureCodingPolicy(policyName); writeTestOutput("Disabled EC policy '" + policyName + "'"); } catch (ParseException pe) { writeTestOutput("Error parsing options for " + command + " " + pe.getMessage()); } catch (Exception e) { writeTestOutput("Caught exception running " + command + ": " + e.getMessage()); throw new Exception("Cannot run " + command + ": " + e.getMessage()); } }
/** * Get an erasure coding policy for a Path. * @param params Parameters passed to the command. * @throws Exception if command failed. */ private void getPolicy(String[] params) throws Exception { String command = "getPolicy"; try { // getPolicy -path <path> Options getPolicyOptions = new Options(); String pathOptionName = "path"; Option policyOption = OptionBuilder.hasArg() .isRequired() .withLongOpt(pathOptionName) .withDescription("Path for which Policy should be fetched") .create(); getPolicyOptions.addOption(policyOption); CommandLine args = parseCommandArgs(getPolicyOptions, params); String path = args.getOptionValue(pathOptionName); HdfsFileErasureCodingPolicy policy = erasureCodingShim.getErasureCodingPolicy(new Path(path)); writeTestOutput("EC policy is '" + (policy != null ? policy.getName() : "REPLICATED") + "'"); } catch (ParseException pe) { writeTestOutput("Error parsing options for " + command + " " + pe.getMessage()); } catch (Exception e) { writeTestOutput("Caught exception running " + command + ": " + e.getMessage()); throw new Exception("Cannot run " + command + ": " + e.getMessage(), e); } }
@Test public void test14786() throws Exception { Option o = OptionBuilder.isRequired().withDescription("test").create("test"); Options opts = new Options(); opts.addOption(o); opts.addOption(o); CommandLineParser parser = new GnuParser(); String[] args = new String[] { "-test" }; CommandLine line = parser.parse( opts, args ); assertTrue( line.hasOption( "test" ) ); }
@Test public void testPropertyOptionRequired() throws Exception { Options opts = new Options(); opts.addOption(OptionBuilder.isRequired().create("f")); Properties properties = new Properties(); properties.setProperty("f", "true"); CommandLine cmd = parse(parser, opts, null, properties); assertTrue(cmd.hasOption("f")); }
@Test public void testWithRequiredOption() throws Exception { String[] args = new String[] { "-b", "file" }; Options options = new Options(); options.addOption("a", "enable-a", false, null); options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); CommandLine cl = parser.parse(options,args); assertTrue("Confirm -a is NOT set", !cl.hasOption("a")); assertTrue("Confirm -b is set", cl.hasOption("b")); assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("file")); assertTrue("Confirm NO of extra args", cl.getArgList().size() == 0); }
@Test public void testOptionAndRequiredOption() throws Exception { String[] args = new String[] { "-a", "-b", "file" }; Options options = new Options(); options.addOption("a", "enable-a", false, null); options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); CommandLine cl = parser.parse(options,args); assertTrue("Confirm -a is set", cl.hasOption("a")); assertTrue("Confirm -b is set", cl.hasOption("b")); assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("file")); assertTrue("Confirm NO of extra args", cl.getArgList().size() == 0); }
@Test public void testReuseOptionsTwice() throws Exception { Options opts = new Options(); opts.addOption(OptionBuilder.isRequired().create('v')); // first parsing parser.parse(opts, new String[] { "-v" }); try { // second parsing, with the same Options instance and an invalid command line parser.parse(opts, new String[0]); fail("MissingOptionException not thrown"); } catch (MissingOptionException e) { // expected } }
@Test public void testMissingOptionsException() throws ParseException { Options options = new Options(); options.addOption(OptionBuilder.isRequired().create("f")); options.addOption(OptionBuilder.isRequired().create("x")); try { new PosixParser().parse(options, new String[0]); fail("Expected MissingOptionException to be thrown"); } catch (MissingOptionException e) { assertEquals("Missing required options: f, x", e.getMessage()); } }
@Test public void testMissingOptionException() throws ParseException { Options options = new Options(); options.addOption(OptionBuilder.isRequired().create("f")); try { new PosixParser().parse(options, new String[0]); fail("Expected MissingOptionException to be thrown"); } catch (MissingOptionException e) { assertEquals("Missing required option: f", e.getMessage()); } }
@Test public void testMissingRequiredOptions() { String[] args = new String[] { "-a" }; Options options = new Options(); options.addOption("a", "enable-a", false, null); options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); options.addOption(OptionBuilder.withLongOpt("cfile").hasArg().isRequired().create('c')); try { parser.parse(options,args); fail("exception should have been thrown"); } catch (MissingOptionException e) { assertEquals("Incorrect exception message", "Missing required options: b, c", e.getMessage()); assertTrue(e.getMissingOptions().contains("b")); assertTrue(e.getMissingOptions().contains("c")); } catch (ParseException e) { fail("expected to catch MissingOptionException"); } }
@Test public void testMissingRequiredOption() { String[] args = new String[] { "-a" }; Options options = new Options(); options.addOption("a", "enable-a", false, null); options.addOption(OptionBuilder.withLongOpt("bfile").hasArg().isRequired().create('b')); try { parser.parse(options,args); fail("exception should have been thrown"); } catch (MissingOptionException e) { assertEquals( "Incorrect exception message", "Missing required option: b", e.getMessage() ); assertTrue(e.getMissingOptions().contains("b")); } catch (ParseException e) { fail("expected to catch MissingOptionException"); } }
@Test public void testMissingRequiredGroup() throws Exception { OptionGroup group = new OptionGroup(); group.addOption(OptionBuilder.create("a")); group.addOption(OptionBuilder.create("b")); group.setRequired(true); Options options = new Options(); options.addOptionGroup(group); options.addOption(OptionBuilder.isRequired().create("c")); try { parser.parse(options, new String[] { "-c" }); fail("MissingOptionException not thrown"); } catch (MissingOptionException e) { assertEquals(1, e.getMissingOptions().size()); assertTrue(e.getMissingOptions().get(0) instanceof OptionGroup); } catch (ParseException e) { fail("Expected to catch MissingOptionException"); } }
@Test public void testCompleteOption( ) { Option simple = OptionBuilder.withLongOpt( "simple option") .hasArg( ) .isRequired( ) .hasArgs( ) .withType( Float.class ) .withDescription( "this is a simple option" ) .create( 's' ); assertEquals( "s", simple.getOpt() ); assertEquals( "simple option", simple.getLongOpt() ); assertEquals( "this is a simple option", simple.getDescription() ); assertEquals( simple.getType(), Float.class ); assertTrue( simple.hasArg() ); assertTrue( simple.isRequired() ); assertTrue( simple.hasArgs() ); }
@Test public void testTwoCompleteOptions( ) { Option simple = OptionBuilder.withLongOpt( "simple option") .hasArg( ) .isRequired( ) .hasArgs( ) .withType( Float.class ) .withDescription( "this is a simple option" ) .create( 's' ); assertEquals( "s", simple.getOpt() ); assertEquals( "simple option", simple.getLongOpt() ); assertEquals( "this is a simple option", simple.getDescription() ); assertEquals( simple.getType(), Float.class ); assertTrue( simple.hasArg() ); assertTrue( simple.isRequired() ); assertTrue( simple.hasArgs() ); simple = OptionBuilder.withLongOpt( "dimple option") .hasArg( ) .withDescription( "this is a dimple option" ) .create( 'd' ); assertEquals( "d", simple.getOpt() ); assertEquals( "dimple option", simple.getLongOpt() ); assertEquals( "this is a dimple option", simple.getDescription() ); assertEquals( String.class, simple.getType() ); assertTrue( simple.hasArg() ); assertTrue( !simple.isRequired() ); assertTrue( !simple.hasArgs() ); }