Browse Source

implement check for existing lowercase character in password

feature-password-validator
binsky 3 years ago
parent
commit
660844010e
  1. 12
      src/main/java/PasswordValidator.java
  2. 9
      src/test/java/PasswordValidatorTest.java

12
src/main/java/PasswordValidator.java

@ -3,14 +3,18 @@ import java.util.regex.Pattern;
public class PasswordValidator { public class PasswordValidator {
int minLength = 6; int minLength = 6;
boolean requireUppercase = true; boolean requireUppercase = true;
boolean requireLowercase = true;
private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$"); private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$");
private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$");
public boolean validate(String password) { public boolean validate(String password) {
if (password.length() < minLength) { if (password.length() < minLength) {
return false; return false;
} else if (requireUppercase && !uppercasePattern.matcher(password).matches()) { } else if (requireUppercase && !uppercasePattern.matcher(password).matches()) {
return false; return false;
} else if (requireLowercase && !lowercasePattern.matcher(password).matches()) {
return false;
} }
return true; return true;
} }
@ -30,4 +34,12 @@ public class PasswordValidator {
public void setRequireUppercase(boolean requireUppercase) { public void setRequireUppercase(boolean requireUppercase) {
this.requireUppercase = requireUppercase; this.requireUppercase = requireUppercase;
} }
public boolean isRequireLowercase() {
return requireLowercase;
}
public void setRequireLowercase(boolean requireLowercase) {
this.requireLowercase = requireLowercase;
}
} }

9
src/test/java/PasswordValidatorTest.java

@ -12,14 +12,23 @@ class PasswordValidatorTest {
// test minimum password length // test minimum password length
passwordValidator.setMinLength(6); passwordValidator.setMinLength(6);
passwordValidator.setRequireUppercase(false); passwordValidator.setRequireUppercase(false);
passwordValidator.setRequireLowercase(false);
assertFalse(passwordValidator.validate("abcde")); assertFalse(passwordValidator.validate("abcde"));
assertTrue(passwordValidator.validate("abcdef")); assertTrue(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abcdefg")); assertTrue(passwordValidator.validate("abcdefg"));
// test uppercase requirement // test uppercase requirement
passwordValidator.setRequireUppercase(true); passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(false);
assertFalse(passwordValidator.validate("abcdef")); assertFalse(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abCdef")); assertTrue(passwordValidator.validate("abCdef"));
assertTrue(passwordValidator.validate("ABCDEF")); assertTrue(passwordValidator.validate("ABCDEF"));
// test lowercase requirement
passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(true);
assertFalse(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abCdef"));
assertFalse(passwordValidator.validate("ABCDEF"));
} }
} }
Loading…
Cancel
Save