From 75355e87b6658e19164866bfcf31b9f5bbd0051d Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sat, 24 Aug 2024 10:57:40 +0200 Subject: [PATCH] refactor: `PasswordGen` (#5373) --- .../com/thealgorithms/others/PasswordGen.java | 33 ++++++++++++------- .../thealgorithms/others/PasswordGenTest.java | 23 +++++++++++-- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/PasswordGen.java b/src/main/java/com/thealgorithms/others/PasswordGen.java index 7d21f112..da9f21bc 100644 --- a/src/main/java/com/thealgorithms/others/PasswordGen.java +++ b/src/main/java/com/thealgorithms/others/PasswordGen.java @@ -12,21 +12,32 @@ import java.util.Random; * @date 2017.10.25 */ final class PasswordGen { + private static final String UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"; + private static final String DIGITS = "0123456789"; + private static final String SPECIAL_CHARACTERS = "!@#$%^&*(){}?"; + private static final String ALL_CHARACTERS = UPPERCASE_LETTERS + LOWERCASE_LETTERS + DIGITS + SPECIAL_CHARACTERS; + private PasswordGen() { } - static String generatePassword(int minLength, int maxLength) { + /** + * Generates a random password with a length between minLength and maxLength. + * + * @param minLength The minimum length of the password. + * @param maxLength The maximum length of the password. + * @return A randomly generated password. + * @throws IllegalArgumentException if minLength is greater than maxLength or if either is non-positive. + */ + public static String generatePassword(int minLength, int maxLength) { + if (minLength > maxLength || minLength <= 0 || maxLength <= 0) { + throw new IllegalArgumentException("Incorrect length parameters: minLength must be <= maxLength and both must be > 0"); + } + Random random = new Random(); - String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - String lower = "abcdefghijklmnopqrstuvwxyz"; - String numbers = "0123456789"; - String specialChars = "!@#$%^&*(){}?"; - - String allChars = upper + lower + numbers + specialChars; - - List letters = new ArrayList(); - for (char c : allChars.toCharArray()) { + List letters = new ArrayList<>(); + for (char c : ALL_CHARACTERS.toCharArray()) { letters.add(c); } @@ -36,7 +47,7 @@ final class PasswordGen { // Note that size of the password is also random for (int i = random.nextInt(maxLength - minLength) + minLength; i > 0; --i) { - password.append(letters.get(random.nextInt(letters.size()))); + password.append(ALL_CHARACTERS.charAt(random.nextInt(ALL_CHARACTERS.length()))); } return password.toString(); diff --git a/src/test/java/com/thealgorithms/others/PasswordGenTest.java b/src/test/java/com/thealgorithms/others/PasswordGenTest.java index 57de329c..76492556 100644 --- a/src/test/java/com/thealgorithms/others/PasswordGenTest.java +++ b/src/test/java/com/thealgorithms/others/PasswordGenTest.java @@ -1,5 +1,6 @@ package com.thealgorithms.others; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -10,7 +11,7 @@ public class PasswordGenTest { @Test public void failGenerationWithSameMinMaxLengthTest() { int length = 10; - assertThrows(IllegalArgumentException.class, () -> { PasswordGen.generatePassword(length, length); }); + assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(length, length)); } @Test @@ -23,7 +24,7 @@ public class PasswordGenTest { public void failGenerationWithMinLengthSmallerThanMaxLengthTest() { int minLength = 10; int maxLength = 5; - assertThrows(IllegalArgumentException.class, () -> { PasswordGen.generatePassword(minLength, maxLength); }); + assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(minLength, maxLength)); } @Test @@ -31,4 +32,22 @@ public class PasswordGenTest { String tempPassword = PasswordGen.generatePassword(8, 16); assertTrue(tempPassword.length() != 0); } + + @Test + public void testGeneratePasswordWithMinGreaterThanMax() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(12, 8)); + assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage()); + } + + @Test + public void testGeneratePasswordWithNegativeLength() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(-5, 10)); + assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage()); + } + + @Test + public void testGeneratePasswordWithZeroLength() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(0, 0)); + assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage()); + } }