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; 030 031/** 032 * @author roberto 033 * 034 * To change the template for this generated type comment go to 035 * Window>Preferences>Java>Code Generation>Code and Comments 036 */ 037public class DefaultOptionTest extends ParentTestCase { 038 039 public static DefaultOption buildHelpOption() { 040 final Set aliases = new HashSet(list("-h", "-?")); 041 return new DefaultOption( 042 "-", 043 "--", 044 true, 045 "--help", 046 "Displays the help", 047 aliases, 048 aliases, 049 false, 050 null, 051 null, 052 'h'); 053 } 054 055 public static DefaultOption buildXOption() { 056 return new DefaultOption( 057 "-", 058 "--", 059 true, 060 "-X", 061 "This is needed", 062 null, 063 null, 064 true, 065 null, 066 null, 067 'X'); 068 } 069 070 /* 071 * (non-Javadoc) 072 * 073 * @see org.apache.commons.cli2.ParentTestCase#testProcessParent() 074 */ 075 public void testProcessParent() throws OptionException { 076 final DefaultOption option = buildHelpOption(); 077 final List args = list("--help"); 078 final WriteableCommandLine commandLine = commandLine(option, args); 079 final ListIterator iterator = args.listIterator(); 080 option.processParent(commandLine, iterator); 081 082 assertFalse(iterator.hasNext()); 083 assertTrue(commandLine.hasOption(option)); 084 assertTrue(commandLine.hasOption("--help")); 085 assertTrue(commandLine.hasOption("-?")); 086 assertTrue(commandLine.getValues(option).isEmpty()); 087 } 088 089 public void testProcessParent_Burst() throws OptionException { 090 final DefaultOption option = buildHelpOption(); 091 final List args = list("-help"); 092 final WriteableCommandLine commandLine = commandLine(option, args); 093 final ListIterator iterator = args.listIterator(); 094 option.processParent(commandLine, iterator); 095 096 assertEquals("-elp", iterator.next()); 097 assertFalse(iterator.hasNext()); 098 assertTrue(commandLine.hasOption(option)); 099 assertTrue(commandLine.hasOption("--help")); 100 assertTrue(commandLine.hasOption("-?")); 101 assertTrue(commandLine.getValues(option).isEmpty()); 102 } 103 104 /* 105 * (non-Javadoc) 106 * 107 * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() 108 */ 109 public void testCanProcess() { 110 final DefaultOption option = buildHelpOption(); 111 assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-?")); 112 } 113 114 public void testCanProcess_BadMatch() { 115 final DefaultOption option = buildHelpOption(); 116 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-H")); 117 } 118 119 /* 120 * (non-Javadoc) 121 * 122 * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() 123 */ 124 public void testPrefixes() { 125 final DefaultOption option = buildHelpOption(); 126 assertContentsEqual(list("-", "--"), option.getPrefixes()); 127 } 128 129 /* 130 * (non-Javadoc) 131 * 132 * @see org.apache.commons.cli2.OptionTestCase#testProcess() 133 */ 134 public void testProcess() { 135 // TODO Auto-generated method stub 136 137 } 138 139 /* 140 * (non-Javadoc) 141 * 142 * @see org.apache.commons.cli2.OptionTestCase#testTriggers() 143 */ 144 public void testTriggers() { 145 final DefaultOption option = buildHelpOption(); 146 assertContentsEqual(list("-?", "-h", "--help"), option.getTriggers()); 147 } 148 149 /* 150 * (non-Javadoc) 151 * 152 * @see org.apache.commons.cli2.OptionTestCase#testValidate() 153 */ 154 public void testValidate() { 155 final Parent option = buildXOption(); 156 final WriteableCommandLine commandLine = commandLine(option, list()); 157 158 try { 159 option.validate(commandLine); 160 fail("Missing an option"); 161 } 162 catch (OptionException moe) { 163 assertSame(option, moe.getOption()); 164 } 165 } 166 167 /* 168 * (non-Javadoc) 169 * 170 * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() 171 */ 172 public void testAppendUsage() { 173 final Option option = buildHelpOption(); 174 final StringBuffer buffer = new StringBuffer(); 175 option.appendUsage(buffer, DisplaySetting.ALL, null); 176 177 assertEquals("[--help (-?,-h)]", buffer.toString()); 178 } 179 180 public void testAppendUsage_NoOptional() { 181 final Option option = buildHelpOption(); 182 final StringBuffer buffer = new StringBuffer(); 183 final Set settings = new HashSet(DisplaySetting.ALL); 184 settings.remove(DisplaySetting.DISPLAY_OPTIONAL); 185 option.appendUsage(buffer, settings, null); 186 187 assertEquals("--help (-?,-h)", buffer.toString()); 188 } 189 190 public void testAppendUsage_NoAlias() { 191 final Option option = buildHelpOption(); 192 final StringBuffer buffer = new StringBuffer(); 193 final Set settings = new HashSet(DisplaySetting.ALL); 194 settings.remove(DisplaySetting.DISPLAY_ALIASES); 195 option.appendUsage(buffer, settings, null); 196 197 assertEquals("[--help]", buffer.toString()); 198 } 199 200 /* 201 * (non-Javadoc) 202 * 203 * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() 204 */ 205 public void testGetPreferredName() { 206 final Option option = buildHelpOption(); 207 assertEquals("--help", option.getPreferredName()); 208 } 209 210 /* 211 * (non-Javadoc) 212 * 213 * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() 214 */ 215 public void testGetDescription() { 216 final Option option = buildHelpOption(); 217 assertEquals("Displays the help", option.getDescription()); 218 } 219 /* 220 * (non-Javadoc) 221 * 222 * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() 223 */ 224 public void testHelpLines() { 225 // TODO Auto-generated method stub 226 } 227}