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.option;
18  
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.ListIterator;
23  import java.util.Set;
24  
25  import org.apache.commons.cli2.DisplaySetting;
26  import org.apache.commons.cli2.HelpLine;
27  import org.apache.commons.cli2.Option;
28  import org.apache.commons.cli2.OptionException;
29  import org.apache.commons.cli2.WriteableCommandLine;
30  import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
31  
32  /**
33   * @author Rob Oxspring
34   */
35  public class PropertyOptionTest extends OptionTestCase {
36  
37      /*
38       * (non-Javadoc)
39       *
40       * @see org.apache.commons.cli2.OptionTestCase#testCanProcess()
41       */
42      public void testCanProcess() {
43          final Option option = new PropertyOption();
44          assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-Dmyprop=myval"));
45      }
46  
47      public void testCanProcess_Null() {
48          final Option option = new PropertyOption();
49          assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), (String) null));
50      }
51  
52      public void testCanProcess_TooShort() {
53          final Option option = new PropertyOption();
54          assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-D"));
55      }
56  
57      public void testCanProcess_BadMatch() {
58          final Option option = new PropertyOption();
59          assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null),"-dump"));
60      }
61  
62      /*
63       * (non-Javadoc)
64       *
65       * @see org.apache.commons.cli2.OptionTestCase#testPrefixes()
66       */
67      public void testPrefixes() {
68          final Option option = new PropertyOption();
69          assertContentsEqual(list("-D"), option.getPrefixes());
70      }
71  
72      /*
73       * (non-Javadoc)
74       *
75       * @see org.apache.commons.cli2.OptionTestCase#testProcess()
76       */
77      public void testProcess() throws OptionException {
78          final PropertyOption option = new PropertyOption();
79          final List args = list("-Dmyprop=myvalue");
80          final WriteableCommandLine commandLine = commandLine(option, args);
81          final ListIterator iterator = args.listIterator();
82  
83          option.process(commandLine, iterator);
84          assertEquals("myvalue", commandLine.getProperty(option, "myprop"));
85          assertFalse(iterator.hasNext());
86          assertEquals(1, commandLine.getProperties(option).size());
87      }
88  
89      public void testProcess_UnexpectedOptionException() {
90          final Option option = new PropertyOption();
91          final List args = list("--help");
92          final WriteableCommandLine commandLine = commandLine(option, args);
93          final ListIterator iterator = args.listIterator();
94  
95          try {
96              option.process(commandLine, iterator);
97              fail("UnexpectedOption");
98          }
99          catch (final OptionException uoe) {
100             assertEquals(option, uoe.getOption());
101             assertEquals(
102                 "Unexpected --help while processing -Dproperty=value",
103                 uoe.getMessage());
104         }
105     }
106 
107     public void testProcess_BadPropertyException() throws OptionException {
108         final PropertyOption option = new PropertyOption();
109         final List args = list("-Dmyprop");
110         final WriteableCommandLine commandLine = commandLine(option, args);
111         final ListIterator iterator = args.listIterator();
112 
113         option.process(commandLine, iterator);
114 
115         assertEquals("true", commandLine.getProperty(option, "myprop"));
116     }
117 
118     public void testProcess_SetToEmpty() throws OptionException {
119         final PropertyOption option = new PropertyOption();
120         final List args = list("-Dmyprop=");
121         final WriteableCommandLine commandLine = commandLine(option, args);
122         final ListIterator iterator = args.listIterator();
123 
124         option.process(commandLine, iterator);
125         assertEquals("", commandLine.getProperty(option, "myprop"));
126         assertFalse(iterator.hasNext());
127         assertEquals(1, commandLine.getProperties(option).size());
128     }
129 
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.apache.commons.cli2.OptionTestCase#testTriggers()
134      */
135     public void testTriggers() {
136         final Option option = new PropertyOption();
137 
138         assertContentsEqual(list("-D"), option.getTriggers());
139     }
140 
141     /*
142      * (non-Javadoc)
143      *
144      * @see org.apache.commons.cli2.OptionTestCase#testValidate()
145      */
146     public void testValidate() throws OptionException {
147         final Option option = new PropertyOption();
148         final List args = list("-Dproperty=value");
149         final WriteableCommandLine commandLine = commandLine(option, args);
150         final ListIterator iterator = args.listIterator();
151 
152         option.process(commandLine, iterator);
153 
154         option.validate(commandLine);
155     }
156 
157     /*
158      * (non-Javadoc)
159      *
160      * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage()
161      */
162     public void testAppendUsage() {
163         final Option option = new PropertyOption();
164         final StringBuffer buffer = new StringBuffer();
165         option.appendUsage(buffer, DisplaySetting.ALL, null);
166 
167         assertEquals("-D<property>=<value>", buffer.toString());
168     }
169 
170     public void testAppendUsage_Hidden() {
171         final Option option = new PropertyOption();
172         final StringBuffer buffer = new StringBuffer();
173         final Set settings = new HashSet(DisplaySetting.ALL);
174         settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION);
175         option.appendUsage(buffer, settings, null);
176 
177         assertEquals("", buffer.toString());
178     }
179 
180     /*
181      * (non-Javadoc)
182      *
183      * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName()
184      */
185     public void testGetPreferredName() {
186         final Option option = new PropertyOption();
187         assertEquals("-D", option.getPreferredName());
188     }
189 
190     /*
191      * (non-Javadoc)
192      *
193      * @see org.apache.commons.cli2.OptionTestCase#testGetDescription()
194      */
195     public void testGetDescription() {
196         final Option option = new PropertyOption();
197         assertEquals(
198             "Passes properties and values to the application",
199             option.getDescription());
200     }
201 
202     /*
203      * (non-Javadoc)
204      *
205      * @see org.apache.commons.cli2.OptionTestCase#testHelpLines()
206      */
207     public void testHelpLines() {
208         final Option option = new PropertyOption();
209         final List lines = option.helpLines(0, DisplaySetting.ALL, null);
210         final Iterator i = lines.iterator();
211 
212         final HelpLine line1 = (HelpLine)i.next();
213         assertEquals(0, line1.getIndent());
214         assertEquals(option, line1.getOption());
215 
216         assertFalse(i.hasNext());
217     }
218 
219     /*
220      * (non-Javadoc)
221      *
222      * @see org.apache.commons.cli2.OptionTestCase#testHelpLines()
223      */
224     public void testHelpLines_NoDisplay() {
225         final Option option = new PropertyOption();
226         final Set settings = new HashSet(DisplaySetting.ALL);
227         settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION);
228         final List lines = option.helpLines(0, settings, null);
229         final Iterator i = lines.iterator();
230 
231         assertFalse(i.hasNext());
232     }
233 }