Skip to content

Commit cb2635c

Browse files
committed
Add new ifValid and ifValidOrElse methods
1 parent a986bd7 commit cb2635c

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# JMail Changelog
22

3+
## 1.6.3
4+
5+
- Fix bug where email addresses containing control characters in the local-part were incorrectly considered valid. (Thanks @PascalSchumacher for reporting!)
6+
- Add new methods `ifValid(Consumer<Email> action)` and `ifValidOrElse(Consumer<Email> action, Consumer<FailureReason> failureAction)` to the `EmailValidationResult` object.
7+
8+
---
39
## 1.6.2
410

511
- Fix bug where IPv4 addresses with non-arabic numerals would incorrectly be considered valid. (Thanks @harrel56 for reporting!)

src/main/java/com/sanctionco/jmail/EmailValidationResult.java

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Objects;
44
import java.util.Optional;
55
import java.util.StringJoiner;
6+
import java.util.function.Consumer;
67

78
/**
89
* Contains information about an email address validation, including success or failure,
@@ -64,6 +65,33 @@ public FailureReason getFailureReason() {
6465
return failureReason;
6566
}
6667

68+
/**
69+
* If the email address is valid, performs the given action with the valid email address,
70+
* otherwise does nothing.
71+
*
72+
* @param action the action to be performed, if the email address is valid
73+
*/
74+
public void ifValid(Consumer<Email> action) {
75+
if (emailAddress != null) {
76+
action.accept(emailAddress);
77+
}
78+
}
79+
80+
/**
81+
* If the email address is valid, performs the given action with the valid email address,
82+
* otherwise performs the given failure action with the failure reason.
83+
*
84+
* @param action the action to be performed, if the email address is valid
85+
* @param failureAction the action to be performed, if the email address is invalid
86+
*/
87+
public void ifValidOrElse(Consumer<Email> action, Consumer<FailureReason> failureAction) {
88+
if (emailAddress != null) {
89+
action.accept(emailAddress);
90+
} else {
91+
failureAction.accept(failureReason);
92+
}
93+
}
94+
6795
@Override
6896
public String toString() {
6997
return new StringJoiner(", ", EmailValidationResult.class.getSimpleName() + "[", "]")

src/test/java/com/sanctionco/jmail/EmailValidationResultTest.java

+68
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.sanctionco.jmail;
22

33
import java.util.Optional;
4+
import java.util.concurrent.atomic.AtomicBoolean;
5+
import java.util.function.Consumer;
46

57
import nl.jqno.equalsverifier.EqualsVerifier;
68

@@ -36,4 +38,70 @@ void testStaticFailureConstructor() {
3638
.hasToString("EmailValidationResult"
3739
+ "[success=false, emailAddress=null, failureReason=NULL_ADDRESS]");
3840
}
41+
42+
@Test
43+
void testIfValidTrue() {
44+
Optional<Email> testEmail = Email.of("[email protected]");
45+
EmailValidationResult result = EmailValidationResult.success(testEmail.get());
46+
47+
AtomicBoolean completedAction = new AtomicBoolean(false);
48+
49+
Consumer<Email> action = email -> {
50+
completedAction.set(true);
51+
assertThat(email).isEqualTo(Email.of("[email protected]").get());
52+
};
53+
54+
result.ifValid(action);
55+
assertThat(completedAction).isTrue();
56+
}
57+
58+
@Test
59+
void testIfValidFalse() {
60+
EmailValidationResult result = EmailValidationResult.failure(FailureReason.NULL_ADDRESS);
61+
62+
AtomicBoolean completedAction = new AtomicBoolean(false);
63+
64+
Consumer<Email> action = email -> completedAction.set(true);
65+
66+
result.ifValid(action);
67+
assertThat(completedAction).isFalse();
68+
}
69+
70+
@Test
71+
void testIfValidOrElseTrue() {
72+
Optional<Email> testEmail = Email.of("[email protected]");
73+
EmailValidationResult result = EmailValidationResult.success(testEmail.get());
74+
75+
AtomicBoolean completedAction = new AtomicBoolean(false);
76+
AtomicBoolean completedFailureAction = new AtomicBoolean(false);
77+
78+
Consumer<Email> action = email -> {
79+
completedAction.set(true);
80+
assertThat(email).isEqualTo(Email.of("[email protected]").get());
81+
};
82+
83+
Consumer<FailureReason> failureAction = failureReason -> completedFailureAction.set(true);
84+
85+
result.ifValidOrElse(action, failureAction);
86+
assertThat(completedAction).isTrue();
87+
assertThat(completedFailureAction).isFalse();
88+
}
89+
90+
@Test
91+
void testIfValidOrElseFalse() {
92+
EmailValidationResult result = EmailValidationResult.failure(FailureReason.NULL_ADDRESS);
93+
94+
AtomicBoolean completedAction = new AtomicBoolean(false);
95+
AtomicBoolean completedFailureAction = new AtomicBoolean(false);
96+
97+
Consumer<Email> action = email -> completedAction.set(true);
98+
Consumer<FailureReason> failureAction = failureReason -> {
99+
completedFailureAction.set(true);
100+
assertThat(failureReason).isEqualTo(FailureReason.NULL_ADDRESS);
101+
};
102+
103+
result.ifValidOrElse(action, failureAction);
104+
assertThat(completedAction).isFalse();
105+
assertThat(completedFailureAction).isTrue();
106+
}
39107
}

0 commit comments

Comments
 (0)