001: package de.java2html.commandline.test;
002:
003: import de.java2html.commandline.CommandlineArguments;
004: import de.java2html.commandline.IllegalCommandlineParametersException;
005: import de.java2html.test.BasicTestCase;
006:
007: /**
008: * @author Markus Gebhard
009: */
010: public class CommandlineArgumentsTest extends BasicTestCase {
011:
012: public void testIsFlagSet()
013: throws IllegalCommandlineParametersException {
014: CommandlineArguments emptyArguments = new CommandlineArguments(
015: new String[] {});
016: assertFalse(emptyArguments.isFlagSet("flag"));
017:
018: CommandlineArguments arguments = new CommandlineArguments(
019: new String[] { "-parameter", "value", "-flag" });
020: assertTrue(arguments.isFlagSet("flag"));
021: assertTrue(arguments.isFlagSet("FLAG"));
022: assertFalse(arguments.isFlagSet("-flag"));
023: assertFalse(arguments.isFlagSet("value"));
024: assertFalse(arguments.isFlagSet("parameter"));
025: }
026:
027: public void testIsParameterSet()
028: throws IllegalCommandlineParametersException {
029: CommandlineArguments emptyArguments = new CommandlineArguments(
030: new String[] {});
031: assertFalse(emptyArguments.isParameterSet("flag"));
032:
033: CommandlineArguments arguments = new CommandlineArguments(
034: new String[] { "-parameter", "value", "-flag" });
035: assertFalse(arguments.isParameterSet("flag"));
036: assertFalse(arguments.isParameterSet("FLAG"));
037: assertFalse(arguments.isParameterSet("-flag"));
038: assertFalse(arguments.isParameterSet("value"));
039: assertTrue(arguments.isParameterSet("parameter"));
040: assertTrue(arguments.isParameterSet("pArAmEtEr"));
041: }
042:
043: public void testNumbersAreNotFlags()
044: throws IllegalCommandlineParametersException {
045: CommandlineArguments arguments = new CommandlineArguments(
046: new String[] { "-flag", "-10" });
047: assertFalse(arguments.isFlagSet("10"));
048: assertFalse(arguments.isFlagSet("flag"));
049: assertTrue(arguments.isParameterSet("flag"));
050: }
051:
052: public void testDuplicateFlagDefinitionThrowsException() {
053: assertThrowsException(
054: IllegalCommandlineParametersException.class,
055: new IBlock() {
056: public void execute() throws Exception {
057: new CommandlineArguments(new String[] {
058: "-flag1", "-flag1" });
059: }
060: });
061: }
062:
063: public void testDuplicateParameterDefinitionThrowsException() {
064: assertThrowsException(
065: IllegalCommandlineParametersException.class,
066: new IBlock() {
067: public void execute() throws Exception {
068: new CommandlineArguments(
069: new String[] { "-flag1", "value1",
070: "-flag1", "value1" });
071: }
072: });
073: }
074:
075: public void testDuplicateMixedDefinitionThrowsException() {
076: assertThrowsException(
077: IllegalCommandlineParametersException.class,
078: new IBlock() {
079: public void execute() throws Exception {
080: new CommandlineArguments(new String[] {
081: "-flag1", "-flag1", "value" });
082: }
083: });
084: }
085:
086: public void testGetArgumentStringValue()
087: throws IllegalCommandlineParametersException {
088: assertEquals("value", new CommandlineArguments(new String[] {
089: "-flag1", "value" }).getParameterStringValue("flag1"));
090: assertEquals(null, new CommandlineArguments(new String[] {
091: "-flag1", "-flag2" }).getParameterStringValue("flag1"));
092: }
093:
094: public void testGetRequiredArgumentStringValue()
095: throws IllegalCommandlineParametersException {
096: assertEquals("value", new CommandlineArguments(new String[] {
097: "-flag1", "value" })
098: .getRequiredParameterStringValue(("flag1")));
099: assertThrowsException(
100: IllegalCommandlineParametersException.class,
101: new IBlock() {
102: public void execute() throws Exception {
103: new CommandlineArguments(
104: new String[] { "-flag1" })
105: .getRequiredParameterStringValue("flag1");
106: }
107: });
108: }
109:
110: public void testGetArgumentStringValueWithDefault()
111: throws IllegalCommandlineParametersException {
112: assertEquals("default", new CommandlineArguments(new String[] {
113: "-flag1", "value" }).getParameterStringValue(
114: "undefinedFlag", "default"));
115: assertEquals("value", new CommandlineArguments(new String[] {
116: "-definedflag", "value" }).getParameterStringValue(
117: "definedflag", "default"));
118: }
119:
120: public void testGetArgumentIntValueWithDefault()
121: throws IllegalCommandlineParametersException {
122: assertEquals(42, new CommandlineArguments(new String[] {
123: "-flag1", "42" }).getParameterPositiveIntValue("flag1",
124: 1));
125: assertEquals(1, new CommandlineArguments(new String[] {})
126: .getParameterPositiveIntValue("flag1", 1));
127: }
128:
129: public void testGetArgumentIntValueWithIllegalValue() {
130: assertThrowsException(
131: IllegalCommandlineParametersException.class,
132: new IBlock() {
133: public void execute() throws Exception {
134: new CommandlineArguments(new String[] {
135: "-flag1", "abc" })
136: .getParameterPositiveIntValue("flag1",
137: 1);
138: }
139: });
140: assertThrowsException(
141: IllegalCommandlineParametersException.class,
142: new IBlock() {
143: public void execute() throws Exception {
144: new CommandlineArguments(new String[] {
145: "-flag1", "-10" })
146: .getParameterPositiveIntValue("flag1",
147: 1);
148: }
149: });
150: }
151:
152: public void testUnsupportedParameterThrowsException()
153: throws IllegalCommandlineParametersException {
154: new CommandlineArguments(new String[] { "-flag1", "value1",
155: "-flag2" })
156: .assertContainsNoUnsupportedParameters(new String[] { "flag1" });
157: new CommandlineArguments(new String[] { "-flag1", "value1",
158: "-flag2" })
159: .assertContainsNoUnsupportedParameters(new String[] { "FLag1" });
160:
161: assertThrowsException(
162: IllegalCommandlineParametersException.class,
163: new IBlock() {
164: public void execute() throws Exception {
165: new CommandlineArguments(
166: new String[] { "-flag1", "value1",
167: "-flag2", "value2" })
168: .assertContainsNoUnsupportedParameters(new String[] { "flag1" });
169: }
170: });
171:
172: assertThrowsException(
173: IllegalCommandlineParametersException.class,
174: new IBlock() {
175: public void execute() throws Exception {
176: new CommandlineArguments(new String[] {
177: "-flag1", "value1", "-flag2" })
178: .assertContainsNoUnsupportedParameters(new String[] { "flag2" });
179: }
180: });
181:
182: assertThrowsException(
183: IllegalCommandlineParametersException.class,
184: new IBlock() {
185: public void execute() throws Exception {
186: new CommandlineArguments(new String[] {
187: "-flag", "value" })
188: .assertContainsNoUnsupportedParameters(new String[0]);
189: }
190: });
191: }
192:
193: public void testUnsupportedFlagThrowsException()
194: throws IllegalCommandlineParametersException {
195: new CommandlineArguments(new String[] { "-flag1", "value1",
196: "-flag2" })
197: .assertContainsNoUnsupportedFlags(new String[] {
198: "flag1", "flag2" });
199: new CommandlineArguments(new String[] { "-flag1", "value1",
200: "-flag2" })
201: .assertContainsNoUnsupportedFlags(new String[] {
202: "FLag1", "FLag2" });
203:
204: assertThrowsException(
205: IllegalCommandlineParametersException.class,
206: new IBlock() {
207: public void execute() throws Exception {
208: new CommandlineArguments(new String[] {
209: "-flag1", "-flag2" })
210: .assertContainsNoUnsupportedFlags(new String[] { "flag1" });
211: }
212: });
213:
214: assertThrowsException(
215: IllegalCommandlineParametersException.class,
216: new IBlock() {
217: public void execute() throws Exception {
218: new CommandlineArguments(new String[] {
219: "-flag1", "-flag2" })
220: .assertContainsNoUnsupportedFlags(new String[] { "flag2" });
221: }
222: });
223:
224: assertThrowsException(
225: IllegalCommandlineParametersException.class,
226: new IBlock() {
227: public void execute() throws Exception {
228: new CommandlineArguments(
229: new String[] { "-flag" })
230: .assertContainsNoUnsupportedFlags(new String[0]);
231: }
232: });
233: }
234: }
|