001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.cli2.option; 018 019import java.util.HashSet; 020import java.util.List; 021import java.util.ListIterator; 022import java.util.Set; 023 024import org.apache.commons.cli2.DisplaySetting; 025import org.apache.commons.cli2.Option; 026import org.apache.commons.cli2.OptionException; 027import org.apache.commons.cli2.Parent; 028import org.apache.commons.cli2.WriteableCommandLine; 029import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; 030import org.apache.commons.cli2.resource.ResourceConstants; 031import org.apache.commons.cli2.resource.ResourceHelper; 032 033/** 034 * @author Rob Oxspring 035 * 036 * To change the template for this generated type comment go to 037 * Window>Preferences>Java>Code Generation>Code and Comments 038 */ 039public class SwitchTest 040 extends ParentTestCase { 041 public static Switch buildDisplaySwitch() { 042 final Set aliases = new HashSet(); 043 aliases.add("d"); 044 aliases.add("disp"); 045 046 return new Switch("+", "-", "display", aliases, "Sets whether to display to screen", true, 047 null, null, 'd', null); 048 } 049 050 /* 051 * (non-Javadoc) 052 * 053 * @see org.apache.commons.cli2.ParentTestCase#testProcessParent() 054 */ 055 public void testProcessParent() 056 throws OptionException { 057 final Switch option = buildDisplaySwitch(); 058 final List args = list("+d"); 059 final WriteableCommandLine commandLine = commandLine(option, args); 060 final ListIterator iterator = args.listIterator(); 061 option.processParent(commandLine, iterator); 062 063 assertFalse(iterator.hasNext()); 064 assertTrue(commandLine.hasOption(option)); 065 assertTrue(commandLine.hasOption("+d")); 066 assertTrue(commandLine.hasOption("-display")); 067 assertEquals(Boolean.TRUE, commandLine.getSwitch("-d")); 068 assertTrue(commandLine.getValues(option).isEmpty()); 069 } 070 071 public void testProcessParent_Disabled() 072 throws OptionException { 073 final Switch option = buildDisplaySwitch(); 074 final List args = list("-disp"); 075 final WriteableCommandLine commandLine = commandLine(option, args); 076 final ListIterator iterator = args.listIterator(); 077 option.process(commandLine, iterator); 078 079 assertFalse(iterator.hasNext()); 080 assertTrue(commandLine.hasOption(option)); 081 assertTrue(commandLine.hasOption("+d")); 082 assertTrue(commandLine.hasOption("-display")); 083 assertEquals(Boolean.FALSE, commandLine.getSwitch("-d")); 084 assertTrue(commandLine.getValues(option).isEmpty()); 085 } 086 087 /* 088 * (non-Javadoc) 089 * 090 * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() 091 */ 092 public void testCanProcess() { 093 final Switch option = buildDisplaySwitch(); 094 assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "+d")); 095 } 096 097 public void testCanProcess_BadMatch() { 098 final Switch option = buildDisplaySwitch(); 099 assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "-dont")); 100 } 101 102 /* 103 * (non-Javadoc) 104 * 105 * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() 106 */ 107 public void testPrefixes() { 108 final Switch option = buildDisplaySwitch(); 109 assertContentsEqual(list("-", "+"), option.getPrefixes()); 110 } 111 112 /* 113 * (non-Javadoc) 114 * 115 * @see org.apache.commons.cli2.OptionTestCase#testProcess() 116 */ 117 public void testProcess() { 118 // TODO Auto-generated method stub 119 } 120 121 /* 122 * (non-Javadoc) 123 * 124 * @see org.apache.commons.cli2.OptionTestCase#testTriggers() 125 */ 126 public void testTriggers() { 127 final Switch option = buildDisplaySwitch(); 128 assertContentsEqual(list("-d", "+d", "-disp", "+disp", "+display", "-display"), 129 option.getTriggers()); 130 } 131 132 /* 133 * (non-Javadoc) 134 * 135 * @see org.apache.commons.cli2.OptionTestCase#testValidate() 136 */ 137 public void testValidate() { 138 final Parent option = buildDisplaySwitch(); 139 final WriteableCommandLine commandLine = commandLine(option, list()); 140 141 try { 142 option.validate(commandLine); 143 fail("Missing an option"); 144 } catch (OptionException moe) { 145 assertSame(option, moe.getOption()); 146 } 147 } 148 149 /* 150 * (non-Javadoc) 151 * 152 * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() 153 */ 154 public void testAppendUsage() { 155 final Option option = buildDisplaySwitch(); 156 final StringBuffer buffer = new StringBuffer(); 157 option.appendUsage(buffer, DisplaySetting.ALL, null); 158 159 assertEquals("+display|-display (+d|-d,+disp|-disp)", buffer.toString()); 160 } 161 162 public void testAppendUsage_NoAlias() { 163 final Option option = buildDisplaySwitch(); 164 final StringBuffer buffer = new StringBuffer(); 165 final Set settings = new HashSet(DisplaySetting.ALL); 166 settings.remove(DisplaySetting.DISPLAY_ALIASES); 167 option.appendUsage(buffer, settings, null); 168 169 assertEquals("+display|-display", buffer.toString()); 170 } 171 172 public void testAppendUsage_NoDisabled() { 173 final Option option = buildDisplaySwitch(); 174 final StringBuffer buffer = new StringBuffer(); 175 final Set settings = new HashSet(DisplaySetting.ALL); 176 settings.remove(DisplaySetting.DISPLAY_SWITCH_DISABLED); 177 option.appendUsage(buffer, settings, null); 178 179 assertEquals("+display (+d,+disp)", buffer.toString()); 180 } 181 182 public void testAppendUsage_NoEnabled() { 183 final Option option = buildDisplaySwitch(); 184 final StringBuffer buffer = new StringBuffer(); 185 final Set settings = new HashSet(DisplaySetting.ALL); 186 settings.remove(DisplaySetting.DISPLAY_SWITCH_ENABLED); 187 option.appendUsage(buffer, settings, null); 188 189 assertEquals("-display (-d,-disp)", buffer.toString()); 190 } 191 192 public void testAppendUsage_NoDisabledOrEnabled() { 193 final Option option = buildDisplaySwitch(); 194 final StringBuffer buffer = new StringBuffer(); 195 final Set settings = new HashSet(DisplaySetting.ALL); 196 settings.remove(DisplaySetting.DISPLAY_SWITCH_DISABLED); 197 settings.remove(DisplaySetting.DISPLAY_SWITCH_ENABLED); 198 option.appendUsage(buffer, settings, null); 199 200 assertEquals("+display (+d,+disp)", buffer.toString()); 201 } 202 203 /* 204 * (non-Javadoc) 205 * 206 * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() 207 */ 208 public void testGetPreferredName() { 209 final Option option = buildDisplaySwitch(); 210 assertEquals("+display", option.getPreferredName()); 211 } 212 213 /* 214 * (non-Javadoc) 215 * 216 * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() 217 */ 218 public void testGetDescription() { 219 final Option option = buildDisplaySwitch(); 220 assertEquals("Sets whether to display to screen", option.getDescription()); 221 } 222 223 public void testNullPreferredName() { 224 try { 225 new Switch("+", "-", null, null, "Sets whether to display to screen", true, null, null, 226 'd', null); 227 } catch (IllegalArgumentException exp) { 228 assertEquals("wrong exception message", 229 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT), 230 exp.getMessage()); 231 } 232 } 233 234 public void testEmptyPreferredName() { 235 try { 236 new Switch("+", "-", "", null, "Sets whether to display to screen", true, null, null, 237 'd', null); 238 } catch (IllegalArgumentException exp) { 239 assertEquals("wrong exception message", 240 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT), 241 exp.getMessage()); 242 } 243 } 244 245 public void testNullAliases() { 246 try { 247 new Switch("+", "-", "display", null, "Sets whether to display to screen", true, null, 248 null, 'd', null); 249 } catch (IllegalArgumentException exp) { 250 assertEquals("wrong exception message", 251 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT), 252 exp.getMessage()); 253 } 254 } 255 256 public void testNullEnablePrefix() { 257 try { 258 new Switch(null, "-", "display", null, "Sets whether to display to screen", true, null, 259 null, 'd', null); 260 } catch (IllegalArgumentException exp) { 261 assertEquals("wrong exception message", 262 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_NO_ENABLED_PREFIX), 263 exp.getMessage()); 264 } 265 } 266 267 public void testNullDisablePrefix() { 268 try { 269 new Switch("+", null, "display", null, "Sets whether to display to screen", true, null, 270 null, 'd', null); 271 } catch (IllegalArgumentException exp) { 272 assertEquals("wrong exception message", 273 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_NO_DISABLED_PREFIX), 274 exp.getMessage()); 275 } 276 } 277 278 public void testEnabledPrefixStartsWithDisabledPrefix() { 279 try { 280 new Switch("-", "-", "display", null, "Sets whether to display to screen", true, null, 281 null, 'd', null); 282 } catch (IllegalArgumentException exp) { 283 assertEquals("wrong exception message", 284 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ENABLED_STARTS_WITH_DISABLED), 285 exp.getMessage()); 286 } 287 } 288 289 public void testDisabledPrefixStartsWithEnabledPrefix() { 290 try { 291 new Switch("o", "on", "display", null, "Sets whether to display to screen", true, null, 292 null, 'd', null); 293 } catch (IllegalArgumentException exp) { 294 assertEquals("wrong exception message", 295 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_DISABLED_STARTWS_WITH_ENABLED), 296 exp.getMessage()); 297 } 298 } 299 300 /* 301 * (non-Javadoc) 302 * 303 * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() 304 */ 305 public void testHelpLines() { 306 // TODO Auto-generated method stub 307 } 308}