| 1 | package fr.sii.ogham.spring.email.condition; | |
| 2 | ||
| 3 | import jakarta.activation.DataContentHandler; | |
| 4 | import jakarta.activation.MailcapCommandMap; | |
| 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.ArrayList; | |
| 11 | import java.util.List; | |
| 12 | import java.util.Objects; | |
| 13 | ||
| 14 | import static org.springframework.boot.autoconfigure.condition.ConditionMessage.Style.QUOTE; | |
| 15 | import static org.springframework.boot.autoconfigure.condition.ConditionMessage.forCondition; | |
| 16 | import static org.springframework.boot.autoconfigure.condition.ConditionOutcome.match; | |
| 17 | import static org.springframework.boot.autoconfigure.condition.ConditionOutcome.noMatch; | |
| 18 | ||
| 19 | public class JakartaActivationDataHandlersAvailable extends SpringBootCondition { | |
| 20 | @Override | |
| 21 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { | |
| 22 | try { | |
| 23 | List<DataContentHandler> handlers = checkDataHandlersAvailable(); | |
| 24 |
1
1. getMatchOutcome : replaced return value with null for fr/sii/ogham/spring/email/condition/JakartaActivationDataHandlersAvailable::getMatchOutcome → RUN_ERROR |
return match(forCondition("jakarta.activation.DataHandler implementations available ?") |
| 25 | .found("") | |
| 26 | .items(QUOTE, handlers.stream() | |
| 27 | .filter(Objects::nonNull) | |
| 28 | .map(DataContentHandler::getClass) | |
| 29 | .map(Class::getName) | |
| 30 | .toArray())); | |
| 31 | } catch(Exception | NoClassDefFoundError e) { | |
| 32 |
1
1. getMatchOutcome : replaced return value with null for fr/sii/ogham/spring/email/condition/JakartaActivationDataHandlersAvailable::getMatchOutcome → NO_COVERAGE |
return noMatch(forCondition("jakarta.activation.DataHandler implementations available ?") |
| 33 | .because(e.getMessage())); | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | // can't reuse JavaMailConsistencyChecker since it may not be present at runtime | |
| 38 | private static List<DataContentHandler> checkDataHandlersAvailable() throws JavaDataHandlersLoadFailed { | |
| 39 | try { | |
| 40 | MailcapCommandMap commandMap = new MailcapCommandMap(); | |
| 41 | List<DataContentHandler> found = new ArrayList<>(); | |
| 42 | for (String mimetype : commandMap.getMimeTypes()) { | |
| 43 | DataContentHandler dataContentHandler = commandMap.createDataContentHandler(mimetype); | |
| 44 |
1
1. checkDataHandlersAvailable : negated conditional → RUN_ERROR |
if (dataContentHandler != null) { |
| 45 | found.add(dataContentHandler); | |
| 46 | } | |
| 47 | } | |
| 48 |
1
1. checkDataHandlersAvailable : replaced return value with Collections.emptyList for fr/sii/ogham/spring/email/condition/JakartaActivationDataHandlersAvailable::checkDataHandlersAvailable → RUN_ERROR |
return found; |
| 49 | } catch(Exception | NoClassDefFoundError e) { | |
| 50 | throw new JavaDataHandlersLoadFailed(e); | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | ||
| 55 | public static class JavaDataHandlersLoadFailed extends Exception { | |
| 56 | public JavaDataHandlersLoadFailed(Throwable cause) { | |
| 57 | super("jakarta.activation.DataHandler implementations can't be loaded. Cause: "+cause.getMessage(), cause); | |
| 58 | } | |
| 59 | } | |
| 60 | } | |
Mutations | ||
| 24 |
1.1 |
|
| 32 |
1.1 |
|
| 44 |
1.1 |
|
| 48 |
1.1 |