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.Iterator;
20  import java.util.Properties;
21  import java.util.Set;
22  
23  import org.apache.commons.cli2.CommandLine;
24  import org.apache.commons.cli2.CommandLineTestCase;
25  
26  /**
27   * @author Rob Oxspring
28   */
29  public class PropertiesCommandLineTest
30      extends CommandLineTestCase {
31      private Properties props = null;
32  
33      protected CommandLine createCommandLine() {
34          props = new Properties();
35          props.setProperty("--present", "present value");
36          props.setProperty("--alsopresent", "");
37          props.setProperty("--multiple", "value 1|value 2|value 3");
38          props.setProperty("--bool", "true");
39  
40          props.setProperty("present", "present property");
41      	return new PropertiesCommandLine(root, props, '|');
42      }
43  
44      protected CommandLine createCommandLineNoSep() {
45          props = new Properties();
46          props.setProperty("--present", "present value");
47          props.setProperty("--alsopresent", "");
48          props.setProperty("--multiple", "value 1|value 2|value 3");
49          props.setProperty("--bool", "false");
50  
51          props.setProperty("present", "present property");
52      	return new PropertiesCommandLine(root, props);
53      }
54  
55      public void testPropertyValues() {
56          // nothing to test
57      	CommandLine cmdline = createCommandLine();
58  
59      	assertEquals("wrong value", "present value", cmdline.getValue("--present"));
60      	assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
61      	assertEquals("wrong # of values", 3, cmdline.getValues("--multiple").size());
62      	assertEquals("wrong value 1", "value 1", cmdline.getValues("--multiple").get(0));
63      	assertEquals("wrong value 2", "value 2", cmdline.getValues("--multiple").get(1));
64      	assertEquals("wrong value 3", "value 3", cmdline.getValues("--multiple").get(2));
65      }
66  
67      public void testNoSeparator() {
68          // nothing to test
69      	CommandLine cmdline = createCommandLineNoSep();
70  
71      	assertEquals("wrong value", "present value", cmdline.getValue("--present"));
72      	assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
73      	assertEquals("wrong # of values", 1, cmdline.getValues("--multiple").size());
74      	assertEquals("wrong value", "value 1|value 2|value 3", cmdline.getValue("--multiple"));
75      	assertFalse("expected a false", cmdline.getSwitch("--bool").booleanValue());
76      }
77  
78      public void testNullOption() {
79          // nothing to test
80      	CommandLine cmdline = createCommandLine();
81  
82      	assertFalse("should not find null option", cmdline.hasOption((String) null));
83      	assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
84      }
85  
86      public void testPropertyTriggers() {
87          // nothing to test
88      	CommandLine cmdline = createCommandLine();
89  
90      	Set triggers = cmdline.getOptionTriggers();
91          Iterator iter = triggers.iterator();
92          assertEquals("wrong # of triggers", 4, triggers.size());
93          assertTrue("cannot find trigger", triggers.contains("--bool"));
94          assertTrue("cannot find trigger", triggers.contains("--present"));
95          assertTrue("cannot find trigger", triggers.contains("--multiple"));
96          assertTrue("cannot find trigger", triggers.contains("--alsopresent"));
97  
98      	assertFalse("should not find null option", cmdline.hasOption((String) null));
99      	assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
100     }
101 }