View Javadoc

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.cli2.commandline;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.Set;
23  
24  import org.apache.commons.cli2.CommandLine;
25  import org.apache.commons.cli2.CommandLineTestCase;
26  import org.apache.commons.cli2.Option;
27  import org.apache.commons.cli2.WriteableCommandLine;
28  import org.apache.commons.cli2.option.PropertyOption;
29  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
30  
31  /**
32   * @author Rob Oxspring
33   */
34  public class DefaultingCommandLineTest
35      extends CommandLineTestCase {
36      private CommandLine first;
37      private CommandLine second;
38      private Option inFirst = new DefaultOptionBuilder().withLongName("infirst").create();
39      private Option inBoth = new DefaultOptionBuilder().withLongName("inboth").create();
40      private Option inSecond = new DefaultOptionBuilder().withLongName("insecond").create();
41  
42      /* (non-Javadoc)
43       * @see org.apache.commons.cli2.CommandLineTest#createCommandLine()
44       */
45      protected final CommandLine createCommandLine() {
46          final WriteableCommandLine writeable = new WriteableCommandLineImpl(root, new ArrayList());
47          writeable.addOption(present);
48          writeable.addProperty(new PropertyOption(), "present", "present property");
49          writeable.addSwitch(bool, true);
50          writeable.addValue(present, "present value");
51          writeable.addOption(multiple);
52          writeable.addValue(multiple, "value 1");
53          writeable.addValue(multiple, "value 2");
54          writeable.addValue(multiple, "value 3");
55  
56          final DefaultingCommandLine defaults = new DefaultingCommandLine();
57          defaults.appendCommandLine(writeable);
58  
59          return defaults;
60      }
61  
62      public void setUp()
63          throws Exception {
64          super.setUp();
65  
66          WriteableCommandLine writeable;
67  
68          writeable = new WriteableCommandLineImpl(root, new ArrayList());
69          writeable.addOption(inFirst);
70          writeable.addOption(inBoth);
71          writeable.addProperty("infirst", "infirst first value");
72          writeable.addProperty("inboth", "inboth first value");
73          writeable.addSwitch(inFirst, true);
74          writeable.addSwitch(inBoth, true);
75          writeable.addValue(inFirst, "infirst first value 1");
76          writeable.addValue(inFirst, "infirst first value 2");
77          writeable.addValue(inBoth, "inboth first value 1");
78          writeable.addValue(inBoth, "inboth first value 2");
79          first = writeable;
80  
81          writeable = new WriteableCommandLineImpl(root, new ArrayList());
82          writeable.addOption(inSecond);
83          writeable.addOption(inBoth);
84          writeable.addProperty("insecond", "insecond second value");
85          writeable.addProperty("inboth", "inboth second value");
86          writeable.addSwitch(inSecond, true);
87          writeable.addSwitch(inBoth, true);
88          writeable.addValue(inSecond, "insecond second value 1");
89          writeable.addValue(inSecond, "insecond second value 2");
90          writeable.addValue(inBoth, "inboth second value 1");
91          writeable.addValue(inBoth, "inboth second value 2");
92          second = writeable;
93      }
94  
95      public final void testAppendCommandLine() {
96          final DefaultingCommandLine defaults = new DefaultingCommandLine();
97          Iterator i;
98  
99          i = defaults.commandLines();
100         assertFalse(i.hasNext());
101 
102         defaults.appendCommandLine(first);
103         i = defaults.commandLines();
104         assertSame(first, i.next());
105         assertFalse(i.hasNext());
106 
107         defaults.appendCommandLine(second);
108         i = defaults.commandLines();
109         assertSame(first, i.next());
110         assertSame(second, i.next());
111         assertFalse(i.hasNext());
112     }
113 
114     public final void testInsertCommandLine() {
115         final DefaultingCommandLine defaults = new DefaultingCommandLine();
116         Iterator i;
117 
118         i = defaults.commandLines();
119         assertFalse(i.hasNext());
120 
121         defaults.insertCommandLine(0, first);
122         i = defaults.commandLines();
123         assertSame(first, i.next());
124         assertFalse(i.hasNext());
125 
126         defaults.insertCommandLine(0, second);
127         i = defaults.commandLines();
128         assertSame(second, i.next());
129         assertSame(first, i.next());
130         assertFalse(i.hasNext());
131     }
132 
133     public void testTriggers() {
134         final DefaultingCommandLine defaults = new DefaultingCommandLine();
135         defaults.appendCommandLine(first);
136         defaults.appendCommandLine(second);
137 
138         Set set = defaults.getOptionTriggers();
139         Iterator iter = set.iterator();
140         assertEquals("wrong # of triggers", 3, set.size());
141         assertTrue("cannot find trigger", set.contains("--insecond"));
142         assertTrue("cannot find trigger", set.contains("--inboth"));
143         assertTrue("cannot find trigger", set.contains("--infirst"));
144     }
145 
146     public void testDefaults() {
147         final DefaultingCommandLine defaults = new DefaultingCommandLine();
148 
149         assertEquals("wrong # of defaults", 0, defaults.getValues("--insecond").size());
150         assertEquals("wrong Set of defaults", Collections.EMPTY_LIST, defaults.getValues("--insecond", null));
151     }
152 }