From 1634188b762e90cd0ab8bf93d3d3b17e0633a70f Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 5 Feb 2022 14:28:31 +0100 Subject: [PATCH 1/6] add PasswordValidator class with validate method --- src/main/java/PasswordValidator.java | 6 ++++++ src/test/java/PasswordValidatorTest.java | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/main/java/PasswordValidator.java create mode 100644 src/test/java/PasswordValidatorTest.java diff --git a/src/main/java/PasswordValidator.java b/src/main/java/PasswordValidator.java new file mode 100644 index 0000000..3825cd5 --- /dev/null +++ b/src/main/java/PasswordValidator.java @@ -0,0 +1,6 @@ +public class PasswordValidator { + + public boolean validate(String password) { + return false; + } +} diff --git a/src/test/java/PasswordValidatorTest.java b/src/test/java/PasswordValidatorTest.java new file mode 100644 index 0000000..5af0485 --- /dev/null +++ b/src/test/java/PasswordValidatorTest.java @@ -0,0 +1,12 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PasswordValidatorTest { + + @Test + void validate() { + PasswordValidator passwordValidator = new PasswordValidator(); + assertFalse(passwordValidator.validate("")); + } +} \ No newline at end of file From 1a037c1927174a9fdc4677e7b1508ce06cc46861 Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 5 Feb 2022 14:31:22 +0100 Subject: [PATCH 2/6] implement check for minimum password length --- src/main/java/PasswordValidator.java | 3 ++- src/test/java/PasswordValidatorTest.java | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/PasswordValidator.java b/src/main/java/PasswordValidator.java index 3825cd5..e469c92 100644 --- a/src/main/java/PasswordValidator.java +++ b/src/main/java/PasswordValidator.java @@ -1,6 +1,7 @@ public class PasswordValidator { + int minLength = 6; public boolean validate(String password) { - return false; + return password.length() >= minLength; } } diff --git a/src/test/java/PasswordValidatorTest.java b/src/test/java/PasswordValidatorTest.java index 5af0485..e40fa23 100644 --- a/src/test/java/PasswordValidatorTest.java +++ b/src/test/java/PasswordValidatorTest.java @@ -8,5 +8,8 @@ class PasswordValidatorTest { void validate() { PasswordValidator passwordValidator = new PasswordValidator(); assertFalse(passwordValidator.validate("")); + + // test minimum password length + assertTrue(passwordValidator.validate("abcdef")); } -} \ No newline at end of file +} From 5de9fadc4068cb70b4adaeb9897f3df4fd5422b7 Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 5 Feb 2022 14:34:58 +0100 Subject: [PATCH 3/6] refactor minimum password length tests and add getter/setter --- src/main/java/PasswordValidator.java | 8 ++++++++ src/test/java/PasswordValidatorTest.java | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/PasswordValidator.java b/src/main/java/PasswordValidator.java index e469c92..123fa9b 100644 --- a/src/main/java/PasswordValidator.java +++ b/src/main/java/PasswordValidator.java @@ -4,4 +4,12 @@ public class PasswordValidator { public boolean validate(String password) { return password.length() >= minLength; } + + public int getMinLength() { + return minLength; + } + + public void setMinLength(int minLength) { + this.minLength = minLength; + } } diff --git a/src/test/java/PasswordValidatorTest.java b/src/test/java/PasswordValidatorTest.java index e40fa23..0079f22 100644 --- a/src/test/java/PasswordValidatorTest.java +++ b/src/test/java/PasswordValidatorTest.java @@ -10,6 +10,9 @@ class PasswordValidatorTest { assertFalse(passwordValidator.validate("")); // test minimum password length + passwordValidator.setMinLength(6); + assertFalse(passwordValidator.validate("abcde")); assertTrue(passwordValidator.validate("abcdef")); + assertTrue(passwordValidator.validate("abcdefg")); } } From 27206673103ae39fd1cb40b29fb9bed90c397cc4 Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 5 Feb 2022 14:48:51 +0100 Subject: [PATCH 4/6] 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")); } } From 660844010e0b0870d93136fbc957aca42340773c Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 5 Feb 2022 14:52:48 +0100 Subject: [PATCH 5/6] implement check for existing lowercase character in password --- src/main/java/PasswordValidator.java | 12 ++++++++++++ src/test/java/PasswordValidatorTest.java | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/src/main/java/PasswordValidator.java b/src/main/java/PasswordValidator.java index 5c126a7..71367da 100644 --- a/src/main/java/PasswordValidator.java +++ b/src/main/java/PasswordValidator.java @@ -3,14 +3,18 @@ import java.util.regex.Pattern; public class PasswordValidator { int minLength = 6; boolean requireUppercase = true; + boolean requireLowercase = true; private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$"); + private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$"); public boolean validate(String password) { if (password.length() < minLength) { return false; } else if (requireUppercase && !uppercasePattern.matcher(password).matches()) { return false; + } else if (requireLowercase && !lowercasePattern.matcher(password).matches()) { + return false; } return true; } @@ -30,4 +34,12 @@ public class PasswordValidator { public void setRequireUppercase(boolean requireUppercase) { this.requireUppercase = requireUppercase; } + + public boolean isRequireLowercase() { + return requireLowercase; + } + + public void setRequireLowercase(boolean requireLowercase) { + this.requireLowercase = requireLowercase; + } } diff --git a/src/test/java/PasswordValidatorTest.java b/src/test/java/PasswordValidatorTest.java index 9794e36..96c050d 100644 --- a/src/test/java/PasswordValidatorTest.java +++ b/src/test/java/PasswordValidatorTest.java @@ -12,14 +12,23 @@ class PasswordValidatorTest { // test minimum password length passwordValidator.setMinLength(6); passwordValidator.setRequireUppercase(false); + passwordValidator.setRequireLowercase(false); assertFalse(passwordValidator.validate("abcde")); assertTrue(passwordValidator.validate("abcdef")); assertTrue(passwordValidator.validate("abcdefg")); // test uppercase requirement passwordValidator.setRequireUppercase(true); + passwordValidator.setRequireLowercase(false); assertFalse(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")); } } From e5b8d493969786f7a0bf31bec92fd6797c3e8556 Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 5 Feb 2022 14:58:06 +0100 Subject: [PATCH 6/6] implement check for existing digit in password --- src/main/java/PasswordValidator.java | 12 ++++++++++++ src/test/java/PasswordValidatorTest.java | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/PasswordValidator.java b/src/main/java/PasswordValidator.java index 71367da..9cb4f03 100644 --- a/src/main/java/PasswordValidator.java +++ b/src/main/java/PasswordValidator.java @@ -4,9 +4,11 @@ public class PasswordValidator { int minLength = 6; boolean requireUppercase = true; boolean requireLowercase = true; + boolean requireDigit = true; private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$"); private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$"); + private final Pattern digitPattern = Pattern.compile("^(?=.*\\d).+$"); public boolean validate(String password) { if (password.length() < minLength) { @@ -15,6 +17,8 @@ public class PasswordValidator { return false; } else if (requireLowercase && !lowercasePattern.matcher(password).matches()) { return false; + } else if (requireDigit && !digitPattern.matcher(password).matches()) { + return false; } return true; } @@ -42,4 +46,12 @@ public class PasswordValidator { public void setRequireLowercase(boolean requireLowercase) { this.requireLowercase = requireLowercase; } + + public boolean isRequireDigit() { + return requireDigit; + } + + public void setRequireDigit(boolean requireDigit) { + this.requireDigit = requireDigit; + } } diff --git a/src/test/java/PasswordValidatorTest.java b/src/test/java/PasswordValidatorTest.java index 96c050d..78d9335 100644 --- a/src/test/java/PasswordValidatorTest.java +++ b/src/test/java/PasswordValidatorTest.java @@ -13,6 +13,7 @@ class PasswordValidatorTest { passwordValidator.setMinLength(6); passwordValidator.setRequireUppercase(false); passwordValidator.setRequireLowercase(false); + passwordValidator.setRequireDigit(false); assertFalse(passwordValidator.validate("abcde")); assertTrue(passwordValidator.validate("abcdef")); assertTrue(passwordValidator.validate("abcdefg")); @@ -20,6 +21,7 @@ class PasswordValidatorTest { // test uppercase requirement passwordValidator.setRequireUppercase(true); passwordValidator.setRequireLowercase(false); + passwordValidator.setRequireDigit(false); assertFalse(passwordValidator.validate("abcdef")); assertTrue(passwordValidator.validate("abCdef")); assertTrue(passwordValidator.validate("ABCDEF")); @@ -27,8 +29,22 @@ class PasswordValidatorTest { // test lowercase requirement passwordValidator.setRequireUppercase(true); passwordValidator.setRequireLowercase(true); + passwordValidator.setRequireDigit(false); assertFalse(passwordValidator.validate("abcdef")); assertTrue(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")); } }