|
| 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 | + |
| 18 | +package org.apache.commons.cli; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.Arguments; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
| 33 | + |
| 34 | +public class OptionValidatorTest { |
| 35 | + |
| 36 | + /* |
| 37 | + * Exemplars of various types of characters |
| 38 | + */ |
| 39 | + |
| 40 | + private static final String LETTERS = "a\u00D1"; // a and Ñ |
| 41 | + |
| 42 | + // '\u0660' through '\u0669', Arabic-Indic digits, '\u06F0' through '\u06F9', |
| 43 | + // Extended Arabic-Indic digits |
| 44 | + // '\u0966' through '\u096F', Devanagari digits, '\uFF10' through '\uFF19', |
| 45 | + // Fullwidth digits |
| 46 | + private static final String DIGITS = "1\u0661\u06f2\u0968\uFF14"; |
| 47 | + |
| 48 | + private static final String CURRENCY = "€$"; |
| 49 | + |
| 50 | + // this is the complete puncutation set do not modify it as Character.isJavaIdentifierPart filters |
| 51 | + // the good and bad ones out in the setup. |
| 52 | + private static final String PUNCTUATION = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; |
| 53 | + |
| 54 | + private static final String COMBINING_MARK = "\u0303"; |
| 55 | + |
| 56 | + private static final String NON_SPACING_MARK = "\u0CBF"; |
| 57 | + |
| 58 | + private static final String IDENTIFIER_IGNORABLE = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008"; |
| 59 | + |
| 60 | + private static String acceptablePunctuation; |
| 61 | + |
| 62 | + private static String notAcceptablePunctuation; |
| 63 | + |
| 64 | + private static String additionalOptonChars; |
| 65 | + private static String additionalLongChars; |
| 66 | + |
| 67 | + private static String firstChars; |
| 68 | + private static String notFirstChars; |
| 69 | + |
| 70 | + private static String restChars; |
| 71 | + private static String notRestChars; |
| 72 | + |
| 73 | + @BeforeAll |
| 74 | + public static void setup() { |
| 75 | + StringBuilder sb = new StringBuilder(); |
| 76 | + StringBuilder sb2 = new StringBuilder(); |
| 77 | + int idx; |
| 78 | + |
| 79 | + for (char c : PUNCTUATION.toCharArray()) { |
| 80 | + if (Character.isJavaIdentifierPart(c)) { |
| 81 | + sb.append(c); |
| 82 | + } else { |
| 83 | + sb2.append(c); |
| 84 | + } |
| 85 | + } |
| 86 | + acceptablePunctuation = sb.toString(); |
| 87 | + notAcceptablePunctuation = sb2.toString(); |
| 88 | + |
| 89 | + sb = new StringBuilder(); |
| 90 | + for (char c : OptionValidator.ADDITIONAL_LONG_CHARS) { |
| 91 | + sb.append(c); |
| 92 | + } |
| 93 | + additionalLongChars = sb.toString(); |
| 94 | + |
| 95 | + sb = new StringBuilder(); |
| 96 | + for (char c : OptionValidator.ADDITIONAL_OPTION_CHARS) { |
| 97 | + sb.append(c); |
| 98 | + } |
| 99 | + additionalOptonChars = sb.toString(); |
| 100 | + |
| 101 | + String javaIdentifierPart = LETTERS + DIGITS + CURRENCY + acceptablePunctuation + COMBINING_MARK |
| 102 | + + NON_SPACING_MARK + IDENTIFIER_IGNORABLE; |
| 103 | + |
| 104 | + firstChars = additionalOptonChars + javaIdentifierPart; |
| 105 | + |
| 106 | + sb = new StringBuilder(notAcceptablePunctuation).append(additionalLongChars); |
| 107 | + for (char c : OptionValidator.ADDITIONAL_OPTION_CHARS) { |
| 108 | + while ((idx = sb.indexOf(Character.toString(c))) > -1) { |
| 109 | + sb.deleteCharAt(idx); |
| 110 | + } |
| 111 | + } |
| 112 | + notFirstChars = sb.toString(); |
| 113 | + |
| 114 | + restChars = additionalLongChars + javaIdentifierPart; |
| 115 | + sb = new StringBuilder(notAcceptablePunctuation).append(additionalOptonChars); |
| 116 | + for (char c : OptionValidator.ADDITIONAL_LONG_CHARS) { |
| 117 | + while ((idx = sb.indexOf(Character.toString(c))) > -1) { |
| 118 | + sb.deleteCharAt(idx); |
| 119 | + } |
| 120 | + } |
| 121 | + notRestChars = sb.toString(); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testExclusivity() { |
| 127 | + /* since we modify acceptable chars by add and removing ADDITIONAL* chars we must verify that they do not exist in the |
| 128 | + * base javaIdentiferPart that is used in OptionValidator to validate basic characters */ |
| 129 | + for (char c : OptionValidator.ADDITIONAL_LONG_CHARS) { |
| 130 | + assertFalse(Character.isJavaIdentifierPart(c), () -> String.format("'%s' should not be in 'ADDITIONAL_LONG_CHARS", c)); |
| 131 | + } |
| 132 | + for (char c : OptionValidator.ADDITIONAL_OPTION_CHARS) { |
| 133 | + assertFalse(Character.isJavaIdentifierPart(c), () -> String.format("'%s' should not be in 'ADDITIONAL_OPTION_CHARS", c)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @ParameterizedTest(name = "{2}") |
| 138 | + @MethodSource("optionParameters") |
| 139 | + public void validateTest(final String str, final boolean expected, final String name) { |
| 140 | + if (expected) { |
| 141 | + assertEquals(str, OptionValidator.validate(str)); |
| 142 | + } else { |
| 143 | + assertThrows(IllegalArgumentException.class, () -> OptionValidator.validate(str)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static Stream<Arguments> optionParameters() { |
| 148 | + |
| 149 | + List<Arguments> args = new ArrayList<>(); |
| 150 | + |
| 151 | + args.add(Arguments.of("CamelCase", true, "Camel case error")); |
| 152 | + args.add(Arguments.of("Snake_case", true, "Snake case error")); |
| 153 | + args.add(Arguments.of("_leadingUnderscore", true, "Leading underscore error")); |
| 154 | + args.add(Arguments.of("kabob-case", true, "Kabob case error")); |
| 155 | + args.add(Arguments.of("-leadingDash", false, "Leading dash error")); |
| 156 | + args.add(Arguments.of("lowercase", true, "Lower case error")); |
| 157 | + args.add(Arguments.of("UPPERCASE", true, "Upper case error")); |
| 158 | + |
| 159 | + // build passing test cases |
| 160 | + for (char c : firstChars.toCharArray()) { |
| 161 | + String s = String.format("%sMoreText", c); |
| 162 | + args.add(Arguments.of(s, true, String.format("testing: First character '%s'", c))); |
| 163 | + } |
| 164 | + |
| 165 | + for (char c : restChars.toCharArray()) { |
| 166 | + String s = String.format("Some%sText", c); |
| 167 | + args.add(Arguments.of(s, true, String.format("testing: Middle character '%s'", c))); |
| 168 | + } |
| 169 | + |
| 170 | + // build failing test cases |
| 171 | + for (char c : notFirstChars.toCharArray()) { |
| 172 | + String s = String.format("%sMoreText", c); |
| 173 | + args.add(Arguments.of(s, false, String.format("testing: Bad first character '%s'", c))); |
| 174 | + } |
| 175 | + |
| 176 | + for (char c : notRestChars.toCharArray()) { |
| 177 | + String s = String.format("Some%sText", c); |
| 178 | + args.add(Arguments.of(s, false, String.format("testing: Bad middle character '%s'", c))); |
| 179 | + } |
| 180 | + |
| 181 | + return args.stream(); |
| 182 | + } |
| 183 | +} |
0 commit comments