From f4be7054bc7fd84de66fae5e57e7564f50b94193 Mon Sep 17 00:00:00 2001 From: gabriel-logan Date: Sun, 7 Jul 2024 08:33:58 -0300 Subject: [PATCH] feat: Add multiform_validator package with CnpjValidator, CpfValidator, EmailValidator, and Validator classes --- .github/workflows/java-pr-check.yml | 8 +- packages/java/pom.xml | 4 +- .../java/io/multiform_validator/Ascii.java | 26 ----- .../java/io/multiform_validator/Base64.java | 27 ----- .../main/java/io/multiform_validator/CEP.java | 35 ------ .../java/io/multiform_validator/Cnpj.java | 70 ------------ .../io/multiform_validator/CnpjValidator.java | 63 +++++++++++ .../main/java/io/multiform_validator/Cpf.java | 76 ------------- .../io/multiform_validator/CpfValidator.java | 39 +++++++ .../io/multiform_validator/CreditCard.java | 101 ------------------ .../java/io/multiform_validator/Date.java | 33 ------ .../java/io/multiform_validator/Email.java | 69 ------------ .../multiform_validator/EmailValidator.java | 73 +++++++++++++ .../main/java/io/multiform_validator/MD5.java | 28 ----- .../io/multiform_validator/MacAddress.java | 29 ----- .../java/io/multiform_validator/Main.java | 35 +----- .../java/io/multiform_validator/Number.java | 62 ----------- .../multiform_validator/PasswordStrength.java | 79 -------------- .../java/io/multiform_validator/Port.java | 30 ------ .../io/multiform_validator/PostalCode.java | 28 ----- .../java/io/multiform_validator/Test.java | 26 ----- .../java/io/multiform_validator/Time.java | 33 ------ .../ValidateFunctions.java | 41 ------- .../ValidatePassportNumber.java | 19 ---- .../io/multiform_validator/Validator.java | 57 ++++++++++ .../java/src/test/java/CnpjValidatorTest.java | 40 +++++++ .../java/src/test/java/CpfValidatorTest.java | 40 +++++++ .../src/test/java/EmailValidatorTest.java | 80 ++++++++++++++ .../java/src/test/java/SimpleMathTest.java | 9 -- .../java/src/test/java/SimpleMathTwoTest.java | 9 -- .../java/src/test/java/ValidatorTest.java | 45 ++++++++ 31 files changed, 446 insertions(+), 868 deletions(-) delete mode 100644 packages/java/src/main/java/io/multiform_validator/Ascii.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Base64.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/CEP.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Cnpj.java create mode 100644 packages/java/src/main/java/io/multiform_validator/CnpjValidator.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Cpf.java create mode 100644 packages/java/src/main/java/io/multiform_validator/CpfValidator.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/CreditCard.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Date.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Email.java create mode 100644 packages/java/src/main/java/io/multiform_validator/EmailValidator.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/MD5.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/MacAddress.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Number.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/PasswordStrength.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Port.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/PostalCode.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Test.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/Time.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/ValidateFunctions.java delete mode 100644 packages/java/src/main/java/io/multiform_validator/ValidatePassportNumber.java create mode 100644 packages/java/src/main/java/io/multiform_validator/Validator.java create mode 100644 packages/java/src/test/java/CnpjValidatorTest.java create mode 100644 packages/java/src/test/java/CpfValidatorTest.java create mode 100644 packages/java/src/test/java/EmailValidatorTest.java delete mode 100644 packages/java/src/test/java/SimpleMathTest.java delete mode 100644 packages/java/src/test/java/SimpleMathTwoTest.java create mode 100644 packages/java/src/test/java/ValidatorTest.java diff --git a/.github/workflows/java-pr-check.yml b/.github/workflows/java-pr-check.yml index e93d129..d563a98 100644 --- a/.github/workflows/java-pr-check.yml +++ b/.github/workflows/java-pr-check.yml @@ -18,12 +18,12 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - - name: Set up JDK 11 - uses: actions/setup-java@v3 + - name: Set up JDK 8 + uses: actions/setup-java@v4 with: - java-version: '11' + java-version: '8' distribution: 'adopt' - name: Build with Maven diff --git a/packages/java/pom.xml b/packages/java/pom.xml index e56a891..7ecef6d 100644 --- a/packages/java/pom.xml +++ b/packages/java/pom.xml @@ -9,8 +9,8 @@ 1.0-SNAPSHOT - 11 - 11 + 1.8 + 1.8 UTF-8 diff --git a/packages/java/src/main/java/io/multiform_validator/Ascii.java b/packages/java/src/main/java/io/multiform_validator/Ascii.java deleted file mode 100644 index 1a32b83..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Ascii.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -public class Ascii { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Ascii() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Check if a string is ASCII. - * - * @param value The string to check. - * @return True if the string is ASCII, false otherwise. - * @throws IllegalArgumentException If the input value is empty. - */ - public static boolean isAscii(@NotNull String value) { - if (value.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - return value.chars().allMatch(c -> c < 128); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Base64.java b/packages/java/src/main/java/io/multiform_validator/Base64.java deleted file mode 100644 index 6e9bee4..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Base64.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -public class Base64 { - - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Base64() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Check if a string is Base64. - * - * @param value The string to check. - * @return True if the string is Base64, false otherwise. - * @throws IllegalArgumentException If the input value is empty. - */ - public static boolean isBase64(@NotNull String value) { - if (value.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - return value.matches("^[a-zA-Z0-9+/]*={0,2}$"); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/CEP.java b/packages/java/src/main/java/io/multiform_validator/CEP.java deleted file mode 100644 index 139e45f..0000000 --- a/packages/java/src/main/java/io/multiform_validator/CEP.java +++ /dev/null @@ -1,35 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -public class CEP { - private CEP() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Verifica se um CEP é válido. - * - * @param cep CEP a ser verificado. - * @return true se o CEP é válido, false caso contrário. - */ - public static boolean isCEP(@NotNull String cep) { - if (cep.length() < 8 || cep.length() > 10) { - return false; - } - - final String cepString = cep.replaceAll("\\D", ""); - - if (cepString.length() != 8) { - return false; - } - - try { - Integer.parseInt(cepString); - } catch (NumberFormatException e) { - return false; - } - - return true; - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Cnpj.java b/packages/java/src/main/java/io/multiform_validator/Cnpj.java deleted file mode 100644 index 4723aee..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Cnpj.java +++ /dev/null @@ -1,70 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; - -public class Cnpj { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Cnpj() { - throw new IllegalStateException("Utility class"); - } - - /** - * Validate CNPJ - * - * @param cnpj CNPJ to be validated - * @return true if CNPJ is valid, false otherwise - */ - public static boolean isValid(@NotNull String cnpj) { - if (cnpj.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - if (cnpj.length() != 14 && cnpj.length() != 18) { - return false; - } - - try { - final String cnpjClean = cnpj.replaceAll("\\D", ""); - - final int[] cnpjArray = cnpjClean.chars().map(Character::getNumericValue).toArray(); - - int firstVerifier = calculateFirstVerifier(Arrays.copyOfRange(cnpjArray, 0, 12)); - int secondVerifier = calculateSecondVerifier(Arrays.copyOfRange(cnpjArray, 0, 13), firstVerifier); - - return cnpjArray[12] == firstVerifier && cnpjArray[13] == secondVerifier; - } catch (Exception e) { - return false; - } - - } - - private static int calculateFirstVerifier(int[] cnpjBase) { - int sum = 0; - int[] weights = {5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; - - for (int i = 0; i < 12; i++) { - sum += cnpjBase[i] * weights[i]; - } - - int remainder = sum % 11; - - return remainder < 2 ? 0 : 11 - remainder; - } - - private static int calculateSecondVerifier(int[] cnpjBase, int firstVerifier) { - int sum = 0; - int[] weights = {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; - - for (int i = 0; i < 13; i++) { - sum += cnpjBase[i] * weights[i]; - } - - sum += firstVerifier * weights[12]; - int remainder = sum % 11; - - return remainder < 2 ? 0 : 11 - remainder; - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/CnpjValidator.java b/packages/java/src/main/java/io/multiform_validator/CnpjValidator.java new file mode 100644 index 0000000..abbe51d --- /dev/null +++ b/packages/java/src/main/java/io/multiform_validator/CnpjValidator.java @@ -0,0 +1,63 @@ +package io.multiform_validator; + +import java.util.Arrays; +public class CnpjValidator { + private CnpjValidator() { + throw new IllegalStateException("Utility class"); + } + private static int calculateFirstVerifier(int[] cnpjBase) { + final int[] weight = {5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; + int sum = 0; + + for (int i = 0; i < 12; i++) { + sum += cnpjBase[i] * weight[i]; + } + final int remainder = sum % 11; + + return remainder < 2 ? 0 : 11 - remainder; + } + + private static int calculateSecondVerifier(int[] cnpjBase, int firstVerifier) { + final int[] weight = {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; + int sum = 0; + + for (int i = 0; i < 12; i++) { + sum += cnpjBase[i] * weight[i]; + } + sum += firstVerifier * weight[12]; + + final int remainder = sum % 11; + + return remainder < 2 ? 0 : 11 - remainder; + } + + public static boolean cnpjIsValid(String cnpj) { + if (cnpj == null) { + throw new NullPointerException("CNPJ cannot be null or empty"); + } + + final String cnpjClean = cnpj.replaceAll("\\D", ""); + + if (cnpjClean.isEmpty()) { + return false; + } + + if (cnpjClean.length() != 14) { + return false; + } + + // Convert the string to an array of integers + final int[] cnpjArray = cnpjClean.chars().map(Character::getNumericValue).toArray(); + + // Calculate the first verifier and second verifier + final int[] cnpjBase = Arrays.copyOfRange(cnpjArray, 0, 12); + final int firstVerifier = calculateFirstVerifier(cnpjBase); + + final int[] cnpjBaseWithFirstVerifier = Arrays.copyOf(cnpjBase, cnpjBase.length + 1); + cnpjBaseWithFirstVerifier[cnpjBaseWithFirstVerifier.length - 1] = firstVerifier; + + final int secondVerifier = calculateSecondVerifier(cnpjBaseWithFirstVerifier, firstVerifier); + + return cnpjArray[12] == firstVerifier && cnpjArray[13] == secondVerifier; + } +} diff --git a/packages/java/src/main/java/io/multiform_validator/Cpf.java b/packages/java/src/main/java/io/multiform_validator/Cpf.java deleted file mode 100644 index 71993b3..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Cpf.java +++ /dev/null @@ -1,76 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -public class Cpf { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Cpf() { - throw new IllegalStateException("Utility class"); - } - - /** - * Validate a CPF number. - * - * @param cpf The CPF number to be validated. - * @return True if the CPF number is valid, false otherwise. - */ - public static boolean isValid(@NotNull String cpf) { - if (cpf.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - try { - int baseNumber = 10; - int baseNumber2 = 11; - - int somaTotal = 0; - int somaTotal2 = 0; - - if (cpf.length() != 11 && cpf.length() != 14) { - return false; - } - - final String cpfClean = cpf.replaceAll("\\D", ""); - - if (cpfClean.chars().allMatch(c -> c == cpfClean.charAt(0))) { - return false; - } - - int firstVerifier = 0; - int secondVerifier = 0; - - int ninthCharAsInt = Integer.parseInt(String.valueOf(cpfClean.charAt(9))); - int tenthCharAsInt = Integer.parseInt(String.valueOf(cpfClean.charAt(10))); - - for (int repeater = 0; repeater < 11; repeater++) { - int currentCharAsInt = Integer.parseInt(String.valueOf(cpfClean.charAt(repeater))); - - final int multiplicative = currentCharAsInt * baseNumber; - - baseNumber--; - somaTotal += multiplicative; - - final int multiplicative2 = currentCharAsInt * baseNumber2; - - baseNumber2--; - somaTotal2 += multiplicative2; - - final int verificationValue = somaTotal - ninthCharAsInt; - final int verificationValue2 = somaTotal2 - tenthCharAsInt; - firstVerifier = 11 - (verificationValue % 11); - secondVerifier = 11 - (verificationValue2 % 11); - } - if (firstVerifier > 9) { - firstVerifier = 0; - } - if (secondVerifier > 9) { - secondVerifier = 0; - } - - return firstVerifier == ninthCharAsInt && secondVerifier == tenthCharAsInt; - } catch (Exception e) { - return false; - } - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/CpfValidator.java b/packages/java/src/main/java/io/multiform_validator/CpfValidator.java new file mode 100644 index 0000000..7ad587c --- /dev/null +++ b/packages/java/src/main/java/io/multiform_validator/CpfValidator.java @@ -0,0 +1,39 @@ +package io.multiform_validator; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.IntStream; + +public class CpfValidator { + private CpfValidator() { + throw new IllegalStateException("Utility class"); + } + public static boolean cpfIsValid(String cpf) { + if (cpf == null) { + throw new NullPointerException("CPF cannot be null or empty"); + } + + final String cpfClean = cpf.replaceAll("\\D", ""); + + if (cpfClean.length() != 11) { + return false; + } + + Pattern pattern = Pattern.compile("(\\d)\\1{10}"); + Matcher matcher = pattern.matcher(cpfClean); + + if (matcher.find()) { + return false; + } + + final int[] cpfArray = cpfClean.chars().map(Character::getNumericValue).toArray(); + + final int sum1 = IntStream.range(0, 9).map(i -> cpfArray[i] * (10 - i)).sum(); + final int sum2 = IntStream.range(0, 10).map(i -> cpfArray[i] * (11 - i)).sum(); + + final int validator1 = sum1 % 11 < 2 ? 0 : 11 - (sum1 % 11); + final int validator2 = sum2 % 11 < 2 ? 0 : 11 - (sum2 % 11); + + return cpfArray[9] == validator1 && cpfArray[10] == validator2; + } +} diff --git a/packages/java/src/main/java/io/multiform_validator/CreditCard.java b/packages/java/src/main/java/io/multiform_validator/CreditCard.java deleted file mode 100644 index e720cba..0000000 --- a/packages/java/src/main/java/io/multiform_validator/CreditCard.java +++ /dev/null @@ -1,101 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -public class CreditCard { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private CreditCard() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Validate a credit card number. - * - * @param cardNumber The credit card number to be validated. - * @return True if the credit card number is valid, false otherwise. - */ - public static boolean isCreditCardValid(@NotNull String cardNumber) { - if (cardNumber.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - final String cleanedCreditCardInput = cardNumber.replaceAll("\\D", ""); - - if (cleanedCreditCardInput.isBlank() || !Number.isNumber(cleanedCreditCardInput)) { - return false; - } - - final int[] creditCardDigits = cleanedCreditCardInput.chars().map(Character::getNumericValue).toArray(); - final int creditCardLength = creditCardDigits.length; - - int sum = 0; - boolean isEven = false; - - for (int i = creditCardLength - 1; i >= 0; i--) { - int digit = creditCardDigits[i]; - - if (isEven) { - digit *= 2; - - if (digit > 9) { - digit -= 9; - } - } - - sum += digit; - isEven = !isEven; - } - - return sum % 10 == 0; - } - - /** - * Identify the flag of a credit card. - * - * @param cardNumber The credit card number to be identified. - * @return The flag of the credit card. - */ - public static String identifyFlagCard(@NotNull String cardNumber) { - if (cardNumber.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - List flags = new ArrayList<>(); - flags.add(new CardFlag("Visa", "^4")); - flags.add(new CardFlag("Mastercard", "^5[1-5]")); - flags.add(new CardFlag("American Express", "^3[47]")); - flags.add(new CardFlag("Discover", "^6(?:011|5)")); - flags.add(new CardFlag("JCB", "^(?:2131|1800|35\\d{3})")); - flags.add(new CardFlag("Diners Club", "^3(?:0[0-5]|[68])")); - flags.add(new CardFlag("Maestro", "^(?:5[0678]\\d\\d|6304|6390|67\\d\\d)")); - flags.add(new CardFlag("UnionPay", "^(62|88)")); - flags.add(new CardFlag("Elo", "^63[789]")); - flags.add(new CardFlag("Hipercard", "^(3841|60)")); - - for (CardFlag flag : flags) { - if (flag.interval.matcher(cardNumber).find()) { - return flag.name; - } - } - - return "Unknown"; - } -} - -/** - * Class to represent a card flag. - */ -class CardFlag { - String name; - Pattern interval; - - CardFlag(String name, String interval) { - this.name = name; - this.interval = Pattern.compile(interval); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Date.java b/packages/java/src/main/java/io/multiform_validator/Date.java deleted file mode 100644 index 2d196f9..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Date.java +++ /dev/null @@ -1,33 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.time.format.DateTimeParseException; -import java.util.regex.Pattern; - -public class Date { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty"; - - private Date() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Check if the given string is a valid date - * - * @param date The date string to be validated - * @return true if the given string is a valid date, false otherwise - */ - public static boolean isDate(@NotNull String date) { - if (date.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - try { - final Pattern dateStringRegex = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$"); - return dateStringRegex.matcher(date).matches(); - } catch (DateTimeParseException e) { - return false; - } - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Email.java b/packages/java/src/main/java/io/multiform_validator/Email.java deleted file mode 100644 index fc34358..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Email.java +++ /dev/null @@ -1,69 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.util.regex.Pattern; - -public class Email { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Email() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Check if the given string is a valid email - * - * @param email The email string to be validated - * @return true if the given string is a valid email, false otherwise - */ - public static boolean isEmail(@NotNull String email) { - if (email.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - final Pattern emailRegex = Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"); - return emailRegex.matcher(email).matches(); - } - - /** - * Validate the given email string - * - * @param email The email string to be validated - * @param maxLength The maximum length of the email string - * @param country The country code of the email domain - * @param validDomains Whether to validate the email domain - * @return A ValidateFunctions object containing the validation result - */ - public static @NotNull ValidateFunctions validateEmail(@NotNull String email, Integer maxLength, String country, boolean validDomains) { - if (email.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - if (!isLengthValid(email, maxLength)) { - return new InvalidFunction("Email length is greater than " + maxLength + " characters."); - } - - if (!isCountryValid(country)) { - return new InvalidFunction("Country cannot be empty."); - } - - if (validDomains && !isDomainValid(email, country)) { - return new InvalidFunction("Email domain must be ." + country.toLowerCase()); - } - - return isEmail(email) ? new ValidFunction() : new InvalidFunction("Invalid email format."); - } - - private static boolean isLengthValid(String email, Integer maxLength) { - return maxLength == null || email.length() <= maxLength; - } - - private static boolean isCountryValid(String country) { - return !country.isBlank(); - } - - private static boolean isDomainValid(String email, String country) { - return email.endsWith("." + country.toLowerCase()); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/EmailValidator.java b/packages/java/src/main/java/io/multiform_validator/EmailValidator.java new file mode 100644 index 0000000..606ba8b --- /dev/null +++ b/packages/java/src/main/java/io/multiform_validator/EmailValidator.java @@ -0,0 +1,73 @@ +package io.multiform_validator; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; + +public class EmailValidator { + private EmailValidator() { + throw new IllegalStateException("Utility class"); + } + + public static boolean isEmail(String email) { + if (email == null) { + throw new NullPointerException("Email cannot be null"); + } + + final Pattern startsWithSpecialChar = Pattern.compile("^[^a-zA-Z0-9]"); + + if (startsWithSpecialChar.matcher(email).find()) { + return false; + } + + final Pattern regex = Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"); + + if (!regex.matcher(email).find()) { + return false; + } + + final int beforeAt = email.indexOf("@"); + final int afterAt = email.indexOf("@") + 1; + final int afterLastDot = email.lastIndexOf("."); + + if (Character.isDigit(email.charAt(afterAt))) { + return false; + } + + if (Character.isDigit(email.charAt(afterLastDot))) { + return false; + } + + if (email.substring(0, beforeAt).contains("..")) { + return false; + } + + if (email.substring(0, beforeAt).endsWith(".")) { + return false; + } + + final String[] parts = email.split("\\."); + + if (parts.length > 2 && parts[parts.length - 2].equals(parts[parts.length - 3])) { + return false; + } + + // Check if there is more than one @ + if (email.split("@").length - 1 > 1) { + return false; + } + + if (email.substring(afterAt).contains("..")) { + return false; + } + + String[] domainParts = email.split("@")[1].split("\\."); + Set uniqueDomainParts = new HashSet<>(Arrays.asList(domainParts)); + + return domainParts.length == uniqueDomainParts.size(); + } +} + + + diff --git a/packages/java/src/main/java/io/multiform_validator/MD5.java b/packages/java/src/main/java/io/multiform_validator/MD5.java deleted file mode 100644 index a6b4100..0000000 --- a/packages/java/src/main/java/io/multiform_validator/MD5.java +++ /dev/null @@ -1,28 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.util.regex.Pattern; - -public class MD5 { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private MD5() { - throw new IllegalStateException("Utility class"); - } - - /** - * Check if the given string is a valid MD5 hash. - * - * @param md5 The string to check. - * @return True if the string is a valid MD5 hash, false otherwise. - */ - public static boolean isMD5(@NotNull String md5) { - if (md5.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - final Pattern md5Regex = Pattern.compile("^[a-fA-F0-9]{32}$"); - return md5Regex.matcher(md5).matches(); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/MacAddress.java b/packages/java/src/main/java/io/multiform_validator/MacAddress.java deleted file mode 100644 index 0c898b4..0000000 --- a/packages/java/src/main/java/io/multiform_validator/MacAddress.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.util.regex.Pattern; - -public class MacAddress { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty"; - - private MacAddress() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Check if the given string is a valid MAC address. - * - * @param macAddress the MAC address to validate - * @return {@code true} if the given string is a valid MAC address, {@code false} otherwise - * @throws IllegalArgumentException if the given MAC address is empty - */ - public static boolean isMacAddress(@NotNull String macAddress) { - if (macAddress.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - final Pattern macAddressRegex = Pattern.compile("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"); - return macAddressRegex.matcher(macAddress).matches(); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Main.java b/packages/java/src/main/java/io/multiform_validator/Main.java index 777bcec..b1f37fe 100644 --- a/packages/java/src/main/java/io/multiform_validator/Main.java +++ b/packages/java/src/main/java/io/multiform_validator/Main.java @@ -3,38 +3,9 @@ import static java.lang.System.*; public class Main { - /** - * Test class - */ - private static final ValidateFunctions testResult = Test.test("Hello, World!"); - private static final ValidateFunctions testValidateEmail = Email.validateEmail("hoo@gmail.co", 15, "us", false); - - /** - * Main method - */ public static void main(String[] args) { - out.println("Hello, World!"); - out.println("isAscii: " + Ascii.isAscii("Hello, World!a")); - out.println("isBase64: " + Base64.isBase64("Hello, World!d")); - out.println("isCEP: " + CEP.isCEP("12345-678")); - out.println("isNumber: " + Number.isNumber("12345")); - out.println("isNaN: " + Number.isNaN("12345a")); - out.println("isCreditCardValid: " + CreditCard.isCreditCardValid("1234567890123456")); - out.println("isDate: " + Date.isDate("2021-10-10")); - out.println("isEmail: " + Email.isEmail("jaj@gmmoo.com")); - out.println("isMacAddress: " + MacAddress.isMacAddress("00:00:00:00:00:00")); - out.println("isMD5: " + MD5.isMD5("12345678901234567890123456789012")); - out.println("isPort: " + Port.isPort("1234")); - out.println("isPostalCode: " + PostalCode.isPostalCode("12345-678")); - out.println("isTime: " + Time.isTime("12:34:56")); - out.println("passwordStrengthTester: " + PasswordStrength.tester("1234567890")); - out.println("cpfIsValid: " + Cpf.isValid("123.456.789-09")); - out.println("cnpjIsValid: " + Cnpj.isValid("12.345.678/0001-09")); - out.println("Test.main boolean: " + testResult.isValid); - out.println("Test.main message: " + testResult.getErrorMsg()); - out.println("Email.main boolean: " + testValidateEmail.isValid); - out.println("Email.main message: " + testValidateEmail.getErrorMsg()); - out.println("IdentifyFlagCard: " + CreditCard.identifyFlagCard("5237212984966627")); - out.println("IdentifyFlagCard: " + CreditCard.identifyFlagCard("4024 0071 3438 4733")); + out.println("This class cannot be instantiated."); + out.println("Please use the classes in the io.multiform_validator package."); + out.println("This is a test VERSION, DON'T USE IN PRODUCTION!"); } } \ No newline at end of file diff --git a/packages/java/src/main/java/io/multiform_validator/Number.java b/packages/java/src/main/java/io/multiform_validator/Number.java deleted file mode 100644 index 1feb7e9..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Number.java +++ /dev/null @@ -1,62 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -public class Number { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Number() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } - - /** - * Check if the given value is a number. - * - * @param value The value to check. - * @return True if the value is a number, false otherwise. - */ - public static boolean isNumber(@NotNull String value) { - if (value.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - try { - Double.parseDouble(value); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - /** - * Check if the given value is not a number. - * - * @param value The value to check. - * @return True if the value is not a number, false otherwise. - */ - public static boolean isNaN(@NotNull String value) { - if (value.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - return !isNumber(value); - } - - /** - * Check if the given value is an integer. - * - * @param value The value to check. - * @return True if the value is an integer, false otherwise. - */ - public static boolean isInteger(@NotNull String value) { - if (value.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - try { - Integer.parseInt(value); - return true; - } catch (NumberFormatException e) { - return false; - } - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/PasswordStrength.java b/packages/java/src/main/java/io/multiform_validator/PasswordStrength.java deleted file mode 100644 index dac01c2..0000000 --- a/packages/java/src/main/java/io/multiform_validator/PasswordStrength.java +++ /dev/null @@ -1,79 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -import java.util.regex.Pattern; - -public class PasswordStrength { - private PasswordStrength() { - throw new IllegalStateException("Utility class"); - } - - /** - * Check the strength of the given password. - * - * @param password The password to check. - * @return The strength of the password. - */ - @Contract("null -> fail") - public static @NotNull String tester(String password) { - if (password == null) throw new IllegalArgumentException("The input should be a string."); - - if (isVeryWeak(password)) { - return "veryWeak"; - } else if (isWeak(password)) { - return "weak"; - } else if (isRegular(password)) { - return "regular"; - } else if (isVeryStrong(password)) { - return "veryStrong"; - } else if (isStrong(password)) { - return "strong"; - } - - return "unknown"; - } - - private static boolean isVeryWeak(@NotNull String password) { - int passwordLength = password.length(); - return passwordLength <= 5 && Pattern.matches("^\\d+$", password); - } - - private static boolean isWeak(@NotNull String password) { - int passwordLength = password.length(); - return (passwordLength <= 5 && Pattern.matches("^[a-zA-Z0-9]+$", password)) || - (passwordLength >= 6 && Pattern.matches("^[a-zA-Z0-9]+$", password) && passwordLength <= 7) || - (passwordLength < 10 && Pattern.matches("(.)\\1{3,}", password)) || - (passwordLength >= 5 && passwordLength <= 8 && Pattern.matches("^\\d+$", password)); - } - - private static boolean isRegular(@NotNull String password) { - int passwordLength = password.length(); - return Pattern.matches("(.)\\1{5,}", password) && passwordLength > 10 || - (passwordLength >= 9 && passwordLength <= 12) || - (password.length() >= 6 && - password.length() <= 8 && - Pattern.matches("\\d", password) && - Pattern.matches("[a-zA-Z]", password)); - } - - private static boolean isVeryStrong(@NotNull String password) { - int passwordLength = password.length(); - return passwordLength > 16 || - (password.length() >= 8 && - Pattern.matches("[A-Z]", password) && - Pattern.matches("[a-z]", password) && - Pattern.matches("\\d", password) && - Pattern.matches("[\\W_]", password)); - } - - private static boolean isStrong(@NotNull String password) { - int passwordLength = password.length(); - return (passwordLength >= 13 && passwordLength <= 16) || - (password.length() >= 8 && - Pattern.matches("[A-Z]", password) && - Pattern.matches("[a-z]", password) && - Pattern.matches("\\d", password)); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Port.java b/packages/java/src/main/java/io/multiform_validator/Port.java deleted file mode 100644 index fd02f37..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Port.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -public class Port { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Port() { - throw new IllegalStateException("Utility class"); - } - - /** - * Check if the given port is valid. - * - * @param port The port to validate. - * @return True if the port is valid, false otherwise. - */ - public static boolean isPort(@NotNull String port) { - if (port.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - try { - final int portNumber = Integer.parseInt(port); - return portNumber >= 0 && portNumber <= 65535; - } catch (NumberFormatException e) { - return false; - } - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/PostalCode.java b/packages/java/src/main/java/io/multiform_validator/PostalCode.java deleted file mode 100644 index c76083e..0000000 --- a/packages/java/src/main/java/io/multiform_validator/PostalCode.java +++ /dev/null @@ -1,28 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.util.regex.Pattern; - -public class PostalCode { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private PostalCode() { - throw new IllegalStateException("Utility class"); - } - - /** - * Check if the given postal code is valid. - * - * @param postalCode The postal code to validate. - * @return True if the postal code is valid, false otherwise. - */ - public static boolean isPostalCode(@NotNull String postalCode) { - if (postalCode.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - final Pattern postalCodeRegex = Pattern.compile("^\\d{5}-\\d{3}$"); - return postalCodeRegex.matcher(postalCode).matches(); - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Test.java b/packages/java/src/main/java/io/multiform_validator/Test.java deleted file mode 100644 index 40dec1f..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -public class Test { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Test() { - throw new IllegalStateException("Utility class"); - } - - @Contract("null -> fail") - public static @NotNull ValidateFunctions test(String value) { - if (value == null || value.isEmpty()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - if (value.equals("valid")) { - return new ValidFunction(); - } else { - return new InvalidFunction("Invalid value."); - } - - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/Time.java b/packages/java/src/main/java/io/multiform_validator/Time.java deleted file mode 100644 index 38c0818..0000000 --- a/packages/java/src/main/java/io/multiform_validator/Time.java +++ /dev/null @@ -1,33 +0,0 @@ -package io.multiform_validator; - -import org.jetbrains.annotations.NotNull; - -import java.time.format.DateTimeParseException; -import java.util.regex.Pattern; - -public class Time { - private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; - - private Time() { - throw new IllegalStateException("Utility class"); - } - - /** - * Check if the given time is valid. - * - * @param time The time to validate. - * @return true if the time is valid, false otherwise. - */ - public static boolean isTime(@NotNull String time) { - if (time.isBlank()) { - throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); - } - - try { - final Pattern timeRegex = Pattern.compile("^\\d{2}:\\d{2}:\\d{2}$"); - return timeRegex.matcher(time).matches(); - } catch (DateTimeParseException e) { - return false; - } - } -} diff --git a/packages/java/src/main/java/io/multiform_validator/ValidateFunctions.java b/packages/java/src/main/java/io/multiform_validator/ValidateFunctions.java deleted file mode 100644 index 130f68a..0000000 --- a/packages/java/src/main/java/io/multiform_validator/ValidateFunctions.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.multiform_validator; - -/** - * Abstract class for validation functions. - */ -public abstract class ValidateFunctions { - protected boolean isValid; - - public abstract String getErrorMsg(); -} - -/** - * Class for valid functions. - */ -class ValidFunction extends ValidateFunctions { - public ValidFunction() { - this.isValid = true; - } - - @Override - public String getErrorMsg() { - return null; - } -} - -/** - * Class for invalid functions. - */ -class InvalidFunction extends ValidateFunctions { - public final String errorMsg; - - public InvalidFunction(String errorMsg) { - this.isValid = false; - this.errorMsg = errorMsg; - } - - @Override - public String getErrorMsg() { - return errorMsg; - } -} \ No newline at end of file diff --git a/packages/java/src/main/java/io/multiform_validator/ValidatePassportNumber.java b/packages/java/src/main/java/io/multiform_validator/ValidatePassportNumber.java deleted file mode 100644 index f660d60..0000000 --- a/packages/java/src/main/java/io/multiform_validator/ValidatePassportNumber.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.multiform_validator; - -/** - * ValidatePassportNumber - */ -public class ValidatePassportNumber { - public final boolean isValid; - public final String country; - - /** - * Constructor - */ - public ValidatePassportNumber(boolean isValid, String country) { - this.isValid = isValid; - this.country = country; - } - - // getters and setters -} \ No newline at end of file diff --git a/packages/java/src/main/java/io/multiform_validator/Validator.java b/packages/java/src/main/java/io/multiform_validator/Validator.java new file mode 100644 index 0000000..af26160 --- /dev/null +++ b/packages/java/src/main/java/io/multiform_validator/Validator.java @@ -0,0 +1,57 @@ +package io.multiform_validator; + +public class Validator { + private static final String INPUT_VALUE_CANNOT_BE_EMPTY = "Input value cannot be empty."; + private Validator() { + throw new IllegalStateException("Utility class"); + } + + public static boolean isAscii(String value) { + if (value == null || value.isEmpty()) { + throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); + } + + return value.chars().allMatch(c -> c < 128); + } + + public static boolean isBase64(String value) { + if (value == null || value.isEmpty()) { + throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); + } + + return value.matches("^[a-zA-Z0-9+/]*={0,2}$"); + } + + public static boolean isCEP(String cep) { + if (cep.length() < 8 || cep.length() > 10) { + return false; + } + + final String cepString = cep.replaceAll("\\D", ""); + + if (cepString.length() != 8) { + return false; + } + + try { + Integer.parseInt(cepString); + } catch (NumberFormatException e) { + return false; + } + + return true; + } + + public static boolean isPort(String port) { + if (port == null || port.isEmpty()) { + throw new IllegalArgumentException(INPUT_VALUE_CANNOT_BE_EMPTY); + } + + try { + final int portNumber = Integer.parseInt(port); + return portNumber >= 0 && portNumber <= 65535; + } catch (NumberFormatException e) { + return false; + } + } +} diff --git a/packages/java/src/test/java/CnpjValidatorTest.java b/packages/java/src/test/java/CnpjValidatorTest.java new file mode 100644 index 0000000..79cc996 --- /dev/null +++ b/packages/java/src/test/java/CnpjValidatorTest.java @@ -0,0 +1,40 @@ +import org.junit.jupiter.api.Test; + +import static io.multiform_validator.CnpjValidator.cnpjIsValid; +import static org.junit.jupiter.api.Assertions.*; + +class CnpjValidatorTest { + @Test + void testValidCnpj() { + assertTrue(cnpjIsValid("72.231.875/0001-05")); + assertTrue(cnpjIsValid("41997509000138")); + } + + @Test + void testInvalidCnpj() { + assertFalse(cnpjIsValid("12.345.678/0001-91")); + assertFalse(cnpjIsValid("12345678000191")); + } + + @Test + void testNullCnpj() { + assertThrows(NullPointerException.class, () -> cnpjIsValid(null)); + } + + @Test + void testEmptyCnpj() { + assertFalse(cnpjIsValid("")); + } + + @Test + void testInvalidFormatCnpj() { + assertFalse(cnpjIsValid("12.345.678/0001-9")); + assertFalse(cnpjIsValid("1234567800019")); + } + + @Test + void testInvalidLengthCnpj() { + assertFalse(cnpjIsValid("12.345.678/0001-900")); + assertFalse(cnpjIsValid("123456780001900")); + } +} \ No newline at end of file diff --git a/packages/java/src/test/java/CpfValidatorTest.java b/packages/java/src/test/java/CpfValidatorTest.java new file mode 100644 index 0000000..64ccd91 --- /dev/null +++ b/packages/java/src/test/java/CpfValidatorTest.java @@ -0,0 +1,40 @@ +import org.junit.jupiter.api.Test; + +import static io.multiform_validator.CpfValidator.cpfIsValid; +import static org.junit.jupiter.api.Assertions.*; + +class CpfValidatorTest { + @Test + void testValidCpf() { + assertTrue(cpfIsValid("12345678909")); + assertTrue(cpfIsValid("11144477735")); + } + + @Test + void testInvalidCpf() { + assertFalse(cpfIsValid("12345678901")); + assertFalse(cpfIsValid("11144477736")); + } + + @Test + void testNullCpf() { + assertThrows(NullPointerException.class, () -> cpfIsValid(null)); + } + + @Test + void testEmptyCpf() { + assertFalse(cpfIsValid("")); + } + + @Test + void testInvalidFormatCpf() { + assertFalse(cpfIsValid("123.456.789-19")); + assertFalse(cpfIsValid("1114447773A")); + } + + @Test + void testInvalidLengthCpf() { + assertFalse(cpfIsValid("1234567890")); + assertFalse(cpfIsValid("111444777350")); + } +} \ No newline at end of file diff --git a/packages/java/src/test/java/EmailValidatorTest.java b/packages/java/src/test/java/EmailValidatorTest.java new file mode 100644 index 0000000..e4c2b27 --- /dev/null +++ b/packages/java/src/test/java/EmailValidatorTest.java @@ -0,0 +1,80 @@ +import org.junit.jupiter.api.Test; + +import static io.multiform_validator.EmailValidator.isEmail; +import static org.junit.jupiter.api.Assertions.*; + +class EmailValidatorTest { + @Test + void testValidEmail() { + assertTrue(isEmail("test@example.com")); + assertTrue(isEmail("john.doe@example.com")); + } + + @Test + void testInvalidEmail() { + assertFalse(isEmail("test@example")); + assertFalse(isEmail("john.doe@example")); + assertFalse(isEmail("test@example.")); + assertFalse(isEmail("john.doe@example.")); + assertFalse(isEmail("@example.com")); + assertFalse(isEmail("@example.")); + assertFalse(isEmail("test@.com")); + assertFalse(isEmail("john.doe@.com")); + assertFalse(isEmail("test@.")); + assertFalse(isEmail("john.doe@.")); + assertFalse(isEmail("test@@example.com")); + assertFalse(isEmail("john.doe@@example.com")); + assertFalse(isEmail("test@example..com")); + assertFalse(isEmail("john.doe@example..com")); + assertFalse(isEmail("test@example.com.")); + assertFalse(isEmail("john.doe@example.com.")); + assertFalse(isEmail("test@example..")); + assertFalse(isEmail("john.doe@example..")); + assertFalse(isEmail("test@example.com..")); + assertFalse(isEmail("john.doe@example.com..")); + assertFalse(isEmail("test@example..com.")); + assertFalse(isEmail("john.doe@example..com.")); + assertFalse(isEmail("test@example.com..")); + assertFalse(isEmail("john.doe@example.com..")); + assertFalse(isEmail("test@example..com..")); + assertFalse(isEmail("john.doe@example..com..")); + assertFalse(isEmail("test@example.com...")); + assertFalse(isEmail("john.doe@example.com...")); + assertFalse(isEmail("test@example...com")); + assertFalse(isEmail("john.doe@example...com")); + assertFalse(isEmail("test@example.com...")); + assertFalse(isEmail("john.doe@example.com...")); + assertFalse(isEmail("test@example...com.")); + assertFalse(isEmail("john.doe@example...com.")); + assertFalse(isEmail("test@example.com...")); + assertFalse(isEmail("john.doe@example.com...")); + assertFalse(isEmail("test@example...com..")); + assertFalse(isEmail("john.doe@example...com..")); + assertFalse(isEmail("test@example.com....")); + assertFalse(isEmail("john.doe@example.com....")); + assertFalse(isEmail("test@example....com")); + assertFalse(isEmail("john.doe@example....com")); + assertFalse(isEmail("test@example.com....")); + assertFalse(isEmail("john.doe@example.com....")); + assertFalse(isEmail("test@example....com.")); + assertFalse(isEmail("john.doe@example....com.")); + assertFalse(isEmail("test@example.com....")); + assertFalse(isEmail("john.doe@example.com....")); + assertFalse(isEmail("test@example....com..")); + assertFalse(isEmail("john.doe@example....com..")); + assertFalse(isEmail("0654")); + assertFalse(isEmail("test")); + assertFalse(isEmail("john.doe")); + assertFalse(isEmail("test@")); + assertFalse(isEmail("john.doe@")); + assertFalse(isEmail("test@.")); + assertFalse(isEmail("john.doe@.")); + assertFalse(isEmail("test@.com")); + assertFalse(isEmail("john.doe@.com")); + } + + @Test + void testNullEmail() { + assertThrows(NullPointerException.class, () -> isEmail(null)); + } +} \ No newline at end of file diff --git a/packages/java/src/test/java/SimpleMathTest.java b/packages/java/src/test/java/SimpleMathTest.java deleted file mode 100644 index 433932f..0000000 --- a/packages/java/src/test/java/SimpleMathTest.java +++ /dev/null @@ -1,9 +0,0 @@ -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -class SimpleMathTest { - @Test - void testAddition() { - assertEquals(2, 1 + 1, "1 + 1 should equal 2"); - } -} \ No newline at end of file diff --git a/packages/java/src/test/java/SimpleMathTwoTest.java b/packages/java/src/test/java/SimpleMathTwoTest.java deleted file mode 100644 index ef448f9..0000000 --- a/packages/java/src/test/java/SimpleMathTwoTest.java +++ /dev/null @@ -1,9 +0,0 @@ -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -class SimpleMathTwoTest { - @Test - void testAddition() { - assertEquals(4, 3 + 1, "3 + 1 should equal 4"); - } -} \ No newline at end of file diff --git a/packages/java/src/test/java/ValidatorTest.java b/packages/java/src/test/java/ValidatorTest.java new file mode 100644 index 0000000..6639bbe --- /dev/null +++ b/packages/java/src/test/java/ValidatorTest.java @@ -0,0 +1,45 @@ +import io.multiform_validator.Validator; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ValidatorTest { + @Test + void testIsAscii() { + assertTrue(Validator.isAscii("Hello World")); + assertTrue(Validator.isAscii("123")); + assertFalse(Validator.isAscii("こんにちは")); + assertFalse(Validator.isAscii("你好")); + assertThrows(IllegalArgumentException.class, () -> Validator.isAscii(null)); + assertThrows(IllegalArgumentException.class, () -> Validator.isAscii("")); + } + + @Test + void testIsBase64() { + assertTrue(Validator.isBase64("SGVsbG8gV29ybGQ=")); + assertTrue(Validator.isBase64("MTIzNDU2Nzg5MA==")); + assertFalse(Validator.isBase64("Hello World")); + assertThrows(IllegalArgumentException.class, () -> Validator.isBase64(null)); + assertThrows(IllegalArgumentException.class, () -> Validator.isBase64("")); + } + + @Test + void testIsCEP() { + assertTrue(Validator.isCEP("12345678")); + assertTrue(Validator.isCEP("12345-678")); + assertFalse(Validator.isCEP("1234567")); + assertFalse(Validator.isCEP("123456789")); + assertFalse(Validator.isCEP("abcdefgh")); + } + + @Test + void testIsPort() { + assertTrue(Validator.isPort("80")); + assertTrue(Validator.isPort("8080")); + assertFalse(Validator.isPort("-1")); + assertFalse(Validator.isPort("65536")); + assertFalse(Validator.isPort("abc")); + assertThrows(IllegalArgumentException.class, () -> Validator.isPort(null)); + assertThrows(IllegalArgumentException.class, () -> Validator.isPort("")); + } +} \ No newline at end of file