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; 018 019import java.util.Arrays; 020import java.util.Collections; 021import java.util.List; 022 023import junit.framework.TestCase; 024 025import org.apache.commons.cli2.builder.ArgumentBuilder; 026import org.apache.commons.cli2.builder.SwitchBuilder; 027import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; 028 029/** 030 * Tests the interaction of command line values and defaults supplied in different ways. 031 * 032 * Tests marked _Parsed involve values parsed from a command line. 033 * 034 * Tests marked _Method involve defaults supplied in the query method. 035 * 036 * Tests marked _Option involce defaults specified in the model. 037 * 038 * @author Rob Oxspring 039 */ 040public class CommandLineDefaultsTest extends TestCase { 041 042 /* 043 * utils to grab the default from the method 044 */ 045 046 private Object methodSwitch(WriteableCommandLine cl, Option o, Boolean bool) { 047 return cl.getSwitch(o, bool); 048 } 049 050 private Object methodSwitchNull(WriteableCommandLine cl, Option o) { 051 return methodSwitch(cl, o, null); 052 } 053 054 private Object methodSwitchOff(WriteableCommandLine cl, Option o) { 055 return methodSwitch(cl, o, Boolean.FALSE); 056 } 057 058 private Object methodSwitchOn(WriteableCommandLine cl, Option o) { 059 return methodSwitch(cl, o, Boolean.TRUE); 060 } 061 062 private Object methodValueMissing(WriteableCommandLine cl, Option o) { 063 return cl.getValue(o); 064 } 065 066 private Object methodValuePresent(WriteableCommandLine cl, Option o) { 067 return cl.getValue(o, "method"); 068 } 069 070 /* 071 * utils to grab the default from the option model 072 */ 073 074 private Option optionSwitch(Boolean bool) { 075 return new SwitchBuilder().withName("switch").withSwitchDefault(bool) 076 .create(); 077 } 078 079 private Option optionSwitchNull() { 080 return optionSwitch(null); 081 } 082 083 private Option optionSwitchOff() { 084 return optionSwitch(Boolean.FALSE); 085 } 086 087 private Option optionSwitchOn() { 088 return optionSwitch(Boolean.TRUE); 089 } 090 091 private Option optionValueMissing() { 092 return new ArgumentBuilder().create(); 093 } 094 095 private Option optionValuePresent() { 096 return new ArgumentBuilder().withDefaults( 097 Arrays.asList(new String[] { "option" })).create(); 098 } 099 100 /* 101 * utils to grab the input from the command line 102 */ 103 104 private WriteableCommandLine parsedSwitch(Option o, Boolean bool) { 105 final List args; 106 if (bool == null) { 107 args = Collections.EMPTY_LIST; 108 } else { 109 args = Collections 110 .singletonList(String.valueOf(bool).toLowerCase()); 111 } 112 WriteableCommandLine cl = new WriteableCommandLineImpl(o, args); 113 o.defaults(cl); 114 if (bool != null) { 115 cl.addSwitch(o, bool.booleanValue()); 116 } 117 return cl; 118 } 119 120 private WriteableCommandLine parsedSwitchNull(Option o) { 121 return parsedSwitch(o, null); 122 } 123 124 private WriteableCommandLine parsedSwitchOn(Option o) { 125 return parsedSwitch(o, Boolean.TRUE); 126 } 127 128 private WriteableCommandLine parsedValueMissing(Option o) { 129 WriteableCommandLine cl = new WriteableCommandLineImpl(o, 130 Collections.EMPTY_LIST); 131 o.defaults(cl); 132 return cl; 133 } 134 135 private WriteableCommandLine parsedValuePresent(Option o) { 136 WriteableCommandLine cl = new WriteableCommandLineImpl(o, Arrays 137 .asList(new String[] { "parsed" })); 138 o.defaults(cl); 139 cl.addValue(o, "parsed"); 140 return cl; 141 } 142 143 /* 144 * tests 145 */ 146 147 public void testSwitch_Method() { 148 final Option o = optionSwitchNull(); 149 final WriteableCommandLine cl = parsedSwitchNull(o); 150 final Object v = methodSwitchOn(cl, o); 151 assertEquals(Boolean.TRUE, v); 152 } 153 154 public void testSwitch_Method_Option() { 155 final Option o = optionSwitchOff(); 156 final WriteableCommandLine cl = parsedSwitchNull(o); 157 final Object v = methodSwitchOn(cl, o); 158 assertEquals(Boolean.TRUE, v); 159 } 160 161 public void testSwitch_Option() { 162 final Option o = optionSwitchOn(); 163 final WriteableCommandLine cl = parsedSwitchNull(o); 164 final Object v = methodSwitchNull(cl, o); 165 assertEquals(Boolean.TRUE, v); 166 } 167 168 public void testSwitch_Parsed() { 169 final Option o = optionSwitchNull(); 170 final WriteableCommandLine cl = parsedSwitchOn(o); 171 final Object v = methodSwitchNull(cl, o); 172 assertEquals(Boolean.TRUE, v); 173 } 174 175 public void testSwitch_Parsed_Method() { 176 final Option o = optionSwitchOff(); 177 final WriteableCommandLine cl = parsedSwitchOn(o); 178 final Object v = methodSwitchNull(cl, o); 179 assertEquals(Boolean.TRUE, v); 180 } 181 182 public void testSwitch_Parsed_Method_Option() { 183 final Option o = optionSwitchOff(); 184 final WriteableCommandLine cl = parsedSwitchOn(o); 185 final Object v = methodSwitchOff(cl, o); 186 assertEquals(Boolean.TRUE, v); 187 } 188 189 public void testSwitch_Parsed_Option() { 190 final Option o = optionSwitchOff(); 191 final WriteableCommandLine cl = parsedSwitchOn(o); 192 final Object v = methodSwitchNull(cl, o); 193 assertEquals(Boolean.TRUE, v); 194 } 195 196 public void testValues() { 197 final Option o = optionValueMissing(); 198 final WriteableCommandLine cl = parsedValueMissing(o); 199 final Object v = methodValueMissing(cl, o); 200 assertNull(v); 201 } 202 203 public void testValues_Method() { 204 final Option o = optionValueMissing(); 205 final WriteableCommandLine cl = parsedValueMissing(o); 206 final Object v = methodValuePresent(cl, o); 207 assertEquals("method", v); 208 } 209 210 public void testValues_Method_Option() { 211 final Option o = optionValuePresent(); 212 final WriteableCommandLine cl = parsedValueMissing(o); 213 final Object v = methodValuePresent(cl, o); 214 assertEquals("method", v); 215 } 216 217 public void testValues_Option() { 218 final Option o = optionValuePresent(); 219 final WriteableCommandLine cl = parsedValueMissing(o); 220 final Object v = methodValueMissing(cl, o); 221 assertEquals("option", v); 222 } 223 224 public void testValues_Parsed() { 225 final Option o = optionValueMissing(); 226 final WriteableCommandLine cl = parsedValuePresent(o); 227 final Object v = methodValueMissing(cl, o); 228 assertEquals("parsed", v); 229 } 230 231 public void testValues_Parsed_Method() { 232 final Option o = optionValueMissing(); 233 final WriteableCommandLine cl = parsedValuePresent(o); 234 final Object v = methodValuePresent(cl, o); 235 assertEquals("parsed", v); 236 } 237 238 public void testValues_Parsed_Method_Option() { 239 final Option o = optionValuePresent(); 240 final WriteableCommandLine cl = parsedValuePresent(o); 241 final Object v = methodValuePresent(cl, o); 242 assertEquals("parsed", v); 243 } 244 245 public void testValues_Parsed_Option() { 246 final Option o = optionValuePresent(); 247 final WriteableCommandLine cl = parsedValuePresent(o); 248 final Object v = methodValueMissing(cl, o); 249 assertEquals("parsed", v); 250 } 251}