1 package com.beust.jcommander; 2 3 import org.testng.Assert; 4 import org.testng.annotations.DataProvider; 5 import org.testng.annotations.Test; 6 7 import java.util.Arrays; 8 import java.util.LinkedList; 9 import java.util.List; 10 11 public class CmdTest { 12 13 @Parameters(commandNames = "--cmd-one") 14 public static class CmdOne { 15 } 16 17 @Parameters(commandNames = "--cmd-two") 18 class CmdTwo { 19 @Parameter 20 List<String> params = new java.util.LinkedList<>(); 21 } 22 parseArgs(boolean withDefault, String[] args)23 public String parseArgs(boolean withDefault, String[] args) { 24 JCommander jc = new JCommander(); 25 jc.addCommand(new CmdOne()); 26 jc.addCommand(new CmdTwo()); 27 28 if (withDefault) { 29 // First check if a command was given, when not prepend default 30 // command (--cmd-two") 31 // In version up to 1.23 JCommander throws an Exception in this 32 // line, 33 // which might be incorrect, at least its not reasonable if the 34 // method 35 // is named "WithoutValidation". 36 jc.parseWithoutValidation(args); 37 if (jc.getParsedCommand() == null) { 38 LinkedList<String> newArgs = new LinkedList<>(); 39 newArgs.add("--cmd-two"); 40 newArgs.addAll(Arrays.asList(args)); 41 jc.parse(newArgs.toArray(new String[0])); 42 } 43 } else { 44 jc.parse(args); 45 } 46 return jc.getParsedCommand(); 47 } 48 49 @DataProvider testData()50 public Object[][] testData() { 51 return new Object[][] { 52 new Object[] { "--cmd-one", false, new String[] { "--cmd-one" } }, 53 new Object[] { "--cmd-two", false, new String[] { "--cmd-two" } }, 54 new Object[] { "--cmd-two", false, 55 new String[] { "--cmd-two", "param1", "param2" } }, 56 // This is the relevant test case to test default commands 57 new Object[] { "--cmd-two", true, 58 new String[] { "param1", "param2" } } }; 59 } 60 61 @Test(dataProvider = "testData") testArgsWithoutDefaultCmd(String expected, boolean requireDefault, String[] args)62 public void testArgsWithoutDefaultCmd(String expected, 63 boolean requireDefault, String[] args) { 64 if (!requireDefault) { 65 Assert.assertEquals(parseArgs(false, args), expected); 66 } 67 } 68 69 @Test(dataProvider = "testData", expectedExceptions = MissingCommandException.class) testArgsWithoutDefaultCmdFail(String expected, boolean requireDefault, String[] args)70 public void testArgsWithoutDefaultCmdFail(String expected, 71 boolean requireDefault, String[] args) { 72 if (requireDefault) { 73 try { 74 parseArgs(false, args); 75 } catch (MissingCommandException e) { 76 Assert.assertEquals(e.getUnknownCommand(), args[0]); 77 throw e; 78 } 79 } else { 80 throw new MissingCommandException("irrelevant test case"); 81 } 82 } 83 84 // We do not expect a MissingCommandException! 85 @Test(dataProvider = "testData") testArgsWithDefaultCmd(String expected, boolean requireDefault, String[] args)86 public void testArgsWithDefaultCmd(String expected, boolean requireDefault, 87 String[] args) { 88 Assert.assertEquals(parseArgs(true, args), expected); 89 } 90 91 @Test testIssue244()92 public void testIssue244() throws Exception { 93 class P1 {} 94 class P2 { 95 @Parameter(names = "--hello") 96 private int test; 97 } 98 P1 p1 = new P1(); 99 P2 p2 = new P2(); 100 JCommander j = new JCommander(p1); 101 j.addCommand("wonderful", p2); 102 j.setAllowAbbreviatedOptions(true); 103 j.parse("wond", "--he", "47"); 104 Assert.assertEquals("wonderful", j.getParsedCommand()); 105 Assert.assertEquals(47, p2.test); 106 } 107 } 108