KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > commandline > test > CommandlineArgumentsTest


1 package de.java2html.commandline.test;
2
3 import de.java2html.commandline.CommandlineArguments;
4 import de.java2html.commandline.IllegalCommandlineParametersException;
5 import de.java2html.test.BasicTestCase;
6
7 /**
8  * @author Markus Gebhard
9  */

10 public class CommandlineArgumentsTest extends BasicTestCase {
11
12   public void testIsFlagSet() throws IllegalCommandlineParametersException {
13     CommandlineArguments emptyArguments = new CommandlineArguments(new String JavaDoc[]{});
14     assertFalse(emptyArguments.isFlagSet("flag"));
15
16     CommandlineArguments arguments = new CommandlineArguments(new String JavaDoc[]{
17         "-parameter",
18         "value",
19         "-flag" });
20     assertTrue(arguments.isFlagSet("flag"));
21     assertTrue(arguments.isFlagSet("FLAG"));
22     assertFalse(arguments.isFlagSet("-flag"));
23     assertFalse(arguments.isFlagSet("value"));
24     assertFalse(arguments.isFlagSet("parameter"));
25   }
26
27   public void testIsParameterSet() throws IllegalCommandlineParametersException {
28     CommandlineArguments emptyArguments = new CommandlineArguments(new String JavaDoc[]{});
29     assertFalse(emptyArguments.isParameterSet("flag"));
30
31     CommandlineArguments arguments = new CommandlineArguments(new String JavaDoc[]{
32         "-parameter",
33         "value",
34         "-flag" });
35     assertFalse(arguments.isParameterSet("flag"));
36     assertFalse(arguments.isParameterSet("FLAG"));
37     assertFalse(arguments.isParameterSet("-flag"));
38     assertFalse(arguments.isParameterSet("value"));
39     assertTrue(arguments.isParameterSet("parameter"));
40     assertTrue(arguments.isParameterSet("pArAmEtEr"));
41   }
42
43   public void testNumbersAreNotFlags() throws IllegalCommandlineParametersException {
44     CommandlineArguments arguments = new CommandlineArguments(new String JavaDoc[]{ "-flag", "-10" });
45     assertFalse(arguments.isFlagSet("10"));
46     assertFalse(arguments.isFlagSet("flag"));
47     assertTrue(arguments.isParameterSet("flag"));
48   }
49
50   public void testDuplicateFlagDefinitionThrowsException() {
51     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
52       public void execute() throws Exception JavaDoc {
53         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "-flag1" });
54       }
55     });
56   }
57
58   public void testDuplicateParameterDefinitionThrowsException() {
59     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
60       public void execute() throws Exception JavaDoc {
61         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value1", "-flag1", "value1" });
62       }
63     });
64   }
65
66   public void testDuplicateMixedDefinitionThrowsException() {
67     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
68       public void execute() throws Exception JavaDoc {
69         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "-flag1", "value" });
70       }
71     });
72   }
73
74   public void testGetArgumentStringValue() throws IllegalCommandlineParametersException {
75     assertEquals("value", new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value" })
76         .getParameterStringValue("flag1"));
77     assertEquals(null, new CommandlineArguments(new String JavaDoc[]{ "-flag1", "-flag2" })
78         .getParameterStringValue("flag1"));
79   }
80
81   public void testGetRequiredArgumentStringValue() throws IllegalCommandlineParametersException {
82     assertEquals("value", new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value" })
83         .getRequiredParameterStringValue(("flag1")));
84     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
85       public void execute() throws Exception JavaDoc {
86         new CommandlineArguments(new String JavaDoc[]{ "-flag1" }).getRequiredParameterStringValue("flag1");
87       }
88     });
89   }
90
91   public void testGetArgumentStringValueWithDefault() throws IllegalCommandlineParametersException {
92     assertEquals("default", new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value" })
93         .getParameterStringValue("undefinedFlag", "default"));
94     assertEquals("value", new CommandlineArguments(new String JavaDoc[]{ "-definedflag", "value" })
95         .getParameterStringValue("definedflag", "default"));
96   }
97
98   public void testGetArgumentIntValueWithDefault() throws IllegalCommandlineParametersException {
99     assertEquals(42, new CommandlineArguments(new String JavaDoc[]{ "-flag1", "42" })
100         .getParameterPositiveIntValue("flag1", 1));
101     assertEquals(1, new CommandlineArguments(new String JavaDoc[]{}).getParameterPositiveIntValue(
102         "flag1",
103         1));
104   }
105
106   public void testGetArgumentIntValueWithIllegalValue() {
107     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
108       public void execute() throws Exception JavaDoc {
109         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "abc" }).getParameterPositiveIntValue(
110             "flag1",
111             1);
112       }
113     });
114     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
115       public void execute() throws Exception JavaDoc {
116         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "-10" }).getParameterPositiveIntValue(
117             "flag1",
118             1);
119       }
120     });
121   }
122
123   public void testUnsupportedParameterThrowsException()
124       throws IllegalCommandlineParametersException {
125     new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value1", "-flag2" })
126         .assertContainsNoUnsupportedParameters(new String JavaDoc[]{ "flag1" });
127     new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value1", "-flag2" })
128         .assertContainsNoUnsupportedParameters(new String JavaDoc[]{ "FLag1" });
129
130     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
131       public void execute() throws Exception JavaDoc {
132         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value1", "-flag2", "value2" })
133             .assertContainsNoUnsupportedParameters(new String JavaDoc[]{ "flag1" });
134       }
135     });
136
137     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
138       public void execute() throws Exception JavaDoc {
139         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value1", "-flag2" })
140             .assertContainsNoUnsupportedParameters(new String JavaDoc[]{ "flag2" });
141       }
142     });
143
144     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
145       public void execute() throws Exception JavaDoc {
146         new CommandlineArguments(new String JavaDoc[]{ "-flag", "value" })
147             .assertContainsNoUnsupportedParameters(new String JavaDoc[0]);
148       }
149     });
150   }
151
152   public void testUnsupportedFlagThrowsException() throws IllegalCommandlineParametersException {
153     new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value1", "-flag2" })
154         .assertContainsNoUnsupportedFlags(new String JavaDoc[]{ "flag1", "flag2" });
155     new CommandlineArguments(new String JavaDoc[]{ "-flag1", "value1", "-flag2" })
156         .assertContainsNoUnsupportedFlags(new String JavaDoc[]{ "FLag1", "FLag2" });
157
158     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
159       public void execute() throws Exception JavaDoc {
160         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "-flag2" })
161             .assertContainsNoUnsupportedFlags(new String JavaDoc[]{ "flag1" });
162       }
163     });
164
165     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
166       public void execute() throws Exception JavaDoc {
167         new CommandlineArguments(new String JavaDoc[]{ "-flag1", "-flag2" })
168             .assertContainsNoUnsupportedFlags(new String JavaDoc[]{ "flag2" });
169       }
170     });
171
172     assertThrowsException(IllegalCommandlineParametersException.class, new IBlock() {
173       public void execute() throws Exception JavaDoc {
174         new CommandlineArguments(new String JavaDoc[]{ "-flag" })
175             .assertContainsNoUnsupportedFlags(new String JavaDoc[0]);
176       }
177     });
178   }
179 }
Popular Tags