Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JUnit Tests and fixed bug in isAlphanumeric method #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/PasswordChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ public String describePasswordLength(String password) {
* @return true if the password is alphanumeric, false otherwise
*/
public boolean isAlphanumeric(String password) {
for (int i = 0; i < password.length() - 1; i++) {
char c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
} else {
return true;
if (password.length() < 1) {
return false;
} else {
for (int i = 0; i < password.length() - 1; i++) {
char c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
}
}
return true;
}
return true;
}

/**
Expand Down
64 changes: 64 additions & 0 deletions src/PasswordCheckerTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

Expand All @@ -11,4 +12,67 @@ public void testDummy() {
assertEquals(0, 0);
}

@Test
public void isThreeAShortPassword() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
String actualResult = passwordUtils.describePasswordLength("zzz");
assertEquals("short", actualResult);
}

@Test
public void isSixAMediumPassword() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
String actualResult = passwordUtils.describePasswordLength("zzzzzz");
assertEquals("medium", actualResult);
}

@Test
public void isAnEmptyStringAShortPassword() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
String actualResult = passwordUtils.describePasswordLength(" ");
assertEquals("short", actualResult);
}

@Test
public void isAnEmptyStringHandledCorrectly() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
Boolean actualResult = passwordUtils.isAlphanumeric("");
assertEquals(false, actualResult);
}

@Test
public void nonAlphaNumericDetectedCorrectly() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
Boolean actualResult = passwordUtils.isAlphanumeric("er^^ty");
assertEquals(false, actualResult);
}

@Test
public void alphaNumericDetectedCorrectly() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
Boolean actualResult = passwordUtils.isAlphanumeric("erty");
assertEquals(true, actualResult);
}

@Test
public void checkForBannedPasswordsEmptySring() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
Boolean actualResult = passwordUtils.isBannedPassword("");
assertEquals(false, actualResult);
}

@Test
public void checkForBannedPasswordsQwerty() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
Boolean actualResult = passwordUtils.isBannedPassword("qwerty");
assertEquals(true, actualResult);
}

@Test
public void checkForBannedPasswordsQwertyCaps() {
PasswordChecker passwordUtils = new PasswordChecker(6, 12);
Boolean actualResult = passwordUtils.isBannedPassword("QWERTY");
assertEquals(true, actualResult);
}

}