| 1 | package fr.sii.ogham.spring.email.condition; | |
| 2 | ||
| 3 | import jakarta.mail.Provider; | |
| 4 | import jakarta.mail.Session; | |
| 5 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; | |
| 6 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; | |
| 7 | import org.springframework.context.annotation.ConditionContext; | |
| 8 | import org.springframework.core.type.AnnotatedTypeMetadata; | |
| 9 | ||
| 10 | import java.util.List; | |
| 11 | import java.util.Objects; | |
| 12 | import java.util.Properties; | |
| 13 | ||
| 14 | import static java.util.Arrays.asList; | |
| 15 | import static org.springframework.boot.autoconfigure.condition.ConditionMessage.Style.QUOTE; | |
| 16 | import static org.springframework.boot.autoconfigure.condition.ConditionMessage.forCondition; | |
| 17 | import static org.springframework.boot.autoconfigure.condition.ConditionOutcome.match; | |
| 18 | import static org.springframework.boot.autoconfigure.condition.ConditionOutcome.noMatch; | |
| 19 | ||
| 20 | public class JakartaMailServiceProvidersAvailable extends SpringBootCondition { | |
| 21 | @Override | |
| 22 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { | |
| 23 | try { | |
| 24 | List<Provider> providers = checkMailProvidersAvailable(); | |
| 25 |
1
1. getMatchOutcome : replaced return value with null for fr/sii/ogham/spring/email/condition/JakartaMailServiceProvidersAvailable::getMatchOutcome → RUN_ERROR |
return match(forCondition("jakarta.mail.Provider implementations available ?") |
| 26 | .found("") | |
| 27 | .items(QUOTE, providers.stream() | |
| 28 | .filter(Objects::nonNull) | |
| 29 | .map(Provider::getClassName) | |
| 30 | .toArray())); | |
| 31 | } catch(Exception | NoClassDefFoundError e) { | |
| 32 |
1
1. getMatchOutcome : replaced return value with null for fr/sii/ogham/spring/email/condition/JakartaMailServiceProvidersAvailable::getMatchOutcome → NO_COVERAGE |
return noMatch(forCondition("jakarta.mail.Provider implementations available ?") |
| 33 | .because(e.getMessage())); | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | // can't reuse JavaMailConsistencyChecker since it may not be present at runtime | |
| 38 | public static List<Provider> checkMailProvidersAvailable() throws JavaMailProvidersLoadFailed { | |
| 39 | try { | |
| 40 | Provider[] providers = Session.getInstance(new Properties()).getProviders(); | |
| 41 |
1
1. checkMailProvidersAvailable : replaced return value with Collections.emptyList for fr/sii/ogham/spring/email/condition/JakartaMailServiceProvidersAvailable::checkMailProvidersAvailable → RUN_ERROR |
return asList(providers); |
| 42 | } catch(Exception | NoClassDefFoundError e) { | |
| 43 | throw new JavaMailProvidersLoadFailed(e); | |
| 44 | } | |
| 45 | } | |
| 46 | ||
| 47 | public static class JavaMailProvidersLoadFailed extends Exception { | |
| 48 | public JavaMailProvidersLoadFailed(Throwable cause) { | |
| 49 | super("jakarta.mail.Provider implementations can't be loaded. Cause: "+cause.getMessage(), cause); | |
| 50 | } | |
| 51 | } | |
| 52 | } | |
Mutations | ||
| 25 |
1.1 |
|
| 32 |
1.1 |
|
| 41 |
1.1 |