|
5 | 5 | import org.testng.annotations.Test;
|
6 | 6 |
|
7 | 7 | import java.lang.reflect.Field;
|
| 8 | +import java.util.ArrayList; |
8 | 9 | import java.util.Arrays;
|
9 | 10 | import java.util.Collections;
|
10 | 11 | import java.util.List;
|
| 12 | +import java.util.Set; |
11 | 13 |
|
12 | 14 | public class NamedArgumentDefinitionUnitTest {
|
13 | 15 |
|
@@ -150,4 +152,123 @@ private Field getFieldForFieldName(final Object argContainer, final String field
|
150 | 152 | throw new IllegalArgumentException("Can't find field");
|
151 | 153 | }
|
152 | 154 |
|
| 155 | + public static class ArgumentLists { |
| 156 | + @Argument |
| 157 | + List<String> required; |
| 158 | + @Argument(optional = true) |
| 159 | + List<String> optional; |
| 160 | + @Argument |
| 161 | + List<String> defaultedRequired = new ArrayList<>(List.of("default")); |
| 162 | + @Argument(optional = true) |
| 163 | + List<String> defaultedOptional = new ArrayList<>(List.of("default")); |
| 164 | + } |
| 165 | + |
| 166 | + @DataProvider |
| 167 | + public Object[][] setArgumentValuesCollectionsTests() { |
| 168 | + return new Object[][]{ |
| 169 | + {"required", Set.of(), |
| 170 | + List.of("stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 171 | + {"optional", Set.of(), |
| 172 | + List.of("stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 173 | + {"defaultedRequired", Set.of(), |
| 174 | + List.of("stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 175 | + {"defaultedOptional", Set.of(), |
| 176 | + List.of("stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 177 | + {"required", Set.of(), |
| 178 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 179 | + {"optional", Set.of(), |
| 180 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 181 | + {"defaultedRequired", Set.of(), |
| 182 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 183 | + {"defaultedOptional", Set.of(), |
| 184 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 185 | + {"required", Set.of(), |
| 186 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 187 | + {"optional", Set.of(), |
| 188 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 189 | + {"defaultedRequired", Set.of(), |
| 190 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 191 | + {"defaultedOptional", Set.of(), |
| 192 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 193 | + {"optional", Set.of(), |
| 194 | + List.of("stuff", "more stuff", "null"), List.of()}, |
| 195 | + {"defaultedOptional", Set.of(), |
| 196 | + List.of("stuff", "more stuff", "null"), List.of()}, |
| 197 | + |
| 198 | + {"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 199 | + List.of("stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 200 | + {"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 201 | + List.of("stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 202 | + {"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 203 | + List.of("stuff", "more stuff"), List.of("default", "stuff", "more stuff")}, |
| 204 | + {"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 205 | + List.of("stuff", "more stuff"), List.of("default", "stuff", "more stuff")}, |
| 206 | + {"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 207 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 208 | + {"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 209 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 210 | + {"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 211 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 212 | + {"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 213 | + List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")}, |
| 214 | + {"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 215 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 216 | + {"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 217 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 218 | + {"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 219 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 220 | + {"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 221 | + List.of("stuff", "null", "more stuff"), List.of("more stuff")}, |
| 222 | + {"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 223 | + List.of("stuff", "more stuff", "null"), List.of()}, |
| 224 | + {"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 225 | + List.of("stuff", "more stuff", "null"), List.of()}, |
| 226 | + }; |
| 227 | + } |
| 228 | + |
| 229 | + @Test(dataProvider = "setArgumentValuesCollectionsTests") |
| 230 | + public void testSetArgumentValuesCollections( |
| 231 | + final String fieldName, |
| 232 | + final Set<CommandLineParserOptions> parserOptions, |
| 233 | + final List<String> newValues, |
| 234 | + final List<String> expectedValues |
| 235 | + ) throws IllegalAccessException { |
| 236 | + final ArgumentLists argLists = new ArgumentLists(); |
| 237 | + final Field field = getFieldForFieldName(argLists, fieldName); |
| 238 | + final NamedArgumentDefinition argDef = |
| 239 | + new NamedArgumentDefinition(field.getAnnotation(Argument.class), argLists, field, null); |
| 240 | + final CommandLineArgumentParser clp = |
| 241 | + new CommandLineArgumentParser(argLists, Collections.emptyList(), parserOptions); |
| 242 | + argDef.setArgumentValues(clp, System.out, newValues); |
| 243 | + Assert.assertEquals(field.get(argLists), expectedValues); |
| 244 | + } |
| 245 | + |
| 246 | + @DataProvider |
| 247 | + public Object[][] setArgumentValuesCollectionsFailures() { |
| 248 | + return new Object[][]{ |
| 249 | + {"required", Set.of(), |
| 250 | + List.of("stuff", "more stuff", "null")}, |
| 251 | + {"defaultedRequired", Set.of(), |
| 252 | + List.of("stuff", "more stuff", "null")}, |
| 253 | + {"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 254 | + List.of("stuff", "more stuff", "null")}, |
| 255 | + {"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS), |
| 256 | + List.of("stuff", "more stuff", "null")}, |
| 257 | + }; |
| 258 | + } |
| 259 | + |
| 260 | + @Test(dataProvider = "setArgumentValuesCollectionsFailures", expectedExceptions = CommandLineException.class) |
| 261 | + public void testSetArgumentValuesCollectionsFailures( |
| 262 | + final String fieldName, |
| 263 | + final Set<CommandLineParserOptions> parserOptions, |
| 264 | + final List<String> newValues |
| 265 | + ) { |
| 266 | + final ArgumentLists argLists = new ArgumentLists(); |
| 267 | + final Field field = getFieldForFieldName(argLists, fieldName); |
| 268 | + final NamedArgumentDefinition argDef = |
| 269 | + new NamedArgumentDefinition(field.getAnnotation(Argument.class), argLists, field, null); |
| 270 | + final CommandLineArgumentParser clp = |
| 271 | + new CommandLineArgumentParser(argLists, Collections.emptyList(), parserOptions); |
| 272 | + argDef.setArgumentValues(clp, System.out, newValues); |
| 273 | + } |
153 | 274 | }
|
0 commit comments