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
2 changed files with 43 additions and 13 deletions

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