Browse Source

implement check for existing digit in password

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

12
src/main/java/PasswordValidator.java

@ -4,9 +4,11 @@ public class PasswordValidator {
int minLength = 6; int minLength = 6;
boolean requireUppercase = true; boolean requireUppercase = true;
boolean requireLowercase = true; boolean requireLowercase = true;
boolean requireDigit = 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]).+$"); private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$");
private final Pattern digitPattern = Pattern.compile("^(?=.*\\d).+$");
public boolean validate(String password) { public boolean validate(String password) {
if (password.length() < minLength) { if (password.length() < minLength) {
@ -15,6 +17,8 @@ public class PasswordValidator {
return false; return false;
} else if (requireLowercase && !lowercasePattern.matcher(password).matches()) { } else if (requireLowercase && !lowercasePattern.matcher(password).matches()) {
return false; return false;
} else if (requireDigit && !digitPattern.matcher(password).matches()) {
return false;
} }
return true; return true;
} }
@ -42,4 +46,12 @@ public class PasswordValidator {
public void setRequireLowercase(boolean requireLowercase) { public void setRequireLowercase(boolean requireLowercase) {
this.requireLowercase = requireLowercase; this.requireLowercase = requireLowercase;
} }
public boolean isRequireDigit() {
return requireDigit;
}
public void setRequireDigit(boolean requireDigit) {
this.requireDigit = requireDigit;
}
} }

16
src/test/java/PasswordValidatorTest.java

@ -13,6 +13,7 @@ class PasswordValidatorTest {
passwordValidator.setMinLength(6); passwordValidator.setMinLength(6);
passwordValidator.setRequireUppercase(false); passwordValidator.setRequireUppercase(false);
passwordValidator.setRequireLowercase(false); passwordValidator.setRequireLowercase(false);
passwordValidator.setRequireDigit(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"));
@ -20,6 +21,7 @@ class PasswordValidatorTest {
// test uppercase requirement // test uppercase requirement
passwordValidator.setRequireUppercase(true); passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(false); passwordValidator.setRequireLowercase(false);
passwordValidator.setRequireDigit(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"));
@ -27,8 +29,22 @@ class PasswordValidatorTest {
// test lowercase requirement // test lowercase requirement
passwordValidator.setRequireUppercase(true); passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(true); passwordValidator.setRequireLowercase(true);
passwordValidator.setRequireDigit(false);
assertFalse(passwordValidator.validate("abcdef")); assertFalse(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abCdef")); assertTrue(passwordValidator.validate("abCdef"));
assertFalse(passwordValidator.validate("ABCDEF")); assertFalse(passwordValidator.validate("ABCDEF"));
// test digits requirement
passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(true);
passwordValidator.setRequireDigit(true);
assertFalse(passwordValidator.validate("8"));
assertFalse(passwordValidator.validate("12345678"));
assertFalse(passwordValidator.validate("abcdef"));
assertFalse(passwordValidator.validate("abcdef8"));
assertFalse(passwordValidator.validate("abCdef"));
assertFalse(passwordValidator.validate("ABCDEF"));
assertFalse(passwordValidator.validate("ABCDEF8"));
assertTrue(passwordValidator.validate("abCDE8F"));
} }
} }
Loading…
Cancel
Save