|
1 | | -/* |
2 | | - * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | - * contributor license agreements. See the NOTICE file distributed with |
4 | | - * this work for additional information regarding copyright ownership. |
5 | | - * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | - * (the "License"); you may not use this file except in compliance with |
7 | | - * the License. You may obtain a copy of the License at |
8 | | - * |
9 | | - * 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 | | - |
18 | | -package org.apache.commons.cli; |
19 | | - |
20 | | -import junit.framework.TestCase; |
21 | | - |
22 | | -/** |
23 | | - * @author brianegge |
24 | | - */ |
25 | | -public class OptionTest extends TestCase { |
26 | | - |
27 | | - private static class TestOption extends Option { |
28 | | - public TestOption(String opt, boolean hasArg, String description) throws IllegalArgumentException { |
29 | | - super(opt, hasArg, description); |
30 | | - } |
31 | | - public boolean addValue(String value) { |
32 | | - addValueForProcessing(value); |
33 | | - return true; |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - public void testClear() { |
38 | | - TestOption option = new TestOption("x", true, ""); |
39 | | - assertEquals(0, option.getValuesList().size()); |
40 | | - option.addValue("a"); |
41 | | - assertEquals(1, option.getValuesList().size()); |
42 | | - option.clearValues(); |
43 | | - assertEquals(0, option.getValuesList().size()); |
44 | | - } |
45 | | - |
46 | | - // See http://issues.apache.org/jira/browse/CLI-21 |
47 | | - public void testClone() throws CloneNotSupportedException { |
48 | | - TestOption a = new TestOption("a", true, ""); |
49 | | - TestOption b = (TestOption) a.clone(); |
50 | | - assertEquals(a, b); |
51 | | - assertNotSame(a, b); |
52 | | - a.setDescription("a"); |
53 | | - assertEquals("", b.getDescription()); |
54 | | - b.setArgs(2); |
55 | | - b.addValue("b1"); |
56 | | - b.addValue("b2"); |
57 | | - assertEquals(1, a.getArgs()); |
58 | | - assertEquals(0, a.getValuesList().size()); |
59 | | - assertEquals(2, b.getValues().length); |
60 | | - } |
61 | | - |
62 | | - private static class DefaultOption extends Option { |
63 | | - |
64 | | - private final String defaultValue; |
65 | | - |
66 | | - public DefaultOption(String opt, String description, String defaultValue) throws IllegalArgumentException { |
67 | | - super(opt, true, description); |
68 | | - this.defaultValue = defaultValue; |
69 | | - } |
70 | | - |
71 | | - public String getValue() { |
72 | | - return super.getValue() != null ? super.getValue() : defaultValue; |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - public void testSubclass() throws CloneNotSupportedException { |
77 | | - Option option = new DefaultOption("f", "file", "myfile.txt"); |
78 | | - Option clone = (Option) option.clone(); |
79 | | - assertEquals("myfile.txt", clone.getValue()); |
80 | | - assertEquals(DefaultOption.class, clone.getClass()); |
81 | | - } |
82 | | - |
83 | | -} |
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * 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 | + |
| 18 | +package org.apache.commons.cli; |
| 19 | + |
| 20 | +import junit.framework.TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author brianegge |
| 24 | + */ |
| 25 | +public class OptionTest extends TestCase { |
| 26 | + |
| 27 | + private static class TestOption extends Option { |
| 28 | + public TestOption(String opt, boolean hasArg, String description) throws IllegalArgumentException { |
| 29 | + super(opt, hasArg, description); |
| 30 | + } |
| 31 | + public boolean addValue(String value) { |
| 32 | + addValueForProcessing(value); |
| 33 | + return true; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public void testClear() { |
| 38 | + TestOption option = new TestOption("x", true, ""); |
| 39 | + assertEquals(0, option.getValuesList().size()); |
| 40 | + option.addValue("a"); |
| 41 | + assertEquals(1, option.getValuesList().size()); |
| 42 | + option.clearValues(); |
| 43 | + assertEquals(0, option.getValuesList().size()); |
| 44 | + } |
| 45 | + |
| 46 | + // See http://issues.apache.org/jira/browse/CLI-21 |
| 47 | + public void testClone() throws CloneNotSupportedException { |
| 48 | + TestOption a = new TestOption("a", true, ""); |
| 49 | + TestOption b = (TestOption) a.clone(); |
| 50 | + assertEquals(a, b); |
| 51 | + assertNotSame(a, b); |
| 52 | + a.setDescription("a"); |
| 53 | + assertEquals("", b.getDescription()); |
| 54 | + b.setArgs(2); |
| 55 | + b.addValue("b1"); |
| 56 | + b.addValue("b2"); |
| 57 | + assertEquals(1, a.getArgs()); |
| 58 | + assertEquals(0, a.getValuesList().size()); |
| 59 | + assertEquals(2, b.getValues().length); |
| 60 | + } |
| 61 | + |
| 62 | + private static class DefaultOption extends Option { |
| 63 | + |
| 64 | + private final String defaultValue; |
| 65 | + |
| 66 | + public DefaultOption(String opt, String description, String defaultValue) throws IllegalArgumentException { |
| 67 | + super(opt, true, description); |
| 68 | + this.defaultValue = defaultValue; |
| 69 | + } |
| 70 | + |
| 71 | + public String getValue() { |
| 72 | + return super.getValue() != null ? super.getValue() : defaultValue; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void testSubclass() throws CloneNotSupportedException { |
| 77 | + Option option = new DefaultOption("f", "file", "myfile.txt"); |
| 78 | + Option clone = (Option) option.clone(); |
| 79 | + assertEquals("myfile.txt", clone.getValue()); |
| 80 | + assertEquals(DefaultOption.class, clone.getClass()); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments