01: /*
02: * ArgumentParserTest.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.util;
13:
14: import java.util.Collection;
15: import junit.framework.*;
16:
17: /**
18: *
19: * @author support@sql-workbench.net
20: */
21: public class ArgumentParserTest extends TestCase {
22: public ArgumentParserTest(String testname) {
23: super (testname);
24: }
25:
26: public void testAllowedValues() {
27: ArgumentParser arg = new ArgumentParser();
28: arg
29: .addArgument("type", StringUtil
30: .stringToList("text,xml,sql"));
31: Collection c = arg.getAllowedValues("type");
32: assertTrue(c.contains("text"));
33: assertTrue(c.contains("TEXT"));
34: assertTrue(c.contains("Text"));
35: }
36:
37: public void testParser() {
38: String cmdline = "-delimiter=',' -otherbool=1 -nosettings -table='\"MIND\"' -boolarg=true -profile='test-prof' -script=bla.sql -arg2=\"with space and quote\"";
39: ArgumentParser arg = new ArgumentParser();
40: arg.addArgument("profile");
41: arg.addArgument("script");
42: arg.addArgument("arg2");
43: arg.addArgument("nosettings");
44: arg.addArgument("boolarg");
45: arg.addArgument("otherbool");
46: arg.addArgument("table");
47: arg.addArgument("delimiter");
48: arg.parse(cmdline);
49: assertEquals("profile not retrieved", "test-prof", arg
50: .getValue("profile"));
51: assertEquals("script not retrieved", "bla.sql", arg
52: .getValue("script"));
53: assertEquals("double quoted value not retrieved",
54: "with space and quote", arg.getValue("arg2"));
55: assertEquals("argument without parameter not found", true, arg
56: .isArgPresent("noSettings"));
57: assertEquals("boolean argument not retrieved", true, arg
58: .getBoolean("boolArg", false));
59: assertEquals("numeric boolean argument not retrieved", true,
60: arg.getBoolean("otherBool", false));
61: assertEquals("Embedded quotes were removed", "\"MIND\"", arg
62: .getValue("TABLE"));
63: assertEquals("Delimiter not retrieved", ",", arg
64: .getValue("delimiter"));
65:
66: arg = new ArgumentParser();
67: arg.addArgument("delimiter");
68: arg.addArgument("altdelimiter");
69:
70: cmdline = "-delimiter='\" \"' -altdelimiter='/;nl'";
71: arg.parse(cmdline);
72:
73: assertEquals("Blank as delimiter not retrieved", " ",
74: StringUtil.trimQuotes(arg.getValue("delimiter")));
75: assertEquals("Wrong altDelimiter", "/;nl", arg
76: .getValue("altDelimiter"));
77: }
78:
79: }
|