Skip to content

Commit 3b9e22b

Browse files
author
alxkm
committed
refactor: PasswordGen
1 parent ce4eb55 commit 3b9e22b

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

src/main/java/com/thealgorithms/others/PasswordGen.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,32 @@
1212
* @date 2017.10.25
1313
*/
1414
final class PasswordGen {
15+
private static final String UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
16+
private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
17+
private static final String DIGITS = "0123456789";
18+
private static final String SPECIAL_CHARACTERS = "!@#$%^&*(){}?";
19+
private static final String ALL_CHARACTERS = UPPERCASE_LETTERS + LOWERCASE_LETTERS + DIGITS + SPECIAL_CHARACTERS;
20+
1521
private PasswordGen() {
1622
}
1723

18-
static String generatePassword(int minLength, int maxLength) {
19-
Random random = new Random();
20-
21-
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
22-
String lower = "abcdefghijklmnopqrstuvwxyz";
23-
String numbers = "0123456789";
24-
String specialChars = "!@#$%^&*(){}?";
24+
/**
25+
* Generates a random password with a length between minLength and maxLength.
26+
*
27+
* @param minLength The minimum length of the password.
28+
* @param maxLength The maximum length of the password.
29+
* @return A randomly generated password.
30+
* @throws IllegalArgumentException if minLength is greater than maxLength or if either is non-positive.
31+
*/
32+
public static String generatePassword(int minLength, int maxLength) {
33+
if (minLength > maxLength || minLength <= 0 || maxLength <= 0) {
34+
throw new IllegalArgumentException("Incorrect length parameters: minLength must be <= maxLength and both must be > 0");
35+
}
2536

26-
String allChars = upper + lower + numbers + specialChars;
37+
Random random = new Random();
2738

28-
List<Character> letters = new ArrayList<Character>();
29-
for (char c : allChars.toCharArray()) {
39+
List<Character> letters = new ArrayList<>();
40+
for (char c : ALL_CHARACTERS.toCharArray()) {
3041
letters.add(c);
3142
}
3243

@@ -36,7 +47,7 @@ static String generatePassword(int minLength, int maxLength) {
3647

3748
// Note that size of the password is also random
3849
for (int i = random.nextInt(maxLength - minLength) + minLength; i > 0; --i) {
39-
password.append(letters.get(random.nextInt(letters.size())));
50+
password.append(ALL_CHARACTERS.charAt(random.nextInt(ALL_CHARACTERS.length())));
4051
}
4152

4253
return password.toString();

src/test/java/com/thealgorithms/others/PasswordGenTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.others;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

@@ -10,7 +11,7 @@ public class PasswordGenTest {
1011
@Test
1112
public void failGenerationWithSameMinMaxLengthTest() {
1213
int length = 10;
13-
assertThrows(IllegalArgumentException.class, () -> { PasswordGen.generatePassword(length, length); });
14+
assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(length, length));
1415
}
1516

1617
@Test
@@ -23,12 +24,30 @@ public void generateOneCharacterPassword() {
2324
public void failGenerationWithMinLengthSmallerThanMaxLengthTest() {
2425
int minLength = 10;
2526
int maxLength = 5;
26-
assertThrows(IllegalArgumentException.class, () -> { PasswordGen.generatePassword(minLength, maxLength); });
27+
assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(minLength, maxLength));
2728
}
2829

2930
@Test
3031
public void generatePasswordNonEmptyTest() {
3132
String tempPassword = PasswordGen.generatePassword(8, 16);
3233
assertTrue(tempPassword.length() != 0);
3334
}
35+
36+
@Test
37+
public void testGeneratePasswordWithMinGreaterThanMax() {
38+
Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(12, 8));
39+
assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage());
40+
}
41+
42+
@Test
43+
public void testGeneratePasswordWithNegativeLength() {
44+
Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(-5, 10));
45+
assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage());
46+
}
47+
48+
@Test
49+
public void testGeneratePasswordWithZeroLength() {
50+
Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(0, 0));
51+
assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage());
52+
}
3453
}

0 commit comments

Comments
 (0)