| 1 | package fr.sii.ogham.email.sender.impl.javamail; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.List; | |
| 5 | ||
| 6 | import javax.mail.BodyPart; | |
| 7 | ||
| 8 | import fr.sii.ogham.core.resource.NamedResource; | |
| 9 | import fr.sii.ogham.email.attachment.Attachment; | |
| 10 | import fr.sii.ogham.email.exception.handler.AttachmentResourceHandlerException; | |
| 11 | import fr.sii.ogham.email.exception.handler.NoAttachmentResourceHandlerException; | |
| 12 | ||
| 13 | /** | |
| 14 | * Provides a handler for the attachment resource based on the class of the | |
| 15 | * attachment resource. The registration order is important. | |
| 16 | * | |
| 17 | * @author Aurélien Baudet | |
| 18 | * | |
| 19 | */ | |
| 20 | public class MapAttachmentResourceHandler implements JavaMailAttachmentResourceHandler { | |
| 21 | /** | |
| 22 | * The mapping of attachment resource handlers indexed by the attachment | |
| 23 | * resource class | |
| 24 | */ | |
| 25 | private final List<Mapping> mappings; | |
| 26 | ||
| 27 | /** | |
| 28 | * Initialize an empty mapping | |
| 29 | */ | |
| 30 | public MapAttachmentResourceHandler() { | |
| 31 | super(); | |
| 32 | this.mappings = new ArrayList<>(); | |
| 33 | } | |
| 34 | ||
| 35 | @Override | |
| 36 | public void setData(BodyPart part, NamedResource resource, Attachment attachment) throws AttachmentResourceHandlerException { | |
| 37 | JavaMailAttachmentResourceHandler attachmentHandler = find(resource.getClass()); | |
| 38 |
3
1. setData : negated conditional → NO_COVERAGE 2. setData : negated conditional → KILLED 3. setData : negated conditional → KILLED |
if (attachmentHandler == null) { |
| 39 | throw new NoAttachmentResourceHandlerException("there is no attachment resource handler defined for managing " + resource.getClass().getSimpleName() + " attachment resource class", | |
| 40 | attachment); | |
| 41 | } | |
| 42 |
3
1. setData : removed call to fr/sii/ogham/email/sender/impl/javamail/JavaMailAttachmentResourceHandler::setData → NO_COVERAGE 2. setData : removed call to fr/sii/ogham/email/sender/impl/javamail/JavaMailAttachmentResourceHandler::setData → KILLED 3. setData : removed call to fr/sii/ogham/email/sender/impl/javamail/JavaMailAttachmentResourceHandler::setData → KILLED |
attachmentHandler.setData(part, resource, attachment); |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Register a new attachment resource handler. The registration order is | |
| 47 | * important. | |
| 48 | * | |
| 49 | * @param clazz | |
| 50 | * the class of the attachment resource | |
| 51 | * @param handler | |
| 52 | * the attachment resource handler | |
| 53 | */ | |
| 54 | public void registerResourceHandler(Class<? extends NamedResource> clazz, JavaMailAttachmentResourceHandler handler) { | |
| 55 | mappings.add(new Mapping(clazz, handler)); | |
| 56 | } | |
| 57 | ||
| 58 | private JavaMailAttachmentResourceHandler find(Class<? extends NamedResource> clazz) { | |
| 59 | for (Mapping mapping : mappings) { | |
| 60 |
3
1. find : negated conditional → NO_COVERAGE 2. find : negated conditional → KILLED 3. find : negated conditional → KILLED |
if (mapping.getClazz().isAssignableFrom(clazz)) { |
| 61 |
3
1. find : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler::find → NO_COVERAGE 2. find : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler::find → KILLED 3. find : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler::find → KILLED |
return mapping.getHandler(); |
| 62 | } | |
| 63 | } | |
| 64 | return null; | |
| 65 | } | |
| 66 | ||
| 67 | private static class Mapping { | |
| 68 | private final Class<? extends NamedResource> clazz; | |
| 69 | private final JavaMailAttachmentResourceHandler handler; | |
| 70 | ||
| 71 | public Mapping(Class<? extends NamedResource> clazz, JavaMailAttachmentResourceHandler handler) { | |
| 72 | super(); | |
| 73 | this.clazz = clazz; | |
| 74 | this.handler = handler; | |
| 75 | } | |
| 76 | ||
| 77 | public Class<? extends NamedResource> getClazz() { | |
| 78 |
3
1. getClazz : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler$Mapping::getClazz → NO_COVERAGE 2. getClazz : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler$Mapping::getClazz → KILLED 3. getClazz : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler$Mapping::getClazz → KILLED |
return clazz; |
| 79 | } | |
| 80 | ||
| 81 | public JavaMailAttachmentResourceHandler getHandler() { | |
| 82 |
3
1. getHandler : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler$Mapping::getHandler → NO_COVERAGE 2. getHandler : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler$Mapping::getHandler → KILLED 3. getHandler : replaced return value with null for fr/sii/ogham/email/sender/impl/javamail/MapAttachmentResourceHandler$Mapping::getHandler → KILLED |
return handler; |
| 83 | } | |
| 84 | } | |
| 85 | } | |
Mutations | ||
| 38 |
1.1 2.2 3.3 |
|
| 42 |
1.1 2.2 3.3 |
|
| 60 |
1.1 2.2 3.3 |
|
| 61 |
1.1 2.2 3.3 |
|
| 78 |
1.1 2.2 3.3 |
|
| 82 |
1.1 2.2 3.3 |