From 27206673103ae39fd1cb40b29fb9bed90c397cc4 Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 5 Feb 2022 14:48:51 +0100 Subject: [PATCH] implement check for existing uppercase character in password --- src/main/java/PasswordValidator.java | 20 +++++++++++++++++++- src/test/java/PasswordValidatorTest.java | 7 +++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/PasswordValidator.java b/src/main/java/PasswordValidator.java index 123fa9b..5c126a7 100644 --- a/src/main/java/PasswordValidator.java +++ b/src/main/java/PasswordValidator.java @@ -1,8 +1,18 @@ +import java.util.regex.Pattern; + public class PasswordValidator { int minLength = 6; + boolean requireUppercase = true; + + private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$"); public boolean validate(String password) { - return password.length() >= minLength; + if (password.length() < minLength) { + return false; + } else if (requireUppercase && !uppercasePattern.matcher(password).matches()) { + return false; + } + return true; } public int getMinLength() { @@ -12,4 +22,12 @@ public class PasswordValidator { public void setMinLength(int minLength) { this.minLength = minLength; } + + public boolean isRequireUppercase() { + return requireUppercase; + } + + public void setRequireUppercase(boolean requireUppercase) { + this.requireUppercase = requireUppercase; + } } diff --git a/src/test/java/PasswordValidatorTest.java b/src/test/java/PasswordValidatorTest.java index 0079f22..9794e36 100644 --- a/src/test/java/PasswordValidatorTest.java +++ b/src/test/java/PasswordValidatorTest.java @@ -11,8 +11,15 @@ class PasswordValidatorTest { // test minimum password length passwordValidator.setMinLength(6); + passwordValidator.setRequireUppercase(false); assertFalse(passwordValidator.validate("abcde")); assertTrue(passwordValidator.validate("abcdef")); assertTrue(passwordValidator.validate("abcdefg")); + + // test uppercase requirement + passwordValidator.setRequireUppercase(true); + assertFalse(passwordValidator.validate("abcdef")); + assertTrue(passwordValidator.validate("abCdef")); + assertTrue(passwordValidator.validate("ABCDEF")); } }