ListDomainsCheckerImpl extends AbstractEmailChecker { @Override public boolean check(String email) { if (email == null) { return false; } int pos = email.indexOf('@'); if (pos == -1) { return false; } String domainPart = email.substring(pos + 1); if ((listDomains == null || listDomains.isEmpty()) && !isBlackListDomains) { return false; } if (listDomains != null) { if (isBlackListDomains) { return !checkListDomainsContainsDomainPart(domainPart); } else { return checkListDomainsContainsDomainPart(domainPart); } } return EmailValidator.getInstance().isValidDomain(domainPart); } ListDomainsCheckerImpl(boolean isBlackListDomains, List<String> listDomains); @Override boolean check(String email);  }
@Test public void testCheck_white() { List<String> domainLs = new ArrayList<String>(); domainLs.add("sina.com"); domainLs.add("yahoo.com"); domainLs.add("163.net"); domainCheck = new ListDomainsCheckerImpl(false, domainLs); String email = "ab.c@sina.com"; assertTrue(domainCheck.check(email)); String email2 = "ab.c@yahoo.com"; assertTrue(domainCheck.check(email2)); }
@Test public void testCheck_black() { List<String> domainLs = new ArrayList<String>(); domainLs.add("sina.com"); domainLs.add("yahoo.com"); domainLs.add("163.net"); domainCheck = new ListDomainsCheckerImpl(true, domainLs); String email = "ab.c@sina.com"; assertFalse(domainCheck.check(email)); String email2 = "ab.c@yahoo.com"; assertFalse(domainCheck.check(email2)); String email3 = "ab.c@talend.com"; assertTrue(domainCheck.check(email3)); }
@Test public void testCheck_null() { domainCheck = new ListDomainsCheckerImpl(false, null); String email = "ab.c@sina.com"; assertFalse(domainCheck.check(email)); domainCheck = new ListDomainsCheckerImpl(true, null); String email2 = "ab.c@yahoo.com"; assertTrue(domainCheck.check(email2)); }
@Test public void testCheck_whitelist_with_wildcard() { List<String> domainList = new ArrayList<String>(); domainList.add("univ-*.fr"); domainCheck = new ListDomainsCheckerImpl(false, domainList); String email = "ab.c@univ-paris-6.fr"; assertTrue(domainCheck.check(email)); email = "ab.c@univ-paris-.fr"; assertFalse(domainCheck.check(email)); email = "ab.c@univ-paris-6.sfr"; assertFalse(domainCheck.check(email)); }