Skip to content

Commit 5ad6ce2

Browse files
author
John Keyes
committed
- added ArgumentBuilder test
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@280699 13f79535-47bb-0310-9956-ffa450edef68
1 parent 099a454 commit 5ad6ce2

6 files changed

Lines changed: 342 additions & 4 deletions

File tree

src/java/org/apache/commons/cli2/builder/ArgumentBuilder.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020

2121
import org.apache.commons.cli2.Argument;
2222
import org.apache.commons.cli2.option.ArgumentImpl;
23+
import org.apache.commons.cli2.resource.ResourceConstants;
24+
import org.apache.commons.cli2.resource.ResourceHelper;
2325
import org.apache.commons.cli2.validation.Validator;
2426

2527
/**
2628
* Builds Argument instances.
2729
*/
2830
public class ArgumentBuilder {
2931

32+
/** i18n */
33+
private final static ResourceHelper resources = ResourceHelper.getResourceHelper();
34+
3035
/** name of the argument. Used for display and lookups in CommandLine */
3136
private String name;
3237

@@ -118,6 +123,12 @@ public final ArgumentBuilder reset() {
118123
* @return this ArgumentBuilder
119124
*/
120125
public final ArgumentBuilder withName(final String newName) {
126+
if (newName == null) {
127+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME));
128+
}
129+
if ("".equals(newName)) {
130+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME));
131+
}
121132
this.name = newName;
122133
return this;
123134
}
@@ -142,6 +153,9 @@ public final ArgumentBuilder withDescription(final String newDescription) {
142153
* @return this ArgumentBuilder
143154
*/
144155
public final ArgumentBuilder withMinimum(final int newMinimum) {
156+
if (newMinimum < 0) {
157+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM));
158+
}
145159
this.minimum = newMinimum;
146160
return this;
147161
}
@@ -153,6 +167,9 @@ public final ArgumentBuilder withMinimum(final int newMinimum) {
153167
* @return this ArgumentBuilder
154168
*/
155169
public final ArgumentBuilder withMaximum(final int newMaximum) {
170+
if (newMaximum < 0) {
171+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM));
172+
}
156173
this.maximum = newMaximum;
157174
return this;
158175
}
@@ -197,6 +214,9 @@ public final ArgumentBuilder withSubsequentSeparator(
197214
* @return this ArgumentBuilder
198215
*/
199216
public final ArgumentBuilder withValidator(final Validator newValidator) {
217+
if (newValidator == null) {
218+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR));
219+
}
200220
this.validator = newValidator;
201221
return this;
202222
}
@@ -210,7 +230,12 @@ public final ArgumentBuilder withValidator(final Validator newValidator) {
210230
* @return this ArgumentBuilder
211231
*/
212232
public final ArgumentBuilder withConsumeRemaining(final String newConsumeRemaining) {
213-
233+
if (newConsumeRemaining == null) {
234+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING));
235+
}
236+
if ( "".equals(newConsumeRemaining)) {
237+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING));
238+
}
214239
this.consumeRemaining = newConsumeRemaining;
215240
return this;
216241
}
@@ -222,6 +247,10 @@ public final ArgumentBuilder withConsumeRemaining(final String newConsumeRemaini
222247
* @return this ArgumentBuilder
223248
*/
224249
public final ArgumentBuilder withDefault(final Object defaultValue) {
250+
if (defaultValue == null) {
251+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT));
252+
}
253+
225254
if (this.defaultValues == null) {
226255
this.defaultValues = new ArrayList(1);
227256
}
@@ -236,6 +265,9 @@ public final ArgumentBuilder withDefault(final Object defaultValue) {
236265
* @return this ArgumentBuilder
237266
*/
238267
public final ArgumentBuilder withDefaults(final List newDefaultValues) {
268+
if (newDefaultValues == null) {
269+
throw new IllegalArgumentException(resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS));
270+
}
239271
this.defaultValues = newDefaultValues;
240272
return this;
241273
}

src/java/org/apache/commons/cli2/option/ArgumentImpl.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class ArgumentImpl
6262
private final int minimum;
6363
private final int maximum;
6464
private final char initialSeparator;
65-
private final char subsequentSepatator;
65+
private final char subsequentSeparator;
6666
private final boolean subsequentSplit;
6767
private final Validator validator;
6868
private final String consumeRemaining;
@@ -112,7 +112,7 @@ public ArgumentImpl(final String name,
112112
this.minimum = minimum;
113113
this.maximum = maximum;
114114
this.initialSeparator = initialSeparator;
115-
this.subsequentSepatator = subsequentSeparator;
115+
this.subsequentSeparator = subsequentSeparator;
116116
this.subsequentSplit = subsequentSeparator != NUL;
117117
this.validator = validator;
118118
this.consumeRemaining = consumeRemaining;
@@ -162,7 +162,7 @@ else if (commandLine.looksLikeOption(allValues)) {
162162
// should we split the string up?
163163
else if (subsequentSplit) {
164164
final StringTokenizer values =
165-
new StringTokenizer(allValues, String.valueOf(subsequentSepatator));
165+
new StringTokenizer(allValues, String.valueOf(subsequentSeparator));
166166

167167
arguments.remove();
168168

@@ -206,10 +206,26 @@ public char getInitialSeparator() {
206206
return this.initialSeparator;
207207
}
208208

209+
public char getSubsequentSeparator() {
210+
return this.subsequentSeparator;
211+
}
212+
209213
public Set getTriggers() {
210214
return Collections.EMPTY_SET;
211215
}
212216

217+
public String getConsumeRemaining() {
218+
return this.consumeRemaining;
219+
}
220+
221+
public List getDefaultValues() {
222+
return this.defaultValues;
223+
}
224+
225+
public Validator getValidator() {
226+
return this.validator;
227+
}
228+
213229
public void validate(final WriteableCommandLine commandLine)
214230
throws OptionException {
215231
validate(commandLine, this);

src/java/org/apache/commons/cli2/resource/CLIMessageBundle_en_US.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ Enum.illegal.value = ''{0}'' is not allowed. Permitted values are: {1}
4545
Unexpected.token = Unexpected {0} while processing
4646
Missing.option = Missing option
4747
Cannot.burst = Could not burst "{0}" while processing
48+
49+
ArgumentBuilder.null.consume.remaining = Cannot use 'null' as the consume remaining token.
50+
ArgumentBuilder.empty.consume.remaining = Cannot use an empty string as the consume remaining token.
51+
ArgumentBuilder.null.defaults = Cannot use 'null' defaults.
52+
ArgumentBuilder.null.default = Cannot use 'null' default.
53+
ArgumentBuilder.negative.maximum = Cannot use a negative maximum value.
54+
ArgumentBuilder.negative.minimum = Cannot use a negative minimum value.
55+
ArgumentBuilder.null.name = Cannot use 'null' as a name.
56+
ArgumentBuilder.empty.name = Cannot use an empty string as a name.
57+
ArgumentBuilder.null.validator = Cannot use 'null' as a validator.

src/java/org/apache/commons/cli2/resource/ResourceConstants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ public abstract class ResourceConstants {
5454
public static final String HELPFORMATTER_GUTTER_TOO_LONG = "HelpFormatter.gutter.too.long";
5555
public static final String HELPFORMATTER_WIDTH_TOO_NARROW = "HelpFormatter.width.too.narrow";
5656
public static final String ENUM_ILLEGAL_VALUE = "Enum.illegal.value";
57+
public static final String ARGUMENT_BUILDER_NULL_CONSUME_REMAINING = "ArgumentBuilder.null.consume.remaining";
58+
public static final String ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING = "ArgumentBuilder.empty.consume.remaining";
59+
public static final String ARGUMENT_BUILDER_NULL_DEFAULT = "ArgumentBuilder.null.default";
60+
public static final String ARGUMENT_BUILDER_NULL_DEFAULTS = "ArgumentBuilder.null.defaults";
61+
public static final String ARGUMENT_BUILDER_NEGATIVE_MAXIMUM = "ArgumentBuilder.negative.maximum";
62+
public static final String ARGUMENT_BUILDER_NEGATIVE_MINIMUM = "ArgumentBuilder.negative.minimum";
63+
public static final String ARGUMENT_BUILDER_NULL_NAME = "ArgumentBuilder.null.name";
64+
public static final String ARGUMENT_BUILDER_EMPTY_NAME = "ArgumentBuilder.empty.name";
65+
public static final String ARGUMENT_BUILDER_NULL_VALIDATOR = "ArgumentBuilder.null.validator";
66+
5767
}

0 commit comments

Comments
 (0)