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