1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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) throws IllegalArgumentException {
28 super(opt, hasArg, description);
29 }
30 public boolean addValue(String value) {
31 addValueForProcessing(value);
32 return true;
33 }
34 }
35
36 public void testClear() {
37 TestOption option = new TestOption("x", true, "");
38 assertEquals(0, option.getValuesList().size());
39 option.addValue("a");
40 assertEquals(1, option.getValuesList().size());
41 option.clearValues();
42 assertEquals(0, option.getValuesList().size());
43 }
44
45
46 public void testClone() throws CloneNotSupportedException {
47 TestOption a = new TestOption("a", true, "");
48 TestOption b = (TestOption) a.clone();
49 assertEquals(a, b);
50 assertNotSame(a, b);
51 a.setDescription("a");
52 assertEquals("", b.getDescription());
53 b.setArgs(2);
54 b.addValue("b1");
55 b.addValue("b2");
56 assertEquals(1, a.getArgs());
57 assertEquals(0, a.getValuesList().size());
58 assertEquals(2, b.getValues().length);
59 }
60
61 private static class DefaultOption extends Option {
62
63 private final String defaultValue;
64
65 public DefaultOption(String opt, String description, String defaultValue) throws IllegalArgumentException {
66 super(opt, true, description);
67 this.defaultValue = defaultValue;
68 }
69
70 public String getValue() {
71 return super.getValue() != null ? super.getValue() : defaultValue;
72 }
73 }
74
75 public void testSubclass() throws CloneNotSupportedException {
76 Option option = new DefaultOption("f", "file", "myfile.txt");
77 Option clone = (Option) option.clone();
78 assertEquals("myfile.txt", clone.getValue());
79 assertEquals(DefaultOption.class, clone.getClass());
80 }
81
82 }