Пример #1
0
 public void testSettingFlag() {
   String original = "-Dsome.name=yes -server -Darg2=no";
   String[] asArray = ArgsUtil.parse(original);
   assertEquals(3, asArray.length);
   String cleared = ArgsUtil.clearFlag(original, "-server");
   assertEquals("-Dsome.name=yes -Darg2=no", cleared);
   String set = ArgsUtil.setFlag(cleared, "-ser3");
   assertEquals("-Dsome.name=yes -Darg2=no -ser3", set);
 }
Пример #2
0
  public void testReplace() {
    String allArgs = "";
    allArgs = ArgsUtil.setArg(allArgs, "-h", null, "new");
    assertTrue(ArgsUtil.parse(allArgs).length == 2);
    assertTrue(ArgsUtil.getValue(allArgs, "-h", null).equals("new"));
    allArgs = ArgsUtil.setArg(allArgs, "-h", null, "correct");
    assertTrue(ArgsUtil.parse(allArgs).length == 2);
    assertTrue(ArgsUtil.getValue(allArgs, "-h", null).equals("correct"));

    allArgs = ArgsUtil.setArg(allArgs, null, "--two", "newtwo");
    assertTrue(ArgsUtil.parse(allArgs).length == 3);

    // opt was set as long opt,  --two=newtwo.   Search for short opt fails
    assertTrue(ArgsUtil.getValue(allArgs, "--two", null) == null);

    // clear long opt
    allArgs = ArgsUtil.setArg(allArgs, null, "--two", null);
    assertTrue(ArgsUtil.parse(allArgs).length == 2);

    // test replacement of short args
    allArgs = ArgsUtil.setArg(allArgs, new String[] {"-h", "-o"}, new String[] {}, "twoOpt", false);
    assertTrue(ArgsUtil.parse(allArgs).length == 2); // no change

    assertTrue(ArgsUtil.getValue(allArgs, "-h", null).equals("twoOpt"));
    assertTrue(ArgsUtil.getValue(allArgs, new String[] {"-h", "-o"}, null).equals("twoOpt"));
    assertTrue(ArgsUtil.getValue(allArgs, new String[] {"-o", "-h"}, null).equals("twoOpt"));

    // test clear of short arg
    allArgs = ArgsUtil.setArg(allArgs, new String[] {"-h", "-o"}, new String[] {}, null, false);
    assertTrue(ArgsUtil.parse(allArgs).length == 1); // no change

    allArgs = ArgsUtil.setArg(allArgs, null, "--three", "three");
    assertTrue(ArgsUtil.getValue(allArgs, null, "--three").equals("three"));

    allArgs = ArgsUtil.setArg(allArgs, null, "--three", "threea");
    assertTrue(ArgsUtil.getValue(allArgs, null, "--three").equals("threea"));

    // already has --three,  test replace, ensure --four is the new arg
    // since it is the first of the two acceptable replacements
    assertTrue(allArgs.contains("--three"));
    allArgs = ArgsUtil.setArg(allArgs, null, new String[] {"--four", "--three"}, "three_b", false);
    assertTrue(allArgs.contains("--four"));
    assertTrue(ArgsUtil.getValue(allArgs, null, "--three") == null);
    assertTrue(ArgsUtil.getValue(allArgs, null, "--four").equals("three_b"));
    assertTrue(
        ArgsUtil.getValue(allArgs, null, new String[] {"--three", "--four"}).equals("three_b"));
  }
Пример #3
0
 public void testParse() {
   assertEquals(1, ArgsUtil.parse("").length);
   assertEquals(1, ArgsUtil.parse("a").length);
   assertEquals(1, ArgsUtil.parse("abc ").length);
   assertEquals(1, ArgsUtil.parse(" abcde ").length);
   assertEquals(1, ArgsUtil.parse("   abcde ").length);
   assertEquals(1, ArgsUtil.parse("   abcde   ").length);
   assertEquals(2, ArgsUtil.parse("a b").length);
   assertEquals(2, ArgsUtil.parse("a  b").length);
   assertEquals(2, ArgsUtil.parse(" a  b").length);
   assertEquals(2, ArgsUtil.parse(" a b  ").length);
   assertEquals(2, ArgsUtil.parse("-b test  ").length);
   assertEquals(2, ArgsUtil.parse("-b --host=someval  ").length);
   assertEquals(2, ArgsUtil.parse("-b \"--host=some val\"").length);
   assertEquals(3, ArgsUtil.parse("-b val --host=someval").length);
   assertEquals(2, ArgsUtil.parse("-b --host=\"some val\"").length);
   assertEquals(2, ArgsUtil.parse("-b \"--host=some val\"").length);
 }