1 package org.objectweb.celtix.tools.common.toolspec.parser; 2 3 import junit.framework.TestCase; 4 5 import org.objectweb.celtix.tools.common.toolspec.ToolSpec; 6 7 public class CommandLineParserTest extends TestCase { 8 private CommandLineParser parser; 9 10 public CommandLineParserTest(String name) { 11 super(name); 12 } 13 14 public static void main(String [] args) { 15 junit.textui.TestRunner.run(CommandLineParserTest.class); 16 } 17 18 public void setUp() throws Exception { 19 String tsSource = "/org/objectweb/celtix/tools/common/toolspec/parser/resources/testtool.xml"; 20 ToolSpec toolspec = new ToolSpec(getClass().getResourceAsStream(tsSource), true); 21 22 parser = new CommandLineParser(toolspec); 23 } 24 25 public void testValidArguments() throws Exception { 26 String [] args = new String [] {"-r", "-n", "test", "arg1"}; 27 CommandDocument result = parser.parseArguments(args); 28 29 assertEquals("testValidArguments Failed", "test", result.getParameter("namespace")); 30 } 31 32 public void testInvalidArgumentValue() throws Exception { 33 try { 34 String [] args = new String [] {"-n", "test@", "arg1"}; 35 parser.parseArguments(args); 36 fail("testInvalidArgumentValue failed"); 37 } catch (BadUsageException ex) { 38 Object [] errors = ex.getErrors().toArray(); 39 assertEquals("testInvalidArgumentValue failed", 1, errors.length); 40 CommandLineError error = (CommandLineError)errors[0]; 41 assertTrue("Expected InvalidArgumentValue error", error instanceof ErrorVisitor.UserError); 42 ErrorVisitor.UserError userError = (ErrorVisitor.UserError)error; 43 assertEquals("Invalid argument value message incorrect", "-n has invalid character!", userError 44 .toString()); 45 } 46 } 47 48 public void testValidArgumentEnumValue() throws Exception { 49 String [] args = new String [] {"-r", "-e", "true", "arg1"}; 50 CommandDocument result = parser.parseArguments(args); 51 assertEquals("testValidArguments Failed", "true", result.getParameter("enum")); 52 } 53 54 public void testInvalidArgumentEnumValue() throws Exception { 55 try { 56 String [] args = new String [] {"-e", "wrongvalue"}; 57 parser.parseArguments(args); 58 fail("testInvalidArgumentEnumValue failed"); 59 } catch (BadUsageException ex) { 60 Object [] errors = ex.getErrors().toArray(); 61 assertEquals("testInvalidArgumentEnumValu failed", 1, errors.length); 62 CommandLineError error = (CommandLineError)errors[0]; 63 assertTrue("Expected InvalidArgumentEnumValu error", error instanceof ErrorVisitor.UserError); 64 ErrorVisitor.UserError userError = (ErrorVisitor.UserError)error; 65 assertEquals("Invalid enum argument value message incorrect", 66 "-e wrongvalue not in the enumeration value list!", 67 userError.toString()); 68 } 69 } 70 71 public void testValidMixedArguments() throws Exception { 72 String [] args = new String [] {"-v", "-r", "-n", "test", "arg1"}; 73 CommandDocument result = parser.parseArguments(args); 74 75 assertEquals("testValidMissedArguments Failed", "test", result.getParameter("namespace")); 76 } 77 78 public void testInvalidOption() { 79 try { 80 String [] args = new String [] {"-n", "-r", "arg1"}; 81 parser.parseArguments(args); 82 83 fail("testInvalidOption failed"); 84 } catch (BadUsageException ex) { 85 Object [] errors = ex.getErrors().toArray(); 86 87 assertEquals("testInvalidOption failed", 1, errors.length); 88 CommandLineError error = (CommandLineError)errors[0]; 89 90 assertTrue("Expected InvalidOption error", error instanceof ErrorVisitor.InvalidOption); 91 ErrorVisitor.InvalidOption option = (ErrorVisitor.InvalidOption)error; 92 93 assertEquals("Invalid option incorrect", "-n", option.getOptionSwitch()); 94 assertEquals("Invalid option message incorrect", 95 "Invalid option: -n is missing its associated argument", option.toString()); 96 } 97 } 98 99 public void testMissingOption() { 100 try { 101 String [] args = new String [] {"-n", "test", "arg1"}; 102 parser.parseArguments(args); 103 fail("testMissingOption failed"); 104 } catch (BadUsageException ex) { 105 Object [] errors = ex.getErrors().toArray(); 106 107 assertEquals("testInvalidOption failed", 1, errors.length); 108 CommandLineError error = (CommandLineError)errors[0]; 109 110 assertTrue("Expected MissingOption error", error instanceof ErrorVisitor.MissingOption); 111 ErrorVisitor.MissingOption option = (ErrorVisitor.MissingOption)error; 112 113 assertEquals("Missing option incorrect", "r", option.getOptionSwitch()); 114 } 115 } 116 117 public void testMissingArgument() { 118 try { 119 String [] args = new String [] {"-n", "test", "-r"}; 120 parser.parseArguments(args); 121 fail("testMissingArgument failed"); 122 } catch (BadUsageException ex) { 123 Object [] errors = ex.getErrors().toArray(); 124 125 assertEquals("testInvalidOption failed", 1, errors.length); 126 CommandLineError error = (CommandLineError)errors[0]; 127 128 assertTrue("Expected MissingArgument error", error instanceof ErrorVisitor.MissingArgument); 129 ErrorVisitor.MissingArgument arg = (ErrorVisitor.MissingArgument)error; 130 131 assertEquals("MissingArgument incorrect", "wsdlurl", arg.getArgument()); 132 } 133 } 134 135 public void testDuplicateArgument() { 136 try { 137 String [] args = new String [] {"-n", "test", "-r", "arg1", "arg2"}; 138 parser.parseArguments(args); 139 fail("testUnexpectedArgument failed"); 140 } catch (BadUsageException ex) { 141 Object [] errors = ex.getErrors().toArray(); 142 assertEquals("testInvalidOption failed", 1, errors.length); 143 CommandLineError error = (CommandLineError)errors[0]; 144 assertTrue("Expected UnexpectedArgument error", error instanceof ErrorVisitor.UnexpectedArgument); 145 } 146 } 147 148 public void testUnexpectedOption() { 149 try { 150 String [] args = new String [] {"-n", "test", "-r", "-unknown"}; 151 parser.parseArguments(args); 152 fail("testUnexpectedOption failed"); 153 } catch (BadUsageException ex) { 154 Object [] errors = ex.getErrors().toArray(); 155 156 assertEquals("testInvalidOption failed", 1, errors.length); 157 CommandLineError error = (CommandLineError)errors[0]; 158 159 assertTrue("Expected UnexpectedOption error", error instanceof ErrorVisitor.UnexpectedOption); 160 ErrorVisitor.UnexpectedOption option = (ErrorVisitor.UnexpectedOption)error; 161 162 assertEquals("UnexpectedOption incorrect", "-unknown", option.getOptionSwitch()); 163 } 164 } 165 166 167 public void testInvalidPackageName() { 168 169 try { 170 String [] args = new String []{ 171 "-p", "/test", "arg1" 172 }; 173 parser.parseArguments(args); 174 fail("testInvalidPackageName failed"); 175 } catch (BadUsageException ex) { 176 Object [] errors = ex.getErrors().toArray(); 177 assertEquals("testInvalidPackageName failed", 1, errors.length); 178 CommandLineError error = (CommandLineError)errors[0]; 179 assertTrue("Expected InvalidArgumentValue error", error instanceof ErrorVisitor.UserError); 180 ErrorVisitor.UserError userError = (ErrorVisitor.UserError)error; 181 assertEquals("Invalid argument value message incorrect", 182 "-p has invalid character!", userError.toString()); 183 } 184 185 } 186 187 public void testvalidPackageName() throws Exception { 188 189 String [] args = new String []{ 190 "-p", "http://www.iona.com/hello_world_soap_http=com.iona", "-r", "arg1" 191 }; 192 CommandDocument result = parser.parseArguments(args); 193 assertEquals("testValidPackageName Failed", 194 "http://www.iona.com/hello_world_soap_http=com.iona", 195 result.getParameter("packagename")); 196 197 } 198 199 200 public void testUsage() throws Exception { 201 String usage = 202 "[ -n <C++ Namespace> ] [ -impl ] [ -e <Enum Value> ] -r " 203 + "[ -p <[wsdl namespace =]Package Name> ]* [ -? ] [ -v ] <wsdlurl> "; 204 String pUsage = parser.getUsage(); 205 assertEquals("testUsage failed", usage, pUsage); 206 } 207 208 public void testDetailedUsage() throws Exception { 209 String lineSeparator = System.getProperty("line.separator"); 210 String usage = "[ -n <C++ Namespace> ]" + lineSeparator; 211 usage += "Namespace" + lineSeparator + lineSeparator; 212 usage += "[ -impl ]" + lineSeparator; 213 usage += "impl" + lineSeparator + lineSeparator; 214 usage += "[ -e <Enum Value> ]" + lineSeparator; 215 usage += "enum" + lineSeparator + lineSeparator; 216 usage += "-r" + lineSeparator; 217 usage += "required" + lineSeparator + lineSeparator; 218 usage += "[ -p <[wsdl namespace =]Package Name> ]*" + lineSeparator; 219 usage += "The java package name to use for the generated code." 220 + "Also, optionally specify the wsdl namespace mapping to a particular java packagename." 221 + lineSeparator + lineSeparator; 222 usage += "[ -? ]" + lineSeparator; 223 usage += "help" + lineSeparator + lineSeparator; 224 usage += "[ -v ]" + lineSeparator; 225 usage += "version" + lineSeparator + lineSeparator; 226 usage += "<wsdlurl>" + lineSeparator; 227 usage += "WSDL/SCHEMA URL" + lineSeparator + lineSeparator; 228 assertEquals("testUsage failed", usage, parser.getDetailedUsage()); 229 } 230 231 public void testOtherMethods() throws Exception { 232 String tsSource = "/org/objectweb/celtix/tools/common/toolspec/parser/resources/testtool.xml"; 233 ToolSpec toolspec = new ToolSpec(getClass().getResourceAsStream(tsSource), false); 234 CommandLineParser commandLineParser = new CommandLineParser(null); 235 commandLineParser.setToolSpec(toolspec); 236 CommandDocument commandDocument = commandLineParser.parseArguments("-r unknown"); 237 assertTrue(commandDocument != null); 238 } 239 240 public void testGetDetailedUsage() { 241 assertTrue("Namespace".equals(parser.getDetailedUsage("namespace"))); 242 } 243 244 } 245 | Popular Tags |