| 1 | package fr.sii.ogham.spring.email.condition; | |
| 2 | ||
| 3 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; | |
| 4 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; | |
| 5 | import org.springframework.context.annotation.ConditionContext; | |
| 6 | import org.springframework.core.type.AnnotatedTypeMetadata; | |
| 7 | ||
| 8 | import javax.mail.Provider; | |
| 9 | import javax.mail.Session; | |
| 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 JavaxMailServiceProvidersAvailable 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/JavaxMailServiceProvidersAvailable::getMatchOutcome → RUN_ERROR |
return match(forCondition("javax.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/JavaxMailServiceProvidersAvailable::getMatchOutcome → NO_COVERAGE |
return noMatch(forCondition("javax.mail.Provider implementations available ?") |
| 33 | .because(e.getMessage())); | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | // can't reuse JavaxMailConsistencyChecker since it may not be present at runtime | |
| 38 | private static List<Provider> checkMailProvidersAvailable() throws JavaxMailProvidersLoadFailed { | |
| 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/JavaxMailServiceProvidersAvailable::checkMailProvidersAvailable → RUN_ERROR |
return asList(providers); |
| 42 | } catch(Exception | NoClassDefFoundError e) { | |
| 43 | throw new JavaxMailProvidersLoadFailed(e); | |
| 44 | } | |
| 45 | } | |
| 46 | ||
| 47 | ||
| 48 | public static class JavaxMailProvidersLoadFailed extends Exception { | |
| 49 | public JavaxMailProvidersLoadFailed(Throwable cause) { | |
| 50 | super("javax.mail.Provider implementations can't be loaded. Cause: "+cause.getMessage(), cause); | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | } | |
Mutations | ||
| 25 |
1.1 |
|
| 32 |
1.1 |
|
| 41 |
1.1 |