Browse Source

implement check for existing uppercase character in password

feature-password-validator
binsky 3 years ago
parent
commit
2720667310
  1. 20
      src/main/java/PasswordValidator.java
  2. 7
      src/test/java/PasswordValidatorTest.java

20
src/main/java/PasswordValidator.java

@ -1,8 +1,18 @@
import java.util.regex.Pattern;
public class PasswordValidator { public class PasswordValidator {
int minLength = 6; int minLength = 6;
boolean requireUppercase = true;
private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$");
public boolean validate(String password) { 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() { public int getMinLength() {
@ -12,4 +22,12 @@ public class PasswordValidator {
public void setMinLength(int minLength) { public void setMinLength(int minLength) {
this.minLength = minLength; this.minLength = minLength;
} }
public boolean isRequireUppercase() {
return requireUppercase;
}
public void setRequireUppercase(boolean requireUppercase) {
this.requireUppercase = requireUppercase;
}
} }

7
src/test/java/PasswordValidatorTest.java

@ -11,8 +11,15 @@ class PasswordValidatorTest {
// test minimum password length // test minimum password length
passwordValidator.setMinLength(6); passwordValidator.setMinLength(6);
passwordValidator.setRequireUppercase(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
passwordValidator.setRequireUppercase(true);
assertFalse(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abCdef"));
assertTrue(passwordValidator.validate("ABCDEF"));
} }
} }
Loading…
Cancel
Save