From dbdf5e54ab5e62f18ca2a8849b06f2a3f61345bb Mon Sep 17 00:00:00 2001 From: augmentedpotato Date: Tue, 24 Mar 2026 20:42:45 -0600 Subject: [PATCH] added null checks to validator, created a bunch of junit tests --- desktop/pom.xml | 8 + .../org/example/petshopdesktop/Validator.java | 37 ++- .../example/petshopdesktop/ValidatorTest.java | 213 ++++++++++++++++++ 3 files changed, 253 insertions(+), 5 deletions(-) create mode 100644 desktop/src/test/java/org/example/petshopdesktop/ValidatorTest.java diff --git a/desktop/pom.xml b/desktop/pom.xml index 9842bdfa..fe513dc6 100644 --- a/desktop/pom.xml +++ b/desktop/pom.xml @@ -76,6 +76,14 @@ 25 + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + --add-opens org.example.petshopdesktop/org.example.petshopdesktop=ALL-UNNAMED + + org.openjfx javafx-maven-plugin diff --git a/desktop/src/main/java/org/example/petshopdesktop/Validator.java b/desktop/src/main/java/org/example/petshopdesktop/Validator.java index 9c6f78a6..cf51eb1e 100644 --- a/desktop/src/main/java/org/example/petshopdesktop/Validator.java +++ b/desktop/src/main/java/org/example/petshopdesktop/Validator.java @@ -13,6 +13,7 @@ public class Validator { if (value == null || value.isBlank()){ msg += name + " is required. \n"; } + return msg; } @@ -24,8 +25,13 @@ public class Validator { */ public static String isNonNegativeDouble(String value, String name){ String msg =""; + if (value == null) { + msg += name + " must be a number.\n"; + + return msg; + } double result; - try{ + try { result = Double.parseDouble(value); if (result < 0){ msg += name + " must be greater than or equal 0. \n"; @@ -34,6 +40,7 @@ public class Validator { catch (NumberFormatException e){ msg += name + " must be a number.\n"; } + return msg; } @@ -47,8 +54,13 @@ public class Validator { */ public static String isDoubleInRange(String value, String name, double minValue, double maxValue){ String msg =""; + if (value == null) { + msg += name + " must be a number.\n"; + + return msg; + } double result; - try{ + try { result = Double.parseDouble(value); if (result < minValue || result > maxValue){ msg += name + " must be between " + minValue + " and " + maxValue + "\n"; @@ -57,6 +69,7 @@ public class Validator { catch (NumberFormatException e){ msg += name + " must be a number.\n"; } + return msg; } @@ -69,7 +82,7 @@ public class Validator { public static String isNonNegativeInteger(String value, String name){ String msg =""; int result; - try{ + try { result = Integer.parseInt(value); if (result < 0){ msg += name + " must be greater than or equal 0. \n"; @@ -78,6 +91,7 @@ public class Validator { catch (NumberFormatException e){ msg += name + " must be a whole number.\n"; } + return msg; } @@ -90,9 +104,10 @@ public class Validator { */ public static String isLessThanVarChars(String value, String name, int length){ String msg =""; - if (value.length() > length){ + if (value == null || value.length() > length){ msg += name + " must be less than " + length + " characters. \n"; } + return msg; } @@ -104,11 +119,17 @@ public class Validator { */ public static String isValidEmail(String value, String name){ String msg = ""; + if (value == null) { + msg += name + " is not in a valid format. \n"; + + return msg; + } String regex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"; if (!value.matches(regex)){ msg += name + " is not in a valid format. \n"; } + return msg; } @@ -120,11 +141,17 @@ public class Validator { */ public static String isValidPhoneNumber(String value, String name){ String msg = ""; + if (value == null) { + msg += name + " must be in format XXX-XXX-XXXX. \n"; + + return msg; + } String regex = "^\\d{3}-\\d{3}-\\d{4}$"; if (!value.matches(regex)){ msg += name + " must be in format XXX-XXX-XXXX. \n"; } + return msg; } -} +} \ No newline at end of file diff --git a/desktop/src/test/java/org/example/petshopdesktop/ValidatorTest.java b/desktop/src/test/java/org/example/petshopdesktop/ValidatorTest.java new file mode 100644 index 00000000..61a8e0c8 --- /dev/null +++ b/desktop/src/test/java/org/example/petshopdesktop/ValidatorTest.java @@ -0,0 +1,213 @@ +//Validator JUnits tests + +package org.example.petshopdesktop; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ValidatorTest { + + //isPresent + @Test + void isPresent_nullValue_returnsError() { + String result = Validator.isPresent(null, "Name"); + assertFalse(result.isEmpty(), "null value should produce an error message"); + } + + @Test + void isPresent_blankValue_returnsError() { + assertFalse(Validator.isPresent(" ", "Name").isEmpty()); + } + + @Test + void isPresent_emptyString_returnsError() { + assertFalse(Validator.isPresent("", "Name").isEmpty()); + } + + @Test + void isPresent_validValue_returnsEmpty() { + assertTrue(Validator.isPresent("Leon", "Name").isEmpty()); + } + + //isNonNegativeDouble + @Test + void isNonNegativeDouble_positiveValue_returnsEmpty() { + assertTrue(Validator.isNonNegativeDouble("5.5", "Price").isEmpty()); + } + + @Test + void isNonNegativeDouble_zero_returnsEmpty() { + assertTrue(Validator.isNonNegativeDouble("0", "Price").isEmpty()); + } + + @Test + void isNonNegativeDouble_negativeValue_returnsError() { + assertFalse(Validator.isNonNegativeDouble("-1.0", "Price").isEmpty()); + } + + @Test + void isNonNegativeDouble_nonNumeric_returnsError() { + assertFalse(Validator.isNonNegativeDouble("abc", "Price").isEmpty()); + } + + @Test + void isNonNegativeDouble_nullInput_returnsError() { + assertFalse(Validator.isNonNegativeDouble(null, "Price").isEmpty()); + } + + //isDoubleInRange + @Test + void isDoubleInRange_withinRange_returnsEmpty() { + assertTrue(Validator.isDoubleInRange("5.0", "Discount", 0.0, 10.0).isEmpty()); + } + + @Test + void isDoubleInRange_atMinBoundary_returnsEmpty() { + assertTrue(Validator.isDoubleInRange("0.0", "Discount", 0.0, 10.0).isEmpty()); + } + + @Test + void isDoubleInRange_atMaxBoundary_returnsEmpty() { + assertTrue(Validator.isDoubleInRange("10.0", "Discount", 0.0, 10.0).isEmpty()); + } + + @Test + void isDoubleInRange_belowMin_returnsError() { + assertFalse(Validator.isDoubleInRange("-1.0", "Discount", 0.0, 10.0).isEmpty()); + } + + @Test + void isDoubleInRange_aboveMax_returnsError() { + assertFalse(Validator.isDoubleInRange("11.0", "Discount", 0.0, 10.0).isEmpty()); + } + + @Test + void isDoubleInRange_nonNumeric_returnsError() { + assertFalse(Validator.isDoubleInRange("abc", "Discount", 0.0, 10.0).isEmpty()); + } + + @Test + void isDoubleInRange_nullInput_returnsError() { + assertFalse(Validator.isDoubleInRange(null, "Discount", 0.0, 10.0).isEmpty()); + } + + //isNonNegativeInteger + @Test + void isNonNegativeInteger_positiveValue_returnsEmpty() { + assertTrue(Validator.isNonNegativeInteger("10", "Quantity").isEmpty()); + } + + @Test + void isNonNegativeInteger_zero_returnsEmpty() { + assertTrue(Validator.isNonNegativeInteger("0", "Quantity").isEmpty()); + } + + @Test + void isNonNegativeInteger_negativeValue_returnsError() { + assertFalse(Validator.isNonNegativeInteger("-1", "Quantity").isEmpty()); + } + + @Test + void isNonNegativeInteger_decimal_returnsError() { + assertFalse(Validator.isNonNegativeInteger("1.5", "Quantity").isEmpty()); + } + + @Test + void isNonNegativeInteger_nonNumeric_returnsError() { + assertFalse(Validator.isNonNegativeInteger("abc", "Quantity").isEmpty()); + } + + @Test + void isNonNegativeInteger_nullInput_returnsError() { + assertFalse(Validator.isNonNegativeInteger(null, "Quantity").isEmpty()); + } + + //isLessThanVarChars + @Test + void isLessThanVarChars_withinLimit_returnsEmpty() { + assertTrue(Validator.isLessThanVarChars("Hello", "Name", 10).isEmpty()); + } + + @Test + void isLessThanVarChars_exactlyAtLimit_returnsEmpty() { + assertTrue(Validator.isLessThanVarChars("Hello", "Name", 5).isEmpty()); + } + + @Test + void isLessThanVarChars_exceedsLimit_returnsError() { + assertFalse(Validator.isLessThanVarChars("Hello World", "Name", 5).isEmpty()); + } + + @Test + void isLessThanVarChars_nullInput_returnsError() { + assertFalse(Validator.isLessThanVarChars(null, "Name", 10).isEmpty()); + } + + @Test + void isLessThanVarChars_emptyString_returnsEmpty() { + assertTrue(Validator.isLessThanVarChars("", "Name", 5).isEmpty()); + } + + //isValidEmail + @Test + void isValidEmail_validEmail_returnsEmpty() { + assertTrue(Validator.isValidEmail("user@example.com", "Email").isEmpty()); + } + + @Test + void isValidEmail_missingAtSign_returnsError() { + assertFalse(Validator.isValidEmail("userexample.com", "Email").isEmpty()); + } + + @Test + void isValidEmail_missingDomain_returnsError() { + assertFalse(Validator.isValidEmail("user@", "Email").isEmpty()); + } + + @Test + void isValidEmail_shortTld_returnsError() { + assertFalse(Validator.isValidEmail("user@example.c", "Email").isEmpty()); + } + + @Test + void isValidEmail_nullInput_returnsError() { + assertFalse(Validator.isValidEmail(null, "Email").isEmpty()); + } + + @Test + void isValidEmail_emptyString_returnsError() { + assertFalse(Validator.isValidEmail("", "Email").isEmpty()); + } + + //isValidPhoneNumber + @Test + void isValidPhoneNumber_validFormat_returnsEmpty() { + assertTrue(Validator.isValidPhoneNumber("403-555-1234", "Phone").isEmpty()); + } + + @Test + void isValidPhoneNumber_missingDashes_returnsError() { + assertFalse(Validator.isValidPhoneNumber("4035551234", "Phone").isEmpty()); + } + + @Test + void isValidPhoneNumber_lettersPresent_returnsError() { + assertFalse(Validator.isValidPhoneNumber("abc-def-ghij", "Phone").isEmpty()); + } + + @Test + void isValidPhoneNumber_wrongSegmentLength_returnsError() { + assertFalse(Validator.isValidPhoneNumber("40-5551-234", "Phone").isEmpty()); + } + + @Test + void isValidPhoneNumber_nullInput_returnsError() { + assertFalse(Validator.isValidPhoneNumber(null, "Phone").isEmpty()); + } + + @Test + void isValidPhoneNumber_emptyString_returnsError() { + assertFalse(Validator.isValidPhoneNumber("", "Phone").isEmpty()); + } +}