File tree Expand file tree Collapse file tree 2 files changed +31
-4
lines changed
core-module/src/main/java/org/simplejavamail/api/email
simple-java-mail/src/test/java/org/simplejavamail/api/email Expand file tree Collapse file tree 2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change 22
33import lombok .Getter ;
44import lombok .RequiredArgsConstructor ;
5+ import lombok .val ;
56import org .jetbrains .annotations .NotNull ;
67
78import java .util .Arrays ;
@@ -30,10 +31,14 @@ public enum ContentTransferEncoding {
3031 private final String encoder ;
3132
3233 public static ContentTransferEncoding byEncoder (@ NotNull final String encoder ) {
33- return Arrays .stream (values ())
34- .filter (c -> c .encoder .equalsIgnoreCase (encoder ))
35- .findFirst ()
36- .orElseThrow (() -> new IllegalArgumentException ("unknown content transfer encoder: " + encoder ));
34+ try {
35+ return ContentTransferEncoding .valueOf (encoder .replaceAll ("-" , "_" ).toUpperCase ());
36+ } catch (IllegalArgumentException e ) {
37+ return Arrays .stream (values ())
38+ .filter (c -> c .encoder .equalsIgnoreCase (encoder .replaceAll ("_" , "-" )))
39+ .findFirst ()
40+ .orElseThrow (() -> new IllegalArgumentException ("unknown content transfer encoder: " + encoder ));
41+ }
3742 }
3843
3944 @ Override
Original file line number Diff line number Diff line change 1+ package org .simplejavamail .api .email ;
2+
3+ import org .junit .Test ;
4+
5+ import static org .assertj .core .api .Assertions .assertThat ;
6+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
7+
8+ public class ContentTransferEncodingTest {
9+
10+ @ Test
11+ public void byEncoder () {
12+ assertThat (ContentTransferEncoding .byEncoder ("BASE_64" )).isEqualTo (ContentTransferEncoding .BASE_64 );
13+ assertThat (ContentTransferEncoding .byEncoder ("B" )).isEqualTo (ContentTransferEncoding .B );
14+ assertThat (ContentTransferEncoding .byEncoder ("b" )).isEqualTo (ContentTransferEncoding .B );
15+ assertThat (ContentTransferEncoding .byEncoder ("x-uuencode" )).isEqualTo (ContentTransferEncoding .X_UU );
16+ assertThat (ContentTransferEncoding .byEncoder ("x_uuencode" )).isEqualTo (ContentTransferEncoding .X_UU );
17+ assertThat (ContentTransferEncoding .byEncoder ("QUOTED-PRINTABLE" )).isEqualTo (ContentTransferEncoding .QUOTED_PRINTABLE );
18+ assertThatThrownBy (() -> ContentTransferEncoding .byEncoder ("moomoo" ))
19+ .isInstanceOf (IllegalArgumentException .class )
20+ .hasMessage ("unknown content transfer encoder: moomoo" );
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments