001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common.toolspec.parser;
019:
020: import java.util.StringTokenizer;
021:
022: import org.apache.cxf.common.util.StringUtils;
023: import org.apache.cxf.tools.common.toolspec.ToolSpec;
024: import org.junit.Assert;
025: import org.junit.Before;
026: import org.junit.Test;
027:
028: public class CommandLineParserTest extends Assert {
029: private CommandLineParser parser;
030:
031: @Before
032: public void setUp() throws Exception {
033: String tsSource = "/org/apache/cxf/tools/common/toolspec/parser/resources/testtool.xml";
034: ToolSpec toolspec = new ToolSpec(getClass()
035: .getResourceAsStream(tsSource), true);
036:
037: parser = new CommandLineParser(toolspec);
038: }
039:
040: @Test
041: public void testValidArguments() throws Exception {
042: String[] args = new String[] { "-r", "-n", "test", "arg1" };
043: CommandDocument result = parser.parseArguments(args);
044:
045: assertEquals("testValidArguments Failed", "test", result
046: .getParameter("namespace"));
047: }
048:
049: @Test
050: public void testInvalidArgumentValue() throws Exception {
051: try {
052: String[] args = new String[] { "-n", "test@", "arg1" };
053: parser.parseArguments(args);
054: fail("testInvalidArgumentValue failed");
055: } catch (BadUsageException ex) {
056: Object[] errors = ex.getErrors().toArray();
057: assertEquals("testInvalidArgumentValue failed", 1,
058: errors.length);
059: CommandLineError error = (CommandLineError) errors[0];
060: assertTrue("Expected InvalidArgumentValue error",
061: error instanceof ErrorVisitor.UserError);
062: ErrorVisitor.UserError userError = (ErrorVisitor.UserError) error;
063: assertEquals("Invalid argument value message incorrect",
064: "-n has invalid character!", userError.toString());
065: }
066: }
067:
068: @Test
069: public void testValidArgumentEnumValue() throws Exception {
070: String[] args = new String[] { "-r", "-e", "true", "arg1" };
071: CommandDocument result = parser.parseArguments(args);
072: assertEquals("testValidArguments Failed", "true", result
073: .getParameter("enum"));
074: }
075:
076: @Test
077: public void testInvalidArgumentEnumValue() throws Exception {
078: try {
079: String[] args = new String[] { "-e", "wrongvalue" };
080: parser.parseArguments(args);
081: fail("testInvalidArgumentEnumValue failed");
082: } catch (BadUsageException ex) {
083: Object[] errors = ex.getErrors().toArray();
084: assertEquals("testInvalidArgumentEnumValu failed", 1,
085: errors.length);
086: CommandLineError error = (CommandLineError) errors[0];
087: assertTrue("Expected InvalidArgumentEnumValu error",
088: error instanceof ErrorVisitor.UserError);
089: ErrorVisitor.UserError userError = (ErrorVisitor.UserError) error;
090: assertEquals(
091: "Invalid enum argument value message incorrect",
092: "-e wrongvalue not in the enumeration value list!",
093: userError.toString());
094: }
095: }
096:
097: @Test
098: public void testValidMixedArguments() throws Exception {
099: String[] args = new String[] { "-v", "-r", "-n", "test", "arg1" };
100: CommandDocument result = parser.parseArguments(args);
101:
102: assertEquals("testValidMissedArguments Failed", "test", result
103: .getParameter("namespace"));
104: }
105:
106: @Test
107: public void testInvalidOption() {
108: try {
109: String[] args = new String[] { "-n", "-r", "arg1" };
110: parser.parseArguments(args);
111:
112: fail("testInvalidOption failed");
113: } catch (BadUsageException ex) {
114: Object[] errors = ex.getErrors().toArray();
115:
116: assertEquals("testInvalidOption failed", 1, errors.length);
117: CommandLineError error = (CommandLineError) errors[0];
118:
119: assertTrue("Expected InvalidOption error",
120: error instanceof ErrorVisitor.InvalidOption);
121: ErrorVisitor.InvalidOption option = (ErrorVisitor.InvalidOption) error;
122:
123: assertEquals("Invalid option incorrect", "-n", option
124: .getOptionSwitch());
125: assertEquals(
126: "Invalid option message incorrect",
127: "Invalid option: -n is missing its associated argument",
128: option.toString());
129: }
130: }
131:
132: @Test
133: public void testMissingOption() {
134: try {
135: String[] args = new String[] { "-n", "test", "arg1" };
136: parser.parseArguments(args);
137: fail("testMissingOption failed");
138: } catch (BadUsageException ex) {
139: Object[] errors = ex.getErrors().toArray();
140:
141: assertEquals("testInvalidOption failed", 1, errors.length);
142: CommandLineError error = (CommandLineError) errors[0];
143:
144: assertTrue("Expected MissingOption error",
145: error instanceof ErrorVisitor.MissingOption);
146: ErrorVisitor.MissingOption option = (ErrorVisitor.MissingOption) error;
147:
148: assertEquals("Missing option incorrect", "r", option
149: .getOptionSwitch());
150: }
151: }
152:
153: @Test
154: public void testMissingArgument() {
155: try {
156: String[] args = new String[] { "-n", "test", "-r" };
157: parser.parseArguments(args);
158: fail("testMissingArgument failed");
159: } catch (BadUsageException ex) {
160: Object[] errors = ex.getErrors().toArray();
161:
162: assertEquals("testInvalidOption failed", 1, errors.length);
163: CommandLineError error = (CommandLineError) errors[0];
164:
165: assertTrue("Expected MissingArgument error",
166: error instanceof ErrorVisitor.MissingArgument);
167: ErrorVisitor.MissingArgument arg = (ErrorVisitor.MissingArgument) error;
168:
169: assertEquals("MissingArgument incorrect", "wsdlurl", arg
170: .getArgument());
171: }
172: }
173:
174: @Test
175: public void testDuplicateArgument() {
176: try {
177: String[] args = new String[] { "-n", "test", "-r", "arg1",
178: "arg2" };
179: parser.parseArguments(args);
180: fail("testUnexpectedArgument failed");
181: } catch (BadUsageException ex) {
182: Object[] errors = ex.getErrors().toArray();
183: assertEquals("testInvalidOption failed", 1, errors.length);
184: CommandLineError error = (CommandLineError) errors[0];
185: assertTrue("Expected UnexpectedArgument error",
186: error instanceof ErrorVisitor.UnexpectedArgument);
187: }
188: }
189:
190: @Test
191: public void testUnexpectedOption() {
192: try {
193: String[] args = new String[] { "-n", "test", "-r",
194: "-unknown" };
195: parser.parseArguments(args);
196: fail("testUnexpectedOption failed");
197: } catch (BadUsageException ex) {
198: Object[] errors = ex.getErrors().toArray();
199:
200: assertEquals("testInvalidOption failed", 1, errors.length);
201: CommandLineError error = (CommandLineError) errors[0];
202:
203: assertTrue("Expected UnexpectedOption error",
204: error instanceof ErrorVisitor.UnexpectedOption);
205: ErrorVisitor.UnexpectedOption option = (ErrorVisitor.UnexpectedOption) error;
206:
207: assertEquals("UnexpectedOption incorrect", "-unknown",
208: option.getOptionSwitch());
209: }
210: }
211:
212: @Test
213: public void testInvalidPackageName() {
214:
215: try {
216: String[] args = new String[] { "-p", "/test", "arg1" };
217: parser.parseArguments(args);
218: fail("testInvalidPackageName failed");
219: } catch (BadUsageException ex) {
220: Object[] errors = ex.getErrors().toArray();
221: assertEquals("testInvalidPackageName failed", 1,
222: errors.length);
223: CommandLineError error = (CommandLineError) errors[0];
224: assertTrue("Expected InvalidArgumentValue error",
225: error instanceof ErrorVisitor.UserError);
226: ErrorVisitor.UserError userError = (ErrorVisitor.UserError) error;
227: assertEquals("Invalid argument value message incorrect",
228: "-p has invalid character!", userError.toString());
229: }
230:
231: }
232:
233: @Test
234: public void testvalidPackageName() throws Exception {
235:
236: String[] args = new String[] { "-p",
237: "http://www.iona.com/hello_world_soap_http=com.iona",
238: "-r", "arg1" };
239: CommandDocument result = parser.parseArguments(args);
240: assertEquals("testValidPackageName Failed",
241: "http://www.iona.com/hello_world_soap_http=com.iona",
242: result.getParameter("packagename"));
243:
244: }
245:
246: @Test
247: public void testUsage() throws Exception {
248: String usage = "[ -n <C++ Namespace> ] [ -impl ] [ -e <Enum Value> ] -r "
249: + "[ -p <[wsdl namespace =]Package Name> ]* [ -? ] [ -v ] <wsdlurl> ";
250: String pUsage = parser.getUsage();
251:
252: if (isQuolifiedVersion()) {
253: assertEquals(
254: "This test failed in the xerces version above 2.7.1 or the version with JDK ",
255: usage, pUsage);
256: } else {
257: usage = "[ -n <C++ Namespace> ] [ -impl ] [ -e <Enum Value> ] -r "
258: + "-p <[wsdl namespace =]Package Name>* [ -? ] [ -v ] <wsdlurl>";
259: assertEquals(
260: "This test failed in the xerces version below 2.7.1",
261: usage.trim(), pUsage.trim());
262: }
263: }
264:
265: private boolean isQuolifiedVersion() {
266: try {
267: Class<?> c = Class
268: .forName("org.apache.xerces.impl.Version");
269: Object o = c.newInstance();
270: String v = (String) c.getMethod("getVersion").invoke(o);
271: float vn = Float.parseFloat(StringUtils.getFirstFound(v,
272: "(\\d+.\\d+)"));
273: return vn >= 2.7;
274: } catch (Exception e) {
275: // ignore
276: }
277: return true;
278: }
279:
280: @Test
281: public void testDetailedUsage() throws Exception {
282: String specialItem = "[ -p <[wsdl namespace =]Package Name> ]*";
283: if (!isQuolifiedVersion()) {
284: specialItem = "-p <[wsdl namespace =]Package Name>*";
285: }
286:
287: String[] expected = new String[] {
288: "[ -n <C++ Namespace> ]",
289: "Namespace",
290: "[ -impl ]",
291: "impl",
292: "[ -e <Enum Value> ]",
293: "enum",
294: "-r",
295: "required",
296: specialItem,
297: "The java package name to use for the generated code."
298: + "Also, optionally specify the wsdl namespace mapping to "
299: + "a particular java packagename.", "[ -? ]",
300: "help", "[ -v ]", "version", "<wsdlurl>",
301: "WSDL/SCHEMA URL" };
302:
303: int index = 0;
304: String lineSeparator = System.getProperty("line.separator");
305: StringTokenizer st1 = new StringTokenizer(parser
306: .getDetailedUsage(), lineSeparator);
307: while (st1.hasMoreTokens()) {
308: assertEquals("Failed at line " + index, expected[index++],
309: st1.nextToken().toString().trim());
310: }
311: }
312:
313: @Test
314: public void testOtherMethods() throws Exception {
315: String tsSource = "/org/apache/cxf/tools/common/toolspec/parser/resources/testtool.xml";
316: ToolSpec toolspec = new ToolSpec(getClass()
317: .getResourceAsStream(tsSource), false);
318: CommandLineParser commandLineParser = new CommandLineParser(
319: null);
320: commandLineParser.setToolSpec(toolspec);
321: CommandDocument commandDocument = commandLineParser
322: .parseArguments("-r unknown");
323: assertTrue(commandDocument != null);
324: }
325:
326: @Test
327: public void testGetDetailedUsage() {
328: assertTrue("Namespace".equals(parser
329: .getDetailedUsage("namespace")));
330: }
331:
332: }
|