Skip to content

Commit 9d8593a

Browse files
committed
1 parent 1f46858 commit 9d8593a

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

src/main/java/org/simplejavamail/internal/util/MiscUtil.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ public static String readInputStreamToString(@Nonnull final InputStream inputStr
8787
}
8888
return byteArrayOutputStream.toString(checkNonEmptyArgument(charset, "charset").name());
8989
}
90-
90+
91+
/**
92+
* Recognizes the tails of each address entry, so it can replace the [';] delimiters, thereby disambiguating the delimiters, since they can
93+
* appear in names as well (making it difficult to split on [,;] delimiters.
94+
*
95+
* @param emailAddressList The delimited list of addresses (or single address) optionally including the name.
96+
* @return Array of address entries optionally including the names, trimmed for spaces or trailing delimiters.
97+
*/
9198
@Nonnull
9299
public static String[] extractEmailAddresses(@Nonnull final String emailAddressList) {
93-
//noinspection DynamicRegexReplaceableByCompiledPattern
100+
// recognize value tails and replace the delimiters there, disambiguating delimiters
94101
return checkNonEmptyArgument(emailAddressList, "emailAddressList")
95-
.replace(';', ',')
96-
.replaceAll("\\s*,\\s*", ",")
97-
.split(",");
102+
.replaceAll("(@.*?>?)\\s*[,;]", "$1<|>")
103+
.replaceAll("<\\|>$", "") // remove trailing delimiter
104+
.split("\\s*<\\|>\\s*"); // split on delimiter including surround space
98105
}
99106
}

src/test/java/org/simplejavamail/internal/util/MiscUtilTest.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,94 @@ public void checkNotNull() {
1111
assertThat(MiscUtil.checkNotNull("blah", null)).isEqualTo("blah");
1212
assertThat(MiscUtil.checkNotNull(23523, null)).isEqualTo(23523);
1313
}
14-
14+
1515
@Test(expected = NullPointerException.class)
1616
public void checkNotNullWithException() {
1717
MiscUtil.checkNotNull(null, null);
1818
}
19-
19+
2020
@Test
2121
public void checkArgumentNotEmpty() {
2222
assertThat(MiscUtil.checkArgumentNotEmpty("blah", null)).isEqualTo("blah");
2323
assertThat(MiscUtil.checkArgumentNotEmpty(234, null)).isEqualTo(234);
2424
}
25-
25+
2626
@Test(expected = IllegalArgumentException.class)
2727
public void checkArgumentNotEmptyWithEmptyString() {
2828
MiscUtil.checkArgumentNotEmpty("", null);
2929
}
30-
30+
3131
@Test(expected = IllegalArgumentException.class)
3232
public void checkArgumentNotEmptyWithNullString() {
3333
MiscUtil.checkArgumentNotEmpty(null, null);
3434
}
35-
35+
3636
@Test
3737
public void valueNullOrEmpty() {
3838
assertThat(MiscUtil.valueNullOrEmpty("")).isEqualTo(true);
3939
assertThat(MiscUtil.valueNullOrEmpty(null)).isEqualTo(true);
4040
assertThat(MiscUtil.valueNullOrEmpty("blah")).isEqualTo(false);
4141
assertThat(MiscUtil.valueNullOrEmpty(2534)).isEqualTo(false);
4242
}
43-
43+
4444
@Test(expected = IllegalArgumentException.class)
4545
public void testExtractEmailAddresses_MissingAddress() {
4646
MiscUtil.extractEmailAddresses(null);
4747
}
48-
48+
4949
@Test(expected = IllegalArgumentException.class)
5050
public void testExtractEmailAddresses_EmptyAddress() {
5151
MiscUtil.extractEmailAddresses("");
5252
}
53-
53+
5454
@Test
5555
public void testExtractEmailAddresses_SingleAddress() {
5656
String[] singleAddressList = MiscUtil.extractEmailAddresses("[email protected]");
5757
assertThat(singleAddressList).hasSize(1);
5858
assertThat(singleAddressList).contains("[email protected]");
5959
}
60-
60+
6161
@Test
6262
public void testExtractEmailAddresses_MultipleAddressesWithCommas() {
6363
String[] singleAddressList = MiscUtil.extractEmailAddresses("[email protected],[email protected],[email protected]");
6464
assertThat(singleAddressList).hasSize(3);
6565
assertThat(singleAddressList).contains("[email protected]", "[email protected]", "[email protected]");
6666
}
67-
67+
6868
@Test
6969
public void testExtractEmailAddresses_MultipleAddressesWithSemicolons() {
7070
String[] singleAddressList = MiscUtil.extractEmailAddresses("[email protected];[email protected];[email protected]");
7171
assertThat(singleAddressList).hasSize(3);
7272
assertThat(singleAddressList).contains("[email protected]", "[email protected]", "[email protected]");
7373
}
74-
74+
7575
@Test
7676
public void testExtractEmailAddresses_MultipleAddressesMixedCommasAndSemicolons() {
7777
String[] singleAddressList = MiscUtil.extractEmailAddresses("[email protected],[email protected];[email protected];[email protected],[email protected]");
7878
assertThat(singleAddressList).hasSize(5);
7979
assertThat(singleAddressList).contains("[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]");
8080
}
81-
81+
8282
@Test
8383
public void testExtractEmailAddresses_MultipleAddressesTralingSpaces() {
8484
String[] singleAddressList = MiscUtil.extractEmailAddresses("[email protected], [email protected] ;[email protected];[email protected] , [email protected],[email protected]");
8585
assertThat(singleAddressList).hasSize(6);
8686
8787
}
88+
89+
@Test
90+
public void testExtractEmailAddresses() {
91+
String testInput = "[email protected],Sixpack, \"Joe 1\" <[email protected]>, Sixpack, Joe 2 <[email protected]> ;Sixpack, Joe, 3<name@domain" +
92+
93+
assertThat(MiscUtil.extractEmailAddresses(testInput)).containsExactlyInAnyOrder(
94+
95+
"Sixpack, \"Joe 1\" <[email protected]>",
96+
"Sixpack, Joe 2 <[email protected]>",
97+
"Sixpack, Joe, 3<[email protected]>",
98+
99+
100+
101+
"\" Joe Sixpack 4 \" <[email protected]>"
102+
);
103+
}
88104
}

0 commit comments

Comments
 (0)