|
22 | 22 | import static org.assertj.core.api.Assertions.assertThat; |
23 | 23 | import static org.assertj.core.api.Assertions.assertThatCode; |
24 | 24 |
|
| 25 | +import java.util.Properties; |
25 | 26 | import java.util.stream.Stream; |
26 | 27 |
|
| 28 | +import jakarta.mail.Session; |
27 | 29 | import jakarta.mail.internet.AddressException; |
28 | 30 | import jakarta.mail.internet.InternetAddress; |
29 | 31 |
|
30 | 32 | import org.assertj.core.api.Assertions; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
31 | 34 | import org.junit.jupiter.api.Test; |
32 | 35 | import org.junit.jupiter.params.ParameterizedTest; |
33 | 36 | import org.junit.jupiter.params.provider.Arguments; |
@@ -55,6 +58,13 @@ private static Stream<Arguments> goodAddresses() { |
55 | 58 | "\\.server-dev@james.apache.org", |
56 | 59 | "Abc@10.42.0.1", |
57 | 60 | "Abc.123@example.com", |
| 61 | + "Loïc.Accentué@voilà.fr8", |
| 62 | + "pelé@exemple.com", |
| 63 | + "δοκιμή@παράδειγμα.δοκιμή", |
| 64 | + "我買@屋企.香港", |
| 65 | + "二ノ宮@黒川.日本", |
| 66 | + "медведь@с-балалайкой.рф", |
| 67 | + //"संपर्क@डाटामेल.भारत", fails in Jakarta, reason still unknown |
58 | 68 | "user+mailbox/department=shipping@example.com", |
59 | 69 | "user+mailbox@example.com", |
60 | 70 | "\"Abc@def\"@example.com", |
@@ -96,40 +106,46 @@ private static Stream<Arguments> badAddresses() { |
96 | 106 | "server-dev@[127.0.1.1.1]", |
97 | 107 | "server-dev@[127.0.1.-1]", |
98 | 108 | "test@dom+ain.com", |
| 109 | + "test@xn--.example", |
99 | 110 | "\"a..b\"@domain.com", // jakarta.mail is unable to handle this so we better reject it |
100 | 111 | "server-dev\\.@james.apache.org", // jakarta.mail is unable to handle this so we better reject it |
101 | 112 | "a..b@domain.com", |
102 | | - // According to wikipedia these addresses are valid but as jakarta.mail is unable |
103 | | - // to work with them we shall rather reject them (note that this is not breaking retro-compatibility) |
104 | | - "Loïc.Accentué@voilà.fr8", |
105 | | - "pelé@exemple.com", |
106 | | - "δοκιμή@παράδειγμα.δοκιμή", |
107 | | - "我買@屋企.香港", |
108 | | - "二ノ宮@黒川.日本", |
109 | | - "медведь@с-балалайкой.рф", |
110 | | - "संपर्क@डाटामेल.भारत", |
| 113 | + "sales@\u200Eibm.example", // U+200E is left-to-right |
| 114 | + // According to wikipedia this address is valid but as jakarta.mail is unable |
| 115 | + // to work with it we shall rather reject them (note that this is not breaking retro-compatibility) |
111 | 116 | "mail.allow\\,d@james.apache.org") |
112 | 117 | .map(Arguments::of); |
113 | 118 | } |
114 | 119 |
|
| 120 | + @BeforeEach |
| 121 | + void setup() { |
| 122 | + Properties props = new Properties(); |
| 123 | + props.setProperty("mail.mime.allowutf8", "true"); |
| 124 | + Session s = Session.getDefaultInstance(props); |
| 125 | + assertThat(Boolean.parseBoolean(s.getProperties().getProperty("mail.mime.allowutf8", "false"))); |
| 126 | + } |
| 127 | + |
115 | 128 | @ParameterizedTest |
116 | 129 | @MethodSource("goodAddresses") |
117 | 130 | void testGoodMailAddressString(String mailAddress) { |
118 | 131 | assertThatCode(() -> new MailAddress(mailAddress)) |
| 132 | + .as("parses " + mailAddress) |
119 | 133 | .doesNotThrowAnyException(); |
120 | 134 | } |
121 | 135 |
|
122 | 136 | @ParameterizedTest |
123 | 137 | @MethodSource("goodAddresses") |
124 | 138 | void toInternetAddressShouldNoop(String mailAddress) throws Exception { |
125 | 139 | assertThat(new MailAddress(mailAddress).toInternetAddress()) |
| 140 | + .as("tries to parse " + mailAddress + " using jakarta.mail") |
126 | 141 | .isNotEmpty(); |
127 | 142 | } |
128 | 143 |
|
129 | 144 | @ParameterizedTest |
130 | 145 | @MethodSource("badAddresses") |
131 | 146 | void testBadMailAddressString(String mailAddress) { |
132 | 147 | Assertions.assertThatThrownBy(() -> new MailAddress(mailAddress)) |
| 148 | + .as("fails to parse " + mailAddress) |
133 | 149 | .isInstanceOf(AddressException.class); |
134 | 150 | } |
135 | 151 |
|
|
0 commit comments