Skip to content

Commit

Permalink
feat: added EmailAdresse parse
Browse files Browse the repository at this point in the history
  • Loading branch information
melistik committed May 23, 2024
1 parent aee48d0 commit 3a05a9e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/main/java/io/rocketbase/mail/dto/EmailAddress.java
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 src/test/java/io/rocketbase/mail/dto/EmailAddressTest.java
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));
}
}
}

0 comments on commit 3a05a9e

Please sign in to comment.