001: package org.objectweb.celtix.tools.common.toolspec.parser;
002:
003: import junit.framework.TestCase;
004:
005: import org.objectweb.celtix.tools.common.toolspec.ToolSpec;
006:
007: public class CommandLineParserTest extends TestCase {
008: private CommandLineParser parser;
009:
010: public CommandLineParserTest(String name) {
011: super (name);
012: }
013:
014: public static void main(String[] args) {
015: junit.textui.TestRunner.run(CommandLineParserTest.class);
016: }
017:
018: public void setUp() throws Exception {
019: String tsSource = "/org/objectweb/celtix/tools/common/toolspec/parser/resources/testtool.xml";
020: ToolSpec toolspec = new ToolSpec(getClass()
021: .getResourceAsStream(tsSource), true);
022:
023: parser = new CommandLineParser(toolspec);
024: }
025:
026: public void testValidArguments() throws Exception {
027: String[] args = new String[] { "-r", "-n", "test", "arg1" };
028: CommandDocument result = parser.parseArguments(args);
029:
030: assertEquals("testValidArguments Failed", "test", result
031: .getParameter("namespace"));
032: }
033:
034: public void testInvalidArgumentValue() throws Exception {
035: try {
036: String[] args = new String[] { "-n", "test@", "arg1" };
037: parser.parseArguments(args);
038: fail("testInvalidArgumentValue failed");
039: } catch (BadUsageException ex) {
040: Object[] errors = ex.getErrors().toArray();
041: assertEquals("testInvalidArgumentValue failed", 1,
042: errors.length);
043: CommandLineError error = (CommandLineError) errors[0];
044: assertTrue("Expected InvalidArgumentValue error",
045: error instanceof ErrorVisitor.UserError);
046: ErrorVisitor.UserError userError = (ErrorVisitor.UserError) error;
047: assertEquals("Invalid argument value message incorrect",
048: "-n has invalid character!", userError.toString());
049: }
050: }
051:
052: public void testValidArgumentEnumValue() throws Exception {
053: String[] args = new String[] { "-r", "-e", "true", "arg1" };
054: CommandDocument result = parser.parseArguments(args);
055: assertEquals("testValidArguments Failed", "true", result
056: .getParameter("enum"));
057: }
058:
059: public void testInvalidArgumentEnumValue() throws Exception {
060: try {
061: String[] args = new String[] { "-e", "wrongvalue" };
062: parser.parseArguments(args);
063: fail("testInvalidArgumentEnumValue failed");
064: } catch (BadUsageException ex) {
065: Object[] errors = ex.getErrors().toArray();
066: assertEquals("testInvalidArgumentEnumValu failed", 1,
067: errors.length);
068: CommandLineError error = (CommandLineError) errors[0];
069: assertTrue("Expected InvalidArgumentEnumValu error",
070: error instanceof ErrorVisitor.UserError);
071: ErrorVisitor.UserError userError = (ErrorVisitor.UserError) error;
072: assertEquals(
073: "Invalid enum argument value message incorrect",
074: "-e wrongvalue not in the enumeration value list!",
075: userError.toString());
076: }
077: }
078:
079: public void testValidMixedArguments() throws Exception {
080: String[] args = new String[] { "-v", "-r", "-n", "test", "arg1" };
081: CommandDocument result = parser.parseArguments(args);
082:
083: assertEquals("testValidMissedArguments Failed", "test", result
084: .getParameter("namespace"));
085: }
086:
087: public void testInvalidOption() {
088: try {
089: String[] args = new String[] { "-n", "-r", "arg1" };
090: parser.parseArguments(args);
091:
092: fail("testInvalidOption failed");
093: } catch (BadUsageException ex) {
094: Object[] errors = ex.getErrors().toArray();
095:
096: assertEquals("testInvalidOption failed", 1, errors.length);
097: CommandLineError error = (CommandLineError) errors[0];
098:
099: assertTrue("Expected InvalidOption error",
100: error instanceof ErrorVisitor.InvalidOption);
101: ErrorVisitor.InvalidOption option = (ErrorVisitor.InvalidOption) error;
102:
103: assertEquals("Invalid option incorrect", "-n", option
104: .getOptionSwitch());
105: assertEquals(
106: "Invalid option message incorrect",
107: "Invalid option: -n is missing its associated argument",
108: option.toString());
109: }
110: }
111:
112: public void testMissingOption() {
113: try {
114: String[] args = new String[] { "-n", "test", "arg1" };
115: parser.parseArguments(args);
116: fail("testMissingOption failed");
117: } catch (BadUsageException ex) {
118: Object[] errors = ex.getErrors().toArray();
119:
120: assertEquals("testInvalidOption failed", 1, errors.length);
121: CommandLineError error = (CommandLineError) errors[0];
122:
123: assertTrue("Expected MissingOption error",
124: error instanceof ErrorVisitor.MissingOption);
125: ErrorVisitor.MissingOption option = (ErrorVisitor.MissingOption) error;
126:
127: assertEquals("Missing option incorrect", "r", option
128: .getOptionSwitch());
129: }
130: }
131:
132: public void testMissingArgument() {
133: try {
134: String[] args = new String[] { "-n", "test", "-r" };
135: parser.parseArguments(args);
136: fail("testMissingArgument failed");
137: } catch (BadUsageException ex) {
138: Object[] errors = ex.getErrors().toArray();
139:
140: assertEquals("testInvalidOption failed", 1, errors.length);
141: CommandLineError error = (CommandLineError) errors[0];
142:
143: assertTrue("Expected MissingArgument error",
144: error instanceof ErrorVisitor.MissingArgument);
145: ErrorVisitor.MissingArgument arg = (ErrorVisitor.MissingArgument) error;
146:
147: assertEquals("MissingArgument incorrect", "wsdlurl", arg
148: .getArgument());
149: }
150: }
151:
152: public void testDuplicateArgument() {
153: try {
154: String[] args = new String[] { "-n", "test", "-r", "arg1",
155: "arg2" };
156: parser.parseArguments(args);
157: fail("testUnexpectedArgument failed");
158: } catch (BadUsageException ex) {
159: Object[] errors = ex.getErrors().toArray();
160: assertEquals("testInvalidOption failed", 1, errors.length);
161: CommandLineError error = (CommandLineError) errors[0];
162: assertTrue("Expected UnexpectedArgument error",
163: error instanceof ErrorVisitor.UnexpectedArgument);
164: }
165: }
166:
167: public void testUnexpectedOption() {
168: try {
169: String[] args = new String[] { "-n", "test", "-r",
170: "-unknown" };
171: parser.parseArguments(args);
172: fail("testUnexpectedOption failed");
173: } catch (BadUsageException ex) {
174: Object[] errors = ex.getErrors().toArray();
175:
176: assertEquals("testInvalidOption failed", 1, errors.length);
177: CommandLineError error = (CommandLineError) errors[0];
178:
179: assertTrue("Expected UnexpectedOption error",
180: error instanceof ErrorVisitor.UnexpectedOption);
181: ErrorVisitor.UnexpectedOption option = (ErrorVisitor.UnexpectedOption) error;
182:
183: assertEquals("UnexpectedOption incorrect", "-unknown",
184: option.getOptionSwitch());
185: }
186: }
187:
188: public void testInvalidPackageName() {
189:
190: try {
191: String[] args = new String[] { "-p", "/test", "arg1" };
192: parser.parseArguments(args);
193: fail("testInvalidPackageName failed");
194: } catch (BadUsageException ex) {
195: Object[] errors = ex.getErrors().toArray();
196: assertEquals("testInvalidPackageName failed", 1,
197: errors.length);
198: CommandLineError error = (CommandLineError) errors[0];
199: assertTrue("Expected InvalidArgumentValue error",
200: error instanceof ErrorVisitor.UserError);
201: ErrorVisitor.UserError userError = (ErrorVisitor.UserError) error;
202: assertEquals("Invalid argument value message incorrect",
203: "-p has invalid character!", userError.toString());
204: }
205:
206: }
207:
208: public void testvalidPackageName() throws Exception {
209:
210: String[] args = new String[] { "-p",
211: "http://www.iona.com/hello_world_soap_http=com.iona",
212: "-r", "arg1" };
213: CommandDocument result = parser.parseArguments(args);
214: assertEquals("testValidPackageName Failed",
215: "http://www.iona.com/hello_world_soap_http=com.iona",
216: result.getParameter("packagename"));
217:
218: }
219:
220: public void testUsage() throws Exception {
221: String usage = "[ -n <C++ Namespace> ] [ -impl ] [ -e <Enum Value> ] -r "
222: + "[ -p <[wsdl namespace =]Package Name> ]* [ -? ] [ -v ] <wsdlurl> ";
223: String pUsage = parser.getUsage();
224: assertEquals("testUsage failed", usage, pUsage);
225: }
226:
227: public void testDetailedUsage() throws Exception {
228: String lineSeparator = System.getProperty("line.separator");
229: String usage = "[ -n <C++ Namespace> ]" + lineSeparator;
230: usage += "Namespace" + lineSeparator + lineSeparator;
231: usage += "[ -impl ]" + lineSeparator;
232: usage += "impl" + lineSeparator + lineSeparator;
233: usage += "[ -e <Enum Value> ]" + lineSeparator;
234: usage += "enum" + lineSeparator + lineSeparator;
235: usage += "-r" + lineSeparator;
236: usage += "required" + lineSeparator + lineSeparator;
237: usage += "[ -p <[wsdl namespace =]Package Name> ]*"
238: + lineSeparator;
239: usage += "The java package name to use for the generated code."
240: + "Also, optionally specify the wsdl namespace mapping to a particular java packagename."
241: + lineSeparator + lineSeparator;
242: usage += "[ -? ]" + lineSeparator;
243: usage += "help" + lineSeparator + lineSeparator;
244: usage += "[ -v ]" + lineSeparator;
245: usage += "version" + lineSeparator + lineSeparator;
246: usage += "<wsdlurl>" + lineSeparator;
247: usage += "WSDL/SCHEMA URL" + lineSeparator + lineSeparator;
248: assertEquals("testUsage failed", usage, parser
249: .getDetailedUsage());
250: }
251:
252: public void testOtherMethods() throws Exception {
253: String tsSource = "/org/objectweb/celtix/tools/common/toolspec/parser/resources/testtool.xml";
254: ToolSpec toolspec = new ToolSpec(getClass()
255: .getResourceAsStream(tsSource), false);
256: CommandLineParser commandLineParser = new CommandLineParser(
257: null);
258: commandLineParser.setToolSpec(toolspec);
259: CommandDocument commandDocument = commandLineParser
260: .parseArguments("-r unknown");
261: assertTrue(commandDocument != null);
262: }
263:
264: public void testGetDetailedUsage() {
265: assertTrue("Namespace".equals(parser
266: .getDetailedUsage("namespace")));
267: }
268:
269: }
|