1 23 24 package com.sun.enterprise.cli.framework; 25 26 32 import junit.framework.*; 33 import junit.textui.TestRunner; 34 import java.util.Vector ; 35 import java.util.Arrays ; 36 import java.util.HashMap ; 37 38 43 44 49 50 public class CommandLineParserTest extends TestCase { 51 public void testInsertOperands() throws Exception { 52 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "-t=false", "operand"}; 53 CommandLineParser clp = new CLP(args, vc1); 54 HashMap options = clp.getOptionsList(); 55 assertTrue(clp.getOperands().contains("operand")); 56 } 57 58 public void testOptionWithEqualsSign() throws Exception { 59 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "--terse=false"}; 60 CommandLineParser clp = new CLP(args, vc1); 61 HashMap options = clp.getOptionsList(); 62 assertEquals("short option for terse if true", "false", (String )options.get("terse")); 63 } 64 65 66 public void testShortOptionsGroup() throws Exception { 67 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "-ti"}; 68 CommandLineParser clp = new CLP(args, vc1); 69 HashMap options = clp.getOptionsList(); 70 assertEquals("short option for terse if true", "true", (String )options.get("terse")); 71 assertEquals("short option for interactive is false", "false", (String )options.get("interactive")); 72 } 73 74 75 public void testToString() throws Exception { 76 CommandLineParser clp = new CLP(); 77 assertEquals("\n**********\nname = null\nOptions = {}\nOperands = []\n**********\n", clp.toString()); 78 } 79 80 public void testLocalizedString() throws Exception { 81 CommandLineParser clp = new CLP(); 82 assertEquals("Key not found (this key)", clp.getLocalizedString("this key", (Object []) null)); 83 } 84 85 86 public void testNullCommand() throws Exception { 87 CommandLineParser clp = new CLP(vc1); 88 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin"}; 89 clp.parseCommandLine(args); 90 HashMap options = clp.getOptionsList(); 91 assertEquals("terse option is by default is false", "false", (String )options.get("terse")); 92 assertEquals("interactive option is by default is true", "true", (String )options.get("interactive")); 93 assertTrue(clp.getOperands().isEmpty()); 94 } 95 96 public void testSimpleConstruction() throws HelpException { 97 CommandLineParser clp = new CLP(); 98 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin"}; 99 try { 100 clp.parseCommandLine(args); 101 fail("Expected to get a CommandValidationException saying that there was no command"); 102 } 103 catch (CommandValidationException cve){ 104 assertEquals("CLI001 Invalid Command, samplecommand.", cve.getMessage()); 105 } 106 } 107 108 public void testDefaultBooleanOptions() throws Exception { 110 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin"}; 111 CommandLineParser clp = new CLP(args, vc1); 112 HashMap options = clp.getOptionsList(); 113 assertEquals("terse option is by default is false", "false", (String )options.get("terse")); 114 assertEquals("interactive option is by default is true", "true", (String )options.get("interactive")); 115 } 116 117 118 public void testShortBooleanOptions() throws Exception { 120 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "-i", "-t"}; 121 CommandLineParser clp = new CLP(args, vc1); 122 HashMap options = clp.getOptionsList(); 123 assertEquals("short option for terse if true", "true", (String )options.get("terse")); 124 assertEquals("short option for interactive is false", "false", (String )options.get("interactive")); 125 } 126 127 public void testLongBooleanOptions() throws Exception { 129 String [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "--interactive", "--terse"}; 130 CommandLineParser clp = new CLP(args, vc1); 131 HashMap options = clp.getOptionsList(); 132 assertEquals("long option for terse if true", "true", (String )options.get("terse")); 133 assertEquals("long option for interactive is true", "true", (String )options.get("interactive")); 134 } 135 136 137 public CommandLineParserTest(String name){ 138 super(name); 139 } 140 141 ValidCommand vc1; 142 143 protected void setUp() { 144 ValidOption vo1 = new ValidOption("user", "string", 1, ""); 145 ValidOption vo2 = new ValidOption("password", "string", 1, ""); 146 ValidOption vo3 = new ValidOption("host", "string", 1, ""); 147 ValidOption vo4 = new ValidOption("port", "string", 1, ""); 148 ValidOption vo5 = new ValidOption("interactive", "boolean", 3, "true"); 149 vo5.setShortName("i"); 150 ValidOption vo6 = new ValidOption("terse", "boolean", 3, "false"); 151 vo6.setShortName("t"); 152 ValidOption[] validOptions = new ValidOption[] {vo3, vo4, vo6}; 153 ValidOption[] requiredOptions = new ValidOption[] {vo1, vo2, vo5}; 154 vc1 = new ValidCommand("sampleCommand", 155 "1", 156 new Vector (Arrays.asList(validOptions)), 157 new Vector (Arrays.asList(requiredOptions)), 158 new Vector (), 159 "sampleCommand"); 160 } 161 162 163 164 protected void tearDown() { 165 } 166 167 private void nyi(){ 168 fail("Not Yet Implemented"); 169 } 170 171 public static Test suite(){ 172 TestSuite suite = new TestSuite(CommandLineParserTest.class); 173 return suite; 174 } 175 176 public static void main(String args[]) throws Exception { 177 final TestRunner runner= new TestRunner(); 178 final TestResult result = runner.doRun(CommandLineParserTest.suite(), false); 179 System.exit(result.errorCount() + result.failureCount()); 180 } 181 182 private class CLP extends CommandLineParser 183 { 184 CLP(ValidCommand vc){ 185 super(vc); 186 } 187 188 CLP(){ 189 super(); 190 } 191 192 CLP(String [] s, ValidCommand vc) throws CommandValidationException, HelpException { 193 super(s, vc); 194 } 195 196 HashMap getEnvironment(){ 197 return new HashMap (); 198 } 199 } 200 201 202 } 203 204 | Popular Tags |