-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,52 @@ | ||
package io.rocketbase.mail.dto; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@EqualsAndHashCode(of = {"email", "name"}) | ||
@Getter | ||
@RequiredArgsConstructor | ||
public class EmailAddress { | ||
|
||
private final String email; | ||
|
||
private final String name; | ||
|
||
private static final Pattern NAME_EMAIL_PATTERN = Pattern.compile("\"([^\"]*)\"\\s*<([^>]*)>"); | ||
private static final Pattern EMAIL_PATTERN = Pattern.compile("<([^>]*)>"); | ||
|
||
public EmailAddress(String email) { | ||
this.email = email; | ||
this.name = null; | ||
} | ||
|
||
public static EmailAddress parse(String email) { | ||
Matcher emailNameMatcher = NAME_EMAIL_PATTERN.matcher(email); | ||
if (emailNameMatcher.matches()) { | ||
return new EmailAddress(emailNameMatcher.group(2),emailNameMatcher.group(1) ); | ||
} | ||
Matcher emailMatcher = EMAIL_PATTERN.matcher(email); | ||
if (emailMatcher.matches()) { | ||
return new EmailAddress(emailMatcher.group(1) ); | ||
} | ||
throw new IllegalArgumentException("Invalid email address: " + email); | ||
} | ||
|
||
public String toRecipient() { | ||
if (!StringUtils.isEmpty(name)) { | ||
return "\"%s\" <%s>".formatted(name, email); | ||
} else { | ||
return "<%s>".formatted(email); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return toRecipient(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/io/rocketbase/mail/dto/EmailAddressTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.rocketbase.mail.dto; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class EmailAddressTest { | ||
|
||
@Test | ||
void parseSimple() { | ||
EmailAddress email = EmailAddress.parse("<[email protected]>"); | ||
assertThat(email, equalTo(new EmailAddress("[email protected]"))); | ||
} | ||
|
||
@Test | ||
void parseCombined() { | ||
EmailAddress whiteSpace = EmailAddress.parse("\"Marten Prieß\" <[email protected]>"); | ||
EmailAddress without = EmailAddress.parse("\"Marten Prieß\"<[email protected]>"); | ||
EmailAddress compared = new EmailAddress("[email protected]", "Marten Prieß"); | ||
assertThat(whiteSpace, equalTo(compared)); | ||
assertThat(without, equalTo(compared)); | ||
} | ||
|
||
@Test() | ||
void checkFailure() { | ||
try { | ||
EmailAddress error = EmailAddress.parse("\"Marten Prieß\" <[email protected]"); | ||
} catch (Exception e) { | ||
assertThat(e, instanceOf(IllegalArgumentException.class)); | ||
} | ||
} | ||
} |