01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.cli;
18:
19: import junit.framework.TestCase;
20:
21: /**
22: * @author brianegge
23: */
24: public class OptionTest extends TestCase {
25:
26: private static class TestOption extends Option {
27: public TestOption(String opt, boolean hasArg, String description)
28: throws IllegalArgumentException {
29: super (opt, hasArg, description);
30: }
31:
32: public boolean addValue(String value) {
33: addValueForProcessing(value);
34: return true;
35: }
36: }
37:
38: public void testClear() {
39: TestOption option = new TestOption("x", true, "");
40: assertEquals(0, option.getValuesList().size());
41: option.addValue("a");
42: assertEquals(1, option.getValuesList().size());
43: option.clearValues();
44: assertEquals(0, option.getValuesList().size());
45: }
46:
47: // See http://issues.apache.org/jira/browse/CLI-21
48: public void testClone() throws CloneNotSupportedException {
49: TestOption a = new TestOption("a", true, "");
50: TestOption b = (TestOption) a.clone();
51: assertEquals(a, b);
52: assertNotSame(a, b);
53: a.setDescription("a");
54: assertEquals("", b.getDescription());
55: b.setArgs(2);
56: b.addValue("b1");
57: b.addValue("b2");
58: assertEquals(1, a.getArgs());
59: assertEquals(0, a.getValuesList().size());
60: assertEquals(2, b.getValues().length);
61: }
62:
63: private static class DefaultOption extends Option {
64:
65: private final String defaultValue;
66:
67: public DefaultOption(String opt, String description,
68: String defaultValue) throws IllegalArgumentException {
69: super (opt, true, description);
70: this .defaultValue = defaultValue;
71: }
72:
73: public String getValue() {
74: return super .getValue() != null ? super .getValue()
75: : defaultValue;
76: }
77: }
78:
79: public void testSubclass() throws CloneNotSupportedException {
80: Option option = new DefaultOption("f", "file", "myfile.txt");
81: Option clone = (Option) option.clone();
82: assertEquals("myfile.txt", clone.getValue());
83: assertEquals(DefaultOption.class, clone.getClass());
84: }
85:
86: }
|