| 1 | package org.europa.together.utils; | |
| 2 | ||
| 3 | import java.time.ZonedDateTime; | |
| 4 | import java.util.List; | |
| 5 | import org.europa.together.application.LogbackLogger; | |
| 6 | import org.europa.together.business.Logger; | |
| 7 | import org.europa.together.domain.LogLevel; | |
| 8 | ||
| 9 | /** | |
| 10 | * A simple RexEx Validator.<br> | |
| 11 | * ### REGEX VALIDATION PATTERN<br> | |
| 12 | * ### * = 0 or more quantifier<br> | |
| 13 | * ### + = 1 or more quantifier<br> | |
| 14 | * ### ? = 0 or 1 quantifier | |
| 15 | */ | |
| 16 | public final class Validator { | |
| 17 | ||
| 18 | private static final Logger LOGGER = new LogbackLogger(Validator.class); | |
| 19 | ||
| 20 | private static final int INT_10 = 10; | |
| 21 | ||
| 22 | /** | |
| 23 | * Constructor. | |
| 24 | */ | |
| 25 | private Validator() { | |
| 26 | throw new UnsupportedOperationException(); | |
| 27 | } | |
| 28 | ||
| 29 | /** | |
| 30 | * Printable ASCII character: a-z A-Z. | |
| 31 | */ | |
| 32 | public static final String ASCII_CHARACTER = "[a-zA-Z ]+"; | |
| 33 | ||
| 34 | /** | |
| 35 | * Boolean. 0/1 true/false TRUE/FALSE | |
| 36 | */ | |
| 37 | public static final String BOOLEAN = "(0|1)|(t|T)(r|R)(u|U)(e|E)|(f|F)(a|A)(l|L)(s|S)(e|E)"; | |
| 38 | ||
| 39 | /** | |
| 40 | * Digits form 0-9. | |
| 41 | */ | |
| 42 | public static final String DIGIT = "[0-9 ]+"; | |
| 43 | ||
| 44 | /** | |
| 45 | * Floating point. | |
| 46 | */ | |
| 47 | public static final String FLOATING_POINT = "-?[0-9 ]+(.[0-9]+)?"; | |
| 48 | ||
| 49 | /** | |
| 50 | * Letters a-z A-Z upper case and lower case. No digits, space or special | |
| 51 | * characters. | |
| 52 | */ | |
| 53 | public static final String LETTERS = "[a-zA-Z]+"; | |
| 54 | ||
| 55 | /** | |
| 56 | * RGB Color schema in HEX: #000000 to #ffffff. | |
| 57 | */ | |
| 58 | public static final String RGB_COLOR | |
| 59 | = "#[0-9a-fA-F]{3,3}([0-9a-fA-F]{3,3})?"; | |
| 60 | ||
| 61 | /** | |
| 62 | * Text. | |
| 63 | */ | |
| 64 | public static final String TEXT | |
| 65 | = "(\\w|\\s|\\d|[¬&§$@€# ?!.,;_Â$Ââ~<>:=_+*/%\\-\\(\\)\\{\\}\\[\\]])*"; | |
| 66 | ||
| 67 | /** | |
| 68 | * Time in 24 hour format: 00:00 to 23:59. | |
| 69 | */ | |
| 70 | public static final String TIME_24H | |
| 71 | = "((0[0-9])|(1[0-9])|(2[0-3])):((0[0-9])|([1-5][0-9]))"; | |
| 72 | ||
| 73 | /** | |
| 74 | * IP Adress (Version 4) with optional port e. g.: 127.0.0.1:80 | |
| 75 | * (1-255).(0-255).(0-255).(0-255):(1-65535) | |
| 76 | */ | |
| 77 | public static final String IP4_ADDRESS | |
| 78 | = "(localhost|" | |
| 79 | + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 1 | |
| 80 | + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 2 | |
| 81 | + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 3 | |
| 82 | + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))" // 4 | |
| 83 | // Optinal Port | |
| 84 | + "(:[1-5]?[0-9]{0,4}" // 1-59.999 | |
| 85 | + "|:6[0-4][0-9][0-9][0-9]" // 60.000-64.999 | |
| 86 | + "|:65[0-4][0-9][0-9]" // 65.000-65.499 | |
| 87 | + "|:655[0-2][0-9]" // 65.500-65.529 | |
| 88 | + "|:6553[0-5])?"; // 65.530-65.535 | |
| 89 | ||
| 90 | /** | |
| 91 | * General E-Mail Regex (RFC 5322 Official Standard. | |
| 92 | */ | |
| 93 | public static final String E_MAIL_ADDRESS = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]" | |
| 94 | + "+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:" | |
| 95 | + "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]" | |
| 96 | + "|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")" | |
| 97 | + "@" | |
| 98 | + "(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)" | |
| 99 | + "+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]" | |
| 100 | + "|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]" | |
| 101 | + "|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:" | |
| 102 | + "(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]" | |
| 103 | + "|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; | |
| 104 | ||
| 105 | /** | |
| 106 | * Version number based on semantic versioning. Major.Minor.Patch-SNAPSHOT : | |
| 107 | * 1.0; 1.0-SNAPSHOT; 1.0.0-SNAPSHOT :: 000.000.000-ABCDEFGHIJ | |
| 108 | */ | |
| 109 | public static final String SEMANTIC_VERSION_NUMBER | |
| 110 | = "[1-9][0-9]*" | |
| 111 | + "(\\.[0-9]|\\.[1-9][0-9]*)?" | |
| 112 | + "(\\.[0-9]|\\.[1-9][0-9]*)?" | |
| 113 | + "(-?[A-Za-z]){0,10}"; | |
| 114 | ||
| 115 | /** | |
| 116 | * Check if the variable test inside the range of the borders min and max. | |
| 117 | * The borders and all numbers between are allowed values for the variable. | |
| 118 | * | |
| 119 | * @param test as Integer | |
| 120 | * @param min as Integer | |
| 121 | * @param max as Integer | |
| 122 | * @return true on success | |
| 123 | */ | |
| 124 | public static boolean isIntegerInRange(final int test, final int min, final int max) { | |
| 125 | boolean check = false; | |
| 126 |
4
1. isIntegerInRange : negated conditional → KILLED 2. isIntegerInRange : changed conditional boundary → KILLED 3. isIntegerInRange : negated conditional → KILLED 4. isIntegerInRange : changed conditional boundary → KILLED |
if (test >= min && test <= max) { |
| 127 | check = true; | |
| 128 | } else { | |
| 129 | LOGGER.log("Integer: " + test + " is not between [" + min + "," + max + "]", | |
| 130 | LogLevel.WARN); | |
| 131 | } | |
| 132 |
2
1. isIntegerInRange : replaced boolean return with false for org/europa/together/utils/Validator::isIntegerInRange → KILLED 2. isIntegerInRange : replaced boolean return with true for org/europa/together/utils/Validator::isIntegerInRange → KILLED |
return check; |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * Check if is a given date after the given boundary. If the check date | |
| 137 | * equal to the after boundary, the validation will fail.<br> | |
| 138 | * Sample: | |
| 139 | * <code>Validator.isDateAfter(new DateTime(), new DateTime(2015, 12, 31, 23, 59));</code> | |
| 140 | * Validate to TRUE because: now() is after 2015 | |
| 141 | * <br><br> | |
| 142 | * Convert java.util.Date to ZonedDateTime | |
| 143 | * <code>ZonedDateTime.from(Date.toInstant();</code> | |
| 144 | * | |
| 145 | * @param check as DateTime | |
| 146 | * @param after as DateTime | |
| 147 | * @return true on success | |
| 148 | */ | |
| 149 | public static boolean isDateAfter(final ZonedDateTime check, final ZonedDateTime after) { | |
| 150 | boolean success = false; | |
| 151 |
2
1. isDateAfter : negated conditional → KILLED 2. isDateAfter : negated conditional → KILLED |
if (check != null && after != null) { |
| 152 | success = check.isAfter(after); | |
| 153 | } | |
| 154 |
2
1. isDateAfter : replaced boolean return with true for org/europa/together/utils/Validator::isDateAfter → KILLED 2. isDateAfter : replaced boolean return with false for org/europa/together/utils/Validator::isDateAfter → KILLED |
return success; |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Check if is a given date before the given boundary. If the check date | |
| 159 | * equal to the before boundary, the validation will fail.<br> | |
| 160 | * Sample: | |
| 161 | * <code>Validator.isDateBefore(new DateTime(2015, 12, 31, 23, 59), new DateTime());</code> | |
| 162 | * Validate to TRUE because: 2015 is before now() | |
| 163 | * <br><br> | |
| 164 | * Convert java.util.Date to ZonedDateTime | |
| 165 | * <code>ZonedDateTime.from(Date.toInstant();</code> | |
| 166 | * | |
| 167 | * @param check as DateTime | |
| 168 | * @param before as DateTime | |
| 169 | * @return true on success | |
| 170 | */ | |
| 171 | public static boolean isDateBefore(final ZonedDateTime check, final ZonedDateTime before) { | |
| 172 | boolean success = false; | |
| 173 |
2
1. isDateBefore : negated conditional → KILLED 2. isDateBefore : negated conditional → KILLED |
if (check != null && before != null) { |
| 174 | success = check.isBefore(before); | |
| 175 | } | |
| 176 |
2
1. isDateBefore : replaced boolean return with false for org/europa/together/utils/Validator::isDateBefore → KILLED 2. isDateBefore : replaced boolean return with true for org/europa/together/utils/Validator::isDateBefore → KILLED |
return success; |
| 177 | } | |
| 178 | ||
| 179 | /** | |
| 180 | * Test if a given date is inside a range between a min and max. The | |
| 181 | * boundaries are inside the range. | |
| 182 | * <br><br> | |
| 183 | * Convert java.util.Date to ZonedDateTime | |
| 184 | * <code>ZonedDateTime.from(Date.toInstant();</code> | |
| 185 | * | |
| 186 | * @param check as DateTime | |
| 187 | * @param min as DateTime | |
| 188 | * @param max as DateTime | |
| 189 | * @return true on success | |
| 190 | */ | |
| 191 | public static boolean isDateInRange( | |
| 192 | final ZonedDateTime check, final ZonedDateTime min, final ZonedDateTime max) { | |
| 193 | boolean success = false; | |
| 194 |
3
1. isDateInRange : negated conditional → KILLED 2. isDateInRange : negated conditional → KILLED 3. isDateInRange : negated conditional → KILLED |
if (check != null && min != null && max != null) { |
| 195 |
2
1. isDateInRange : negated conditional → KILLED 2. isDateInRange : negated conditional → KILLED |
if (isDateBefore(check, max) && isDateAfter(check, min)) { |
| 196 | success = true; | |
| 197 | } else { | |
| 198 | LOGGER.log("Date: " + check.toString() + " is not in range.", LogLevel.WARN); | |
| 199 | } | |
| 200 | } | |
| 201 |
2
1. isDateInRange : replaced boolean return with false for org/europa/together/utils/Validator::isDateInRange → KILLED 2. isDateInRange : replaced boolean return with true for org/europa/together/utils/Validator::isDateInRange → KILLED |
return success; |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * Test if a 10 or 13 digit ISBN number is valid. | |
| 206 | * | |
| 207 | * @param isbn as String | |
| 208 | * @return true on success | |
| 209 | */ | |
| 210 | public static boolean isIsbn(final String isbn) | |
| 211 | throws NumberFormatException { | |
| 212 | boolean success = false; | |
| 213 | final int value09 = 9; | |
| 214 | final int value10 = 10; | |
| 215 | final int value11 = 11; | |
| 216 | final int value13 = 13; | |
| 217 | int tmp = 0; | |
| 218 | String check = isbn.replaceAll("-", ""); | |
| 219 | int size = check.length(); | |
| 220 |
3
1. isIsbn : negated conditional → KILLED 2. isIsbn : negated conditional → KILLED 3. isIsbn : negated conditional → KILLED |
if (StringUtils.isEmpty(isbn) || size != value10 && size != value13) { |
| 221 | throw new NumberFormatException( | |
| 222 | "The format has not the correct lenght of a valid ISBN."); | |
| 223 | } | |
| 224 | ||
| 225 |
2
1. isIsbn : negated conditional → KILLED 2. isIsbn : changed conditional boundary → KILLED |
for (int i = 0; i < size; i++) { |
| 226 |
1
1. isIsbn : Replaced integer addition with subtraction → KILLED |
String element = check.substring(i, i + 1); |
| 227 | ||
| 228 | int digit; | |
| 229 |
3
1. isIsbn : negated conditional → KILLED 2. isIsbn : negated conditional → KILLED 3. isIsbn : negated conditional → KILLED |
if (size == value10 && i == value09 && element.equalsIgnoreCase("x")) { |
| 230 | digit = value10; | |
| 231 | } else { | |
| 232 | digit = Integer.parseInt(element); | |
| 233 | } | |
| 234 | ||
| 235 |
1
1. isIsbn : negated conditional → KILLED |
if (size == value10) { |
| 236 |
3
1. isIsbn : Replaced integer addition with subtraction → SURVIVED 2. isIsbn : Replaced integer multiplication with division → KILLED 3. isIsbn : Replaced integer subtraction with addition → KILLED |
tmp += digit * (value10 - i); |
| 237 | LOGGER.log("(ISBN-10 SUM: " + tmp, LogLevel.DEBUG); | |
| 238 | } else { | |
| 239 |
2
1. isIsbn : Replaced integer modulus with multiplication → SURVIVED 2. isIsbn : negated conditional → KILLED |
if (i % 2 == 0) { |
| 240 |
1
1. isIsbn : Replaced integer addition with subtraction → KILLED |
tmp += digit; |
| 241 | } else { | |
| 242 |
2
1. isIsbn : Replaced integer multiplication with division → KILLED 2. isIsbn : Replaced integer addition with subtraction → KILLED |
tmp += digit * 3; |
| 243 | } | |
| 244 | LOGGER.log("(ISBN-13 SUM: " + tmp, LogLevel.DEBUG); | |
| 245 | } | |
| 246 | } | |
| 247 | ||
| 248 |
1
1. isIsbn : negated conditional → KILLED |
if (size == value10) { |
| 249 |
2
1. isIsbn : Replaced integer modulus with multiplication → KILLED 2. isIsbn : negated conditional → KILLED |
if (tmp % value11 == 0) { |
| 250 | success = true; | |
| 251 | } | |
| 252 | } else { | |
| 253 |
2
1. isIsbn : negated conditional → KILLED 2. isIsbn : Replaced integer modulus with multiplication → KILLED |
if (tmp % value10 == 0) { |
| 254 | success = true; | |
| 255 | } | |
| 256 | } | |
| 257 |
2
1. isIsbn : replaced boolean return with true for org/europa/together/utils/Validator::isIsbn → KILLED 2. isIsbn : replaced boolean return with false for org/europa/together/utils/Validator::isIsbn → KILLED |
return success; |
| 258 | } | |
| 259 | ||
| 260 | /** | |
| 261 | * Luhns Algortihm is secure way to find for an number it's corrsponding | |
| 262 | * checksum (validation key). This algorithm is used by Visa, Mastercard and | |
| 263 | * others to verify the credit card number.This mechanism can also used to | |
| 264 | * verify gift card codes.<br> | |
| 265 | * You need to place the number wich have verified and it's corresponding | |
| 266 | * checksum. If the validation succeed by the correct checksum the method | |
| 267 | * return tue. | |
| 268 | * | |
| 269 | * @param numbers as Integer | |
| 270 | * @return true on success | |
| 271 | */ | |
| 272 | public static boolean luhnNumberChecksum(final String numbers) { | |
| 273 | boolean success = false; | |
| 274 | List<Integer> digits = StringUtils.splitDigitStringToIntegerList(numbers); | |
| 275 | ||
| 276 | int sum = 0; | |
| 277 | int counter = 1; | |
| 278 | for (Integer digit : digits) { | |
| 279 | ||
| 280 |
2
1. luhnNumberChecksum : Replaced integer modulus with multiplication → KILLED 2. luhnNumberChecksum : negated conditional → KILLED |
if (counter % 2 == 0) { |
| 281 |
1
1. luhnNumberChecksum : Replaced integer addition with subtraction → KILLED |
sum += digit; |
| 282 | ||
| 283 | } else { | |
| 284 |
1
1. luhnNumberChecksum : Replaced integer multiplication with division → SURVIVED |
int result = digit * 2; |
| 285 |
2
1. luhnNumberChecksum : negated conditional → KILLED 2. luhnNumberChecksum : changed conditional boundary → KILLED |
if (result >= INT_10) { |
| 286 | List<Integer> stack = StringUtils.splitDigitStringToIntegerList( | |
| 287 | String.valueOf(result)); | |
| 288 | int temporal = 0; | |
| 289 | for (Integer element : stack) { | |
| 290 |
1
1. luhnNumberChecksum : Replaced integer addition with subtraction → KILLED |
temporal += element; |
| 291 | } | |
| 292 |
1
1. luhnNumberChecksum : Replaced integer addition with subtraction → KILLED |
sum += temporal; |
| 293 | ||
| 294 | } else { | |
| 295 |
1
1. luhnNumberChecksum : Replaced integer addition with subtraction → KILLED |
sum += result; |
| 296 | } | |
| 297 | } | |
| 298 | LOGGER.log("CNT: " + counter + "\tDigit: " + digit + "\tSUM: " + sum, LogLevel.DEBUG); | |
| 299 |
1
1. luhnNumberChecksum : Changed increment from 1 to -1 → SURVIVED |
counter++; |
| 300 | } | |
| 301 | ||
| 302 |
2
1. luhnNumberChecksum : Replaced integer modulus with multiplication → KILLED 2. luhnNumberChecksum : negated conditional → KILLED |
if (sum % INT_10 == 0) { |
| 303 | success = true; | |
| 304 | } | |
| 305 |
2
1. luhnNumberChecksum : replaced boolean return with false for org/europa/together/utils/Validator::luhnNumberChecksum → KILLED 2. luhnNumberChecksum : replaced boolean return with true for org/europa/together/utils/Validator::luhnNumberChecksum → KILLED |
return success; |
| 306 | } | |
| 307 | ||
| 308 | /** | |
| 309 | * Validate a String against an regular expression and return true if the | |
| 310 | * String matches the RegEx. | |
| 311 | * | |
| 312 | * @param content as String | |
| 313 | * @param regEx as String | |
| 314 | * @return true on success | |
| 315 | */ | |
| 316 | public static boolean validate(final String content, final String regEx) { | |
| 317 | boolean test = false; | |
| 318 |
1
1. validate : negated conditional → KILLED |
if (content.matches(regEx)) { |
| 319 | test = true; | |
| 320 | } else { | |
| 321 | String msg = "validate('" + regEx + "') did not match " + content; | |
| 322 | LOGGER.log(msg, LogLevel.WARN); | |
| 323 | } | |
| 324 |
2
1. validate : replaced boolean return with false for org/europa/together/utils/Validator::validate → KILLED 2. validate : replaced boolean return with true for org/europa/together/utils/Validator::validate → KILLED |
return test; |
| 325 | } | |
| 326 | } | |
Mutations | ||
| 126 |
1.1 2.2 3.3 4.4 |
|
| 132 |
1.1 2.2 |
|
| 151 |
1.1 2.2 |
|
| 154 |
1.1 2.2 |
|
| 173 |
1.1 2.2 |
|
| 176 |
1.1 2.2 |
|
| 194 |
1.1 2.2 3.3 |
|
| 195 |
1.1 2.2 |
|
| 201 |
1.1 2.2 |
|
| 220 |
1.1 2.2 3.3 |
|
| 225 |
1.1 2.2 |
|
| 226 |
1.1 |
|
| 229 |
1.1 2.2 3.3 |
|
| 235 |
1.1 |
|
| 236 |
1.1 2.2 3.3 |
|
| 239 |
1.1 2.2 |
|
| 240 |
1.1 |
|
| 242 |
1.1 2.2 |
|
| 248 |
1.1 |
|
| 249 |
1.1 2.2 |
|
| 253 |
1.1 2.2 |
|
| 257 |
1.1 2.2 |
|
| 280 |
1.1 2.2 |
|
| 281 |
1.1 |
|
| 284 |
1.1 |
|
| 285 |
1.1 2.2 |
|
| 290 |
1.1 |
|
| 292 |
1.1 |
|
| 295 |
1.1 |
|
| 299 |
1.1 |
|
| 302 |
1.1 2.2 |
|
| 305 |
1.1 2.2 |
|
| 318 |
1.1 |
|
| 324 |
1.1 2.2 |