Created
December 23, 2011 00:18
-
-
Save tfnico/1512455 to your computer and use it in GitHub Desktop.
Some business rule tests
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
public static boolean isCodeAllowed(String code) { | |
return isNotNullOrEmpty(code) && | |
onlyLettersCorrectLength(code) && | |
lettersAndNumbersCorrectLength(code) && | |
numbersOnlyNotAllowed(code) && | |
plusSignOnlyParticularity(code); | |
} |
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
public static boolean isCodeAllowed(String code) { | |
Predicate<String> predicate = and( | |
not(NULL_OR_EMPTY), | |
ONLY_LETTERS_CORRECT_LENGTH, | |
LETTERS_AND_NUMBERS_CORRECT_LENGTH, | |
not(matching(NUMBERS_ONLY)), | |
PLUS_SIGN_PARTICULARITY); | |
return predicate.apply(code); | |
} |
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
@Test | |
public void cannot_be_blank_or_null(){ | |
assertFalse(isCodeAllowed(null)); | |
assertFalse(isCodeAllowed("")); | |
} | |
@Test | |
public void only_letters_length_must_be_between_10_and_20() { | |
assertFalse(isCodeAllowed("ABCDEFGHIJ")); // 10 chars | |
assertTrue(isCodeAllowed("ABCDEFGHIJK"));// 11 | |
assertTrue(isCodeAllowed("ABCDEFGHIJ" + "ABCDEFGHI")); // 19 chars | |
assertFalse(isCodeAllowed("ABCDEFGHIJ" + "ABCDEFGHIJ")); // 20 chars | |
} | |
@Test | |
public void mixed_length_must_be_either_10_or_15() { | |
assertTrue(isCodeAllowed("ABCDEF1234")); // 10 | |
assertTrue(isCodeAllowed("ABCDEF12345ABCD")); // 15 | |
assertFalse(isCodeAllowed("ABCDEF12345A")); // 12 | |
} | |
@Test | |
public void numbers_only_are_not_allowed() { | |
assertFalse(isCodeAllowed("123")); | |
} | |
@Test | |
public void plus_sign_allowed_if_starts_with_999_and_max_length_is_10() throws Exception { | |
assertTrue(isCodeAllowed("999+")); | |
assertTrue(isCodeAllowed("99912C45+6")); // 10 chars | |
assertFalse(isCodeAllowed("99912C45+67")); // 11 chars | |
assertFalse(isCodeAllowed("88812C45+6")); // 10 chars, wrong prefix | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment