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  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      // See http://issues.apache.org/jira/browse/CLI-21
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  }