Last active
January 17, 2019 06:20
-
-
Save Ten-Wang/5e520f9b3e15f69946e76dd6b2b7ecea to your computer and use it in GitHub Desktop.
測試密碼。(密碼條件 至少應包含數字、大小寫英文一個,排除特殊字元、至少需8個以上的字元)
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 class ValidUtils { | |
// (?=.*\d) # must contains one digit from 0-9 | |
// (?=.*[a-zA-Z]) # must contains one alphabet characters | |
// (?!.*[\W_]) # must no special characters | |
// . # match anything with previous condition checking | |
// {8,} # length at least 8 characters | |
private final static String STR_PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-zA-Z])(?!.*[\\W_]).{8,})"; | |
private final static Pattern PASSWORD_PATTERN = Pattern.compile(STR_PASSWORD_PATTERN); | |
public static boolean isPasswordValid(String password) { | |
return PASSWORD_PATTERN.matcher(password).matches(); | |
} | |
public static boolean isUrlValid(String url) { | |
return android.util.Patterns.WEB_URL.matcher(url).matches(); | |
} | |
} | |
public class ValidUtils_isPasswordValidTest { | |
@Test | |
public void positive_case() { | |
// input 正確的 password | |
// 預期結果為true | |
assertTrue(ValidUtils.isPasswordValid("1234qazw")); | |
} | |
@Test | |
public void negative_case() { | |
// input 短於長度條件的 password | |
// 預期結果為 false | |
assertFalse(ValidUtils.isPasswordValid("2wsx22")); | |
// input 全數字的 password | |
// 預期結果為 false | |
assertFalse(ValidUtils.isPasswordValid("12345678")); | |
// input 參雜特殊字元的 password | |
// 預期結果為 false | |
assertFalse(ValidUtils.isPasswordValid("1234,ter")); | |
} | |
} | |
public class ValidUtils_isUrlValidTest { | |
@Test | |
public void positive_case() { | |
// input 正確的 Url | |
// 預期結果為true | |
assertTrue(ValidUtils.isUrlValid("https://google.com")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment