refactor: PasswordGen (#5373)

This commit is contained in:
Alex Klymenko 2024-08-24 10:57:40 +02:00 committed by GitHub
parent 4e72056527
commit 75355e87b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 13 deletions

View File

@ -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<Character> letters = new ArrayList<Character>();
for (char c : allChars.toCharArray()) {
List<Character> 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();

View File

@ -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());
}
}