Last active
March 17, 2016 16:17
-
-
Save RyanBreaker/7c20bcfb0239f027d74d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package csci242.assignments.stringhandler; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static csci242.assignments.stringhandler.StringHandlerTestHelper.*; | |
import static org.junit.Assert.*; | |
/** | |
* Short description. | |
* <p> | |
* Long description. | |
* | |
* @author Ryan Breaker | |
* @edu.uwp.cs.242.course CSCI242 - Computer Science II | |
* @edu.uwp.cs.242.section 001 | |
* @edu.uwp.cs.242.assignment 3 | |
* @bugs None | |
*/ | |
public class PasswordSecurityHandlerTests { | |
PasswordSecurityHandler passwordHandler; | |
@Before | |
public void setUp() throws Exception { | |
// Reset passwordHandler for each test. | |
passwordHandler = new PasswordSecurityHandler(); | |
// Test initial values. | |
assertFalse(passwordHandler.getDigit()); | |
assertFalse(passwordHandler.getOtherCharacter()); | |
} | |
//region Security tests | |
@Test | |
public void testSecurityLevelWeak() throws Exception { | |
String password = "foobar"; | |
StringParser parser = new StringParser(passwordHandler); | |
parser.parse(password); | |
assertEquals(password.length(), passwordHandler.getLength()); | |
assertEquals(PasswordSecurityHandler.SECURITY_LEVEL_WEAK, | |
passwordHandler.securityLevel()); | |
assertFalse(passwordHandler.getDigit()); | |
assertFalse(passwordHandler.getOtherCharacter()); | |
} | |
@Test | |
public void testSecurityMediumDigit() throws Exception { | |
String password = "foobar123"; | |
StringParser parser = new StringParser(passwordHandler); | |
parser.parse(password); | |
assertEquals(password.length(), passwordHandler.getLength()); | |
assertEquals(PasswordSecurityHandler.SECURITY_LEVEL_MEDIUM, | |
passwordHandler.securityLevel()); | |
assertTrue(passwordHandler.getDigit()); | |
assertFalse(passwordHandler.getOtherCharacter()); | |
} | |
@Test | |
public void testSecurityMediumOther() throws Exception { | |
String password = "foobar!@#"; | |
StringParser parser = new StringParser(passwordHandler); | |
parser.parse(password); | |
assertEquals(password.length(), passwordHandler.getLength()); | |
assertEquals(PasswordSecurityHandler.SECURITY_LEVEL_MEDIUM, | |
passwordHandler.securityLevel()); | |
assertFalse(passwordHandler.getDigit()); | |
assertTrue(passwordHandler.getOtherCharacter()); | |
} | |
@Test | |
public void testSecurityStrong() throws Exception { | |
String password = "foobar123!@#"; | |
StringParser parser = new StringParser(passwordHandler); | |
parser.parse(password); | |
assertEquals(password.length(), passwordHandler.getLength()); | |
assertEquals(PasswordSecurityHandler.SECURITY_LEVEL_STRONG, | |
passwordHandler.securityLevel()); | |
assertTrue(passwordHandler.getDigit()); | |
assertTrue(passwordHandler.getOtherCharacter()); | |
} | |
//endregion | |
@Test | |
public void testProcessDigit() throws Exception { | |
loopTest(Character::isDigit, passwordHandler::processDigit, | |
"processDigit"); | |
} | |
@Test | |
public void testProcessLetter() throws Exception { | |
loopTest(Character::isAlphabetic, passwordHandler::processLetter, | |
"processLetter"); | |
} | |
@Test | |
public void testProcessOther() throws Exception { | |
loopTest((c) -> !(Character.isDigit(c) || Character.isAlphabetic(c)), | |
passwordHandler::processOther, "processOther"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package csci242.assignments.stringhandler; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import static org.junit.Assert.fail; | |
/** | |
* Short description. | |
* <p> | |
* Long description. | |
* | |
* @author Ryan Breaker | |
* @edu.uwp.cs.242.course CSCI242 - Computer Science II | |
* @edu.uwp.cs.242.section 001 | |
* @edu.uwp.cs.242.assignment 3 | |
* @bugs None | |
*/ | |
public class StringHandlerTestHelper { | |
protected enum CharType { | |
Digit, Letter, Other | |
} | |
private static Function<Character, Boolean> checker; | |
private StringHandlerTestHelper() {} | |
private static boolean isOther(char c) { | |
return !(Character.isDigit(c) || Character.isAlphabetic(c)); | |
} | |
private static void failWithMessage(CharType charType, char c) { | |
fail(String.format("Type %s failed at '%c' (%d).", charType, c, (int)c)); | |
} | |
protected static void loopTest(Predicate<Character> validator, | |
Consumer<Character> processor, String processorName) | |
throws Exception { | |
for(char i = 0; i < 256; i++) { | |
boolean valid = validator.test(i); | |
try { | |
processor.accept(i); | |
} catch(IllegalArgumentException e) { | |
if(valid) { | |
// Exception incorrectly thrown. | |
fail(processorName + " threw incorrectly at: " + i); | |
} | |
// Exception correctly thrown, continue. | |
continue; | |
} | |
if(!valid) { | |
// Exception should have been thrown. | |
fail(processorName + " should have thrown at: " + i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment