EmailValidator { public void validateUserMail(String userMail) throws BadRequestException { if (userMail == null || userMail.isEmpty()) { throw new BadRequestException("User mail can't be null or ''"); } userMail = userMail.toLowerCase(); if (EMAIL_ILLEGAL_CHARACTERS_PATTERN.matcher(userMail).find()) { throw new BadRequestException( "User mail must not contain characters like '+','/'or consecutive periods"); } try { InternetAddress address = new InternetAddress(userMail); address.validate(); } catch (AddressException e) { throw new BadRequestException( "E-Mail validation failed. Please check the format of your e-mail address."); } try { readBlacklistFile(); } catch (FileNotFoundException e) { LOG.warn("Email blacklist is not found or is a directory: {}", e); } catch (IOException e) { LOG.error(e.getLocalizedMessage(), e); } rw.readLock().lock(); try { if (blacklist.contains(userMail)) { throw new BadRequestException(String.format("User mail %s is forbidden", userMail)); } if (isGmailAddress(userMail) && blacklistGmail.contains(getGmailNormalizedLocalPart(userMail))) { throw new BadRequestException(String.format("User mail %s is forbidden", userMail)); } for (String blacklistedPartialEmail : blacklistPartial) { if (userMail.endsWith(blacklistedPartialEmail)) { throw new BadRequestException(String.format("User mail %s is forbidden", userMail)); } } for (Pattern blackListRegexp : blacklistRegexp) { if (blackListRegexp.matcher(userMail).find()) { throw new BadRequestException(String.format("User mail %s is forbidden", userMail)); } } } finally { rw.readLock().unlock(); } } @Inject  EmailValidator(@Nullable @Named(EMAIL_BLACKLIST_FILE) String emailBlacklistFile); void validateUserMail(String userMail);  }
@Test(dataProvider = "validEmails") public void shouldValidateEmail(String emails) throws Exception { emailValidator.validateUserMail(emails); }
@Test( dataProvider = "invalidEmails", expectedExceptions = BadRequestException.class, expectedExceptionsMessageRegExp = "User mail must not contain characters like '\\+','\\/'or consecutive periods" ) public void shouldInvalidateEmailWithIllegalCharacters(String emails) throws Exception { emailValidator.validateUserMail(emails); }
@Test( dataProvider = "blackListedEmails", expectedExceptions = BadRequestException.class, expectedExceptionsMessageRegExp = "User mail .* is forbidden" ) public void shouldInvalidateBlackListedEmail(String emails) throws Exception { emailValidator.validateUserMail(emails); }