| 1 | package fr.sii.ogham.testing.assertion.util; | |
| 2 | ||
| 3 | import java.io.ByteArrayOutputStream; | |
| 4 | import java.io.IOException; | |
| 5 | import java.io.InputStream; | |
| 6 | import java.nio.charset.Charset; | |
| 7 | import java.util.ArrayList; | |
| 8 | import java.util.List; | |
| 9 | import java.util.function.Predicate; | |
| 10 | ||
| 11 | import javax.mail.BodyPart; | |
| 12 | import javax.mail.Message; | |
| 13 | import javax.mail.MessagingException; | |
| 14 | import javax.mail.Multipart; | |
| 15 | import javax.mail.Part; | |
| 16 | import javax.mail.internet.MimeMessage; | |
| 17 | ||
| 18 | import org.apache.commons.io.IOUtils; | |
| 19 | import org.slf4j.Logger; | |
| 20 | import org.slf4j.LoggerFactory; | |
| 21 | ||
| 22 | import fr.sii.ogham.testing.assertion.filter.DefaultAttachmentPredicate; | |
| 23 | import fr.sii.ogham.testing.assertion.filter.FileNamePredicate; | |
| 24 | ||
| 25 | public final class EmailUtils { | |
| 26 | private static final Logger LOG = LoggerFactory.getLogger(EmailUtils.class); | |
| 27 | public static final String ATTACHMENT_DISPOSITION = "attachment"; | |
| 28 | public static final String INLINE_DISPOSITION = "inline"; | |
| 29 | | |
| 30 | /** | |
| 31 | * Retrieve the body parts of the message (recursively): | |
| 32 | * <ul> | |
| 33 | * <li>The part of the message when it contains only one part</li> | |
| 34 | * <li>The parts with text/* mimetype</li> | |
| 35 | * </ul> | |
| 36 | * | |
| 37 | * @param actualEmail | |
| 38 | * the message | |
| 39 | * @return the body of the message | |
| 40 | * @throws MessagingException | |
| 41 | * when message can't be read | |
| 42 | */ | |
| 43 | public static List<Part> getBodyParts(Part actualEmail) throws MessagingException { | |
| 44 |
5
1. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → NO_COVERAGE 2. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → TIMED_OUT 3. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → KILLED 4. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → KILLED 5. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → KILLED |
return getTextualParts(actualEmail); |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Retrieve the main part of the message (recursively): | |
| 49 | * <ul> | |
| 50 | * <li>The part of the message when it contains only one part</li> | |
| 51 | * <li>The part with text/* mimetype if only one part with one of that | |
| 52 | * mimetype</li> | |
| 53 | * <li>The last part with text or HTML mimetype if there are several text/* | |
| 54 | * parts</li> | |
| 55 | * </ul> | |
| 56 | * | |
| 57 | * @param actualEmail | |
| 58 | * the message | |
| 59 | * @return the body of the message | |
| 60 | * @throws MessagingException | |
| 61 | * when message can't be read | |
| 62 | */ | |
| 63 | public static Part getBodyPart(Part actualEmail) throws MessagingException { | |
| 64 |
5
1. getBodyPart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyPart → NO_COVERAGE 2. getBodyPart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyPart → TIMED_OUT 3. getBodyPart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyPart → KILLED 4. getBodyPart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyPart → KILLED 5. getBodyPart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyPart → KILLED |
return getBestAlternative(getBodyParts(actualEmail)); |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Retrieve the alternative part of the message (recursively). The | |
| 69 | * alternative is useful when sending HTML email that may be unreadable on | |
| 70 | * some email clients. For example, a smartphone will display the 2 or 3 | |
| 71 | * first lines as a summary. Many smartphones will take the HTML message | |
| 72 | * as-is and will display HTML tags instead of content of email. Alternative | |
| 73 | * is used to provide a textual visualization of the message that will be | |
| 74 | * readable by any system. | |
| 75 | * | |
| 76 | * <p> | |
| 77 | * The alternative of the message is either: | |
| 78 | * <ul> | |
| 79 | * <li>null if there is only one part</li> | |
| 80 | * <li>null if there is only one text or HTML part</li> | |
| 81 | * <li>the first part if there are more than one text or HTML part</li> | |
| 82 | * </ul> | |
| 83 | * | |
| 84 | * @param actualEmail | |
| 85 | * the message | |
| 86 | * @return the alternative part of the message if exists, null otherwise | |
| 87 | * @throws MessagingException | |
| 88 | * when message can't be read | |
| 89 | */ | |
| 90 | public static Part getAlternativePart(Part actualEmail) throws MessagingException { | |
| 91 | List<Part> bodyParts = getTextualParts(actualEmail); | |
| 92 |
8
1. getAlternativePart : changed conditional boundary → NO_COVERAGE 2. getAlternativePart : changed conditional boundary → SURVIVED 3. getAlternativePart : negated conditional → NO_COVERAGE 4. getAlternativePart : changed conditional boundary → KILLED 5. getAlternativePart : changed conditional boundary → KILLED 6. getAlternativePart : negated conditional → KILLED 7. getAlternativePart : negated conditional → KILLED 8. getAlternativePart : negated conditional → KILLED |
if (bodyParts.size() < 2) { |
| 93 | return null; | |
| 94 | } | |
| 95 |
3
1. getAlternativePart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getAlternativePart → NO_COVERAGE 2. getAlternativePart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getAlternativePart → KILLED 3. getAlternativePart : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getAlternativePart → KILLED |
return bodyParts.get(0); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Get the whole list of "textual" parts (text or HTML mimetypes). | |
| 100 | * | |
| 101 | * @param actualEmail | |
| 102 | * the message | |
| 103 | * @return the list of "textual" parts | |
| 104 | * @throws MessagingException | |
| 105 | * when message can't be read | |
| 106 | */ | |
| 107 | public static List<Part> getTextualParts(Part actualEmail) throws MessagingException { | |
| 108 |
5
1. getTextualParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getTextualParts → NO_COVERAGE 2. getTextualParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getTextualParts → TIMED_OUT 3. getTextualParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getTextualParts → KILLED 4. getTextualParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getTextualParts → KILLED 5. getTextualParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getTextualParts → KILLED |
return getBodyParts(actualEmail, EmailUtils::isTextualContent); |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Get the content as byte array of a particular part. | |
| 113 | * | |
| 114 | * <p> | |
| 115 | * If the part is null or the content stream is null, then | |
| 116 | * null is returned. | |
| 117 | * | |
| 118 | * @param part | |
| 119 | * the part | |
| 120 | * @return the content as byte array or null | |
| 121 | * @throws IOException | |
| 122 | * when part can't be read | |
| 123 | * @throws MessagingException | |
| 124 | * when message can't be read | |
| 125 | */ | |
| 126 | @SuppressWarnings("squid:S1168") // return null on purpose (to be able to distinguish empty content from no content at all in tests) | |
| 127 | public static byte[] getContent(Part part) throws IOException, MessagingException { | |
| 128 |
5
1. getContent : negated conditional → NO_COVERAGE 2. getContent : negated conditional → TIMED_OUT 3. getContent : negated conditional → KILLED 4. getContent : negated conditional → KILLED 5. getContent : negated conditional → KILLED |
if (part == null) { |
| 129 | return null; | |
| 130 | } | |
| 131 | InputStream stream = part.getInputStream(); | |
| 132 |
5
1. getContent : negated conditional → NO_COVERAGE 2. getContent : negated conditional → TIMED_OUT 3. getContent : negated conditional → KILLED 4. getContent : negated conditional → KILLED 5. getContent : negated conditional → KILLED |
if (stream == null) { |
| 133 | return null; | |
| 134 | } | |
| 135 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| 136 | IOUtils.copy(stream, baos); | |
| 137 |
5
1. getContent : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → NO_COVERAGE 2. getContent : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → TIMED_OUT 3. getContent : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED 4. getContent : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED 5. getContent : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED |
return baos.toByteArray(); |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Get the content as {@link String} of a particular part. | |
| 142 | * | |
| 143 | * <p> | |
| 144 | * <strong>NOTE: This method handles a special case due to how Java Mail | |
| 145 | * sends textual content (adds CRLF)</strong> If the content is a textual | |
| 146 | * content ("text/*" mimetype) and the part has no parent and it ends with | |
| 147 | * CRLF, remove the last CRLF. | |
| 148 | * | |
| 149 | * <strong>If your original text had the CRLF, this method can't know that | |
| 150 | * it was already part of the original text because Java Mail only adds CRLF | |
| 151 | * if there not already has CRLF at the end of the text.</strong> | |
| 152 | * | |
| 153 | * <p> | |
| 154 | * If the part is null or the content stream is null, then | |
| 155 | * null is returned. | |
| 156 | * | |
| 157 | * @param part | |
| 158 | * the part | |
| 159 | * @param charset | |
| 160 | * the charset used to decode the part content | |
| 161 | * @return the part content | |
| 162 | * @throws MessagingException | |
| 163 | * when the part can't be accessed | |
| 164 | * @throws IOException | |
| 165 | * when the part content can't be read | |
| 166 | */ | |
| 167 | public static String getContent(Part part, Charset charset) throws IOException, MessagingException { | |
| 168 | byte[] bytes = getContent(part); | |
| 169 |
5
1. getContent : negated conditional → NO_COVERAGE 2. getContent : negated conditional → TIMED_OUT 3. getContent : negated conditional → KILLED 4. getContent : negated conditional → KILLED 5. getContent : negated conditional → KILLED |
if (bytes == null) { |
| 170 |
1
1. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → NO_COVERAGE |
return null; |
| 171 | } | |
| 172 | String content = IOUtils.toString(bytes, charset.name()); | |
| 173 |
15
1. getContent : negated conditional → NO_COVERAGE 2. getContent : negated conditional → SURVIVED 3. getContent : negated conditional → NO_COVERAGE 4. getContent : negated conditional → SURVIVED 5. getContent : negated conditional → NO_COVERAGE 6. getContent : negated conditional → TIMED_OUT 7. getContent : negated conditional → TIMED_OUT 8. getContent : negated conditional → TIMED_OUT 9. getContent : negated conditional → KILLED 10. getContent : negated conditional → KILLED 11. getContent : negated conditional → KILLED 12. getContent : negated conditional → KILLED 13. getContent : negated conditional → KILLED 14. getContent : negated conditional → KILLED 15. getContent : negated conditional → KILLED |
if (isTextualContent(part) && !hasParent(part) && content.endsWith("\r\n")) { |
| 174 |
8
1. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → NO_COVERAGE 2. getContent : Replaced integer subtraction with addition → NO_COVERAGE 3. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → TIMED_OUT 4. getContent : Replaced integer subtraction with addition → TIMED_OUT 5. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED 6. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED 7. getContent : Replaced integer subtraction with addition → KILLED 8. getContent : Replaced integer subtraction with addition → KILLED |
return content.substring(0, content.length() - 2); |
| 175 | } | |
| 176 |
4
1. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → NO_COVERAGE 2. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED 3. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED 4. getContent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getContent → KILLED |
return content; |
| 177 | } | |
| 178 | ||
| 179 | /** | |
| 180 | * Get a particular attachment (found using exact filename field). | |
| 181 | * | |
| 182 | * @param multipart | |
| 183 | * the email that contains several parts | |
| 184 | * @param filename | |
| 185 | * the name of the attachment to find | |
| 186 | * @return the found attachment or null | |
| 187 | * @throws MessagingException | |
| 188 | * when message can't be read | |
| 189 | */ | |
| 190 | public static BodyPart getAttachment(Multipart multipart, final String filename) throws MessagingException { | |
| 191 |
2
1. getAttachment : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachment → NO_COVERAGE 2. getAttachment : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachment → KILLED |
return getAttachment(multipart, new FileNamePredicate(filename)); |
| 192 | } | |
| 193 | ||
| 194 | /** | |
| 195 | * Get a particular attachment (found using provided predicate). If several | |
| 196 | * attachments match the predicate, only the first one is retrieved. | |
| 197 | * | |
| 198 | * @param multipart | |
| 199 | * the email that contains several parts | |
| 200 | * @param filter | |
| 201 | * the predicate used to find the attachment | |
| 202 | * @return the found attachment or null | |
| 203 | * @throws MessagingException | |
| 204 | * when message can't be read | |
| 205 | */ | |
| 206 | public static BodyPart getAttachment(Multipart multipart, Predicate<Part> filter) throws MessagingException { | |
| 207 | List<BodyPart> attachments = getAttachments(multipart, filter); | |
| 208 |
4
1. getAttachment : negated conditional → NO_COVERAGE 2. getAttachment : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachment → NO_COVERAGE 3. getAttachment : negated conditional → KILLED 4. getAttachment : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachment → KILLED |
return attachments.isEmpty() ? null : attachments.get(0); |
| 209 | } | |
| 210 | ||
| 211 | /** | |
| 212 | * Get a list of direct attachments that match the provided predicate. | |
| 213 | * | |
| 214 | * @param multipart | |
| 215 | * the email that contains several parts | |
| 216 | * @param filter | |
| 217 | * the predicate used to find the attachments | |
| 218 | * @return the found attachments or empty list | |
| 219 | * @throws MessagingException | |
| 220 | * when message can't be read | |
| 221 | */ | |
| 222 | public static List<BodyPart> getAttachments(Multipart multipart, Predicate<Part> filter) throws MessagingException { | |
| 223 |
2
1. getAttachments : negated conditional → NO_COVERAGE 2. getAttachments : negated conditional → KILLED |
if (multipart == null) { |
| 224 | throw new IllegalStateException("The multipart can't be null"); | |
| 225 | } | |
| 226 | List<BodyPart> found = new ArrayList<>(); | |
| 227 |
4
1. getAttachments : changed conditional boundary → NO_COVERAGE 2. getAttachments : negated conditional → NO_COVERAGE 3. getAttachments : changed conditional boundary → KILLED 4. getAttachments : negated conditional → KILLED |
for (int i = 0; i < multipart.getCount(); i++) { |
| 228 | BodyPart bodyPart = multipart.getBodyPart(i); | |
| 229 |
2
1. getAttachments : negated conditional → NO_COVERAGE 2. getAttachments : negated conditional → KILLED |
if (filter.test(bodyPart)) { |
| 230 | found.add(bodyPart); | |
| 231 | } | |
| 232 | } | |
| 233 |
2
1. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → NO_COVERAGE 2. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → KILLED |
return found; |
| 234 | } | |
| 235 | ||
| 236 | /** | |
| 237 | * Get the whole list of attachments (recursively). | |
| 238 | * | |
| 239 | * @param message | |
| 240 | * the email that contains several parts | |
| 241 | * @return the found attachments or empty list | |
| 242 | * @throws MessagingException | |
| 243 | * when message can't be read | |
| 244 | */ | |
| 245 | public static List<BodyPart> getAttachments(Part message) throws MessagingException { | |
| 246 |
8
1. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → NO_COVERAGE 2. lambda$getAttachments$0 : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/EmailUtils::lambda$getAttachments$0 → NO_COVERAGE 3. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → KILLED 4. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → KILLED 5. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → KILLED 6. lambda$getAttachments$0 : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/EmailUtils::lambda$getAttachments$0 → KILLED 7. lambda$getAttachments$0 : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/EmailUtils::lambda$getAttachments$0 → KILLED 8. lambda$getAttachments$0 : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/EmailUtils::lambda$getAttachments$0 → KILLED |
return getAttachments(message, p -> true); |
| 247 | } | |
| 248 | ||
| 249 | /** | |
| 250 | * Get the whole list of attachments. | |
| 251 | * | |
| 252 | * <p> | |
| 253 | * <strong>WARNING:</strong> As there is no way to ensure that a part is an | |
| 254 | * attachment, every part is testing against the filter (event multipart | |
| 255 | * containers). So the filter that you provide is combined with | |
| 256 | * {@link DefaultAttachmentPredicate}. This way, only the list of parts that | |
| 257 | * may be potential attachments (downloadable or embeddable) are provided to | |
| 258 | * your filter. | |
| 259 | * | |
| 260 | * @param message | |
| 261 | * the email that contains several parts | |
| 262 | * @param filter | |
| 263 | * filter the parts to keep only some attachments. If filter | |
| 264 | * returns true then the part is added to the list. | |
| 265 | * @param <T> | |
| 266 | * type of part | |
| 267 | * @return the found attachments or empty list | |
| 268 | * @throws MessagingException | |
| 269 | * when message can't be read | |
| 270 | */ | |
| 271 | public static <T extends Part> List<T> getAttachments(Part message, Predicate<Part> filter) throws MessagingException { | |
| 272 | List<T> attachments = new ArrayList<>(); | |
| 273 |
4
1. getAttachments : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → NO_COVERAGE 2. getAttachments : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 3. getAttachments : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 4. getAttachments : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED |
findBodyParts(message, new DefaultAttachmentPredicate().and(filter), attachments); |
| 274 |
4
1. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → NO_COVERAGE 2. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → KILLED 3. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → KILLED 4. getAttachments : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getAttachments → KILLED |
return attachments; |
| 275 | } | |
| 276 | ||
| 277 | /** | |
| 278 | * Indicates if a part is a multipart (its Content-Type starts with | |
| 279 | * "multipart/"). | |
| 280 | * | |
| 281 | * @param part | |
| 282 | * the part to check | |
| 283 | * @return true if a multipart | |
| 284 | */ | |
| 285 | public static boolean isMultipart(Part part) { | |
| 286 | try { | |
| 287 |
6
1. isMultipart : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/EmailUtils::isMultipart → SURVIVED 2. isMultipart : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/EmailUtils::isMultipart → NO_COVERAGE 3. isMultipart : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isMultipart → NO_COVERAGE 4. isMultipart : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isMultipart → KILLED 5. isMultipart : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isMultipart → KILLED 6. isMultipart : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isMultipart → KILLED |
return part.isMimeType("multipart/*"); |
| 288 | } catch (MessagingException e) { | |
| 289 | throw new MessagingRuntimeException("Failed to retrieve Content-Type of part", e); | |
| 290 | } | |
| 291 | } | |
| 292 | ||
| 293 | /** | |
| 294 | * Indicates if the part contains text (either plain text or html). It is | |
| 295 | * checked using Content-Type (either "text/*", "application/html" or | |
| 296 | * "application/xhtml"). | |
| 297 | * | |
| 298 | * @param part | |
| 299 | * the part to check | |
| 300 | * @return true if it is text | |
| 301 | */ | |
| 302 | public static boolean isTextualContent(Part part) { | |
| 303 | try { | |
| 304 |
18
1. isTextualContent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isTextualContent → NO_COVERAGE 2. isTextualContent : negated conditional → NO_COVERAGE 3. isTextualContent : negated conditional → NO_COVERAGE 4. isTextualContent : negated conditional → NO_COVERAGE 5. isTextualContent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isTextualContent → TIMED_OUT 6. isTextualContent : negated conditional → TIMED_OUT 7. isTextualContent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isTextualContent → KILLED 8. isTextualContent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isTextualContent → KILLED 9. isTextualContent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::isTextualContent → KILLED 10. isTextualContent : negated conditional → KILLED 11. isTextualContent : negated conditional → KILLED 12. isTextualContent : negated conditional → KILLED 13. isTextualContent : negated conditional → KILLED 14. isTextualContent : negated conditional → KILLED 15. isTextualContent : negated conditional → KILLED 16. isTextualContent : negated conditional → KILLED 17. isTextualContent : negated conditional → KILLED 18. isTextualContent : negated conditional → KILLED |
return part.isMimeType("text/*") || part.isMimeType("application/html") || part.isMimeType("application/xhtml"); |
| 305 | } catch (MessagingException e) { | |
| 306 | throw new MessagingRuntimeException("Failed to retrieve Content-Type of part", e); | |
| 307 | } | |
| 308 | } | |
| 309 | ||
| 310 | /** | |
| 311 | * Get the whole structure of the email. This is mainly used for debugging | |
| 312 | * purpose. | |
| 313 | * | |
| 314 | * @param mimeMessage | |
| 315 | * the email | |
| 316 | * @return the structure of the email | |
| 317 | * @throws IOException | |
| 318 | * when email can't be read | |
| 319 | * @throws MessagingException | |
| 320 | * when email can't be read | |
| 321 | */ | |
| 322 | public static String getStructure(MimeMessage mimeMessage) throws IOException, MessagingException { | |
| 323 | StringBuilder structure = new StringBuilder(); | |
| 324 |
2
1. getStructure : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → NO_COVERAGE 2. getStructure : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → KILLED |
findParts(mimeMessage, structure, ""); |
| 325 |
2
1. getStructure : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getStructure → NO_COVERAGE 2. getStructure : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getStructure → KILLED |
return structure.toString(); |
| 326 | } | |
| 327 | ||
| 328 | /** | |
| 329 | * Get the partial structure of the email from the provided container. This | |
| 330 | * is mainly used for debugging purpose. | |
| 331 | * | |
| 332 | * @param multipart | |
| 333 | * the container | |
| 334 | * @return the structure of the email | |
| 335 | * @throws IOException | |
| 336 | * when email can't be read | |
| 337 | * @throws MessagingException | |
| 338 | * when email can't be read | |
| 339 | */ | |
| 340 | public static String getStructure(Multipart multipart) throws IOException, MessagingException { | |
| 341 | StringBuilder structure = new StringBuilder(); | |
| 342 |
1
1. getStructure : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → NO_COVERAGE |
findParts(multipart, structure, ""); |
| 343 |
1
1. getStructure : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getStructure → NO_COVERAGE |
return structure.toString(); |
| 344 | } | |
| 345 | ||
| 346 | /** | |
| 347 | * Get the partial structure of the email from the provided part. This is | |
| 348 | * mainly used for debugging purpose. | |
| 349 | * | |
| 350 | * @param part | |
| 351 | * the part | |
| 352 | * @return the structure of the email | |
| 353 | * @throws IOException | |
| 354 | * when email can't be read | |
| 355 | * @throws MessagingException | |
| 356 | * when email can't be read | |
| 357 | */ | |
| 358 | public static String getStructure(BodyPart part) throws IOException, MessagingException { | |
| 359 | StringBuilder structure = new StringBuilder(); | |
| 360 |
1
1. getStructure : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → NO_COVERAGE |
findParts(part, structure, ""); |
| 361 |
1
1. getStructure : replaced return value with "" for fr/sii/ogham/testing/assertion/util/EmailUtils::getStructure → NO_COVERAGE |
return structure.toString(); |
| 362 | } | |
| 363 | ||
| 364 | private static <T extends Part> List<T> getBodyParts(Part actualEmail, Predicate<Part> filter) throws MessagingException { | |
| 365 | List<T> founds = new ArrayList<>(); | |
| 366 |
5
1. getBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → NO_COVERAGE 2. getBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → TIMED_OUT 3. getBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 4. getBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 5. getBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED |
findBodyParts(actualEmail, filter, founds); |
| 367 |
5
1. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → NO_COVERAGE 2. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → TIMED_OUT 3. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → KILLED 4. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → KILLED 5. getBodyParts : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/EmailUtils::getBodyParts → KILLED |
return founds; |
| 368 | } | |
| 369 | ||
| 370 | private static <T extends Part> void findBodyParts(Part actualEmail, Predicate<Part> filter, List<T> founds) throws MessagingException { | |
| 371 | LOG.trace("---------------------------"); | |
| 372 |
5
1. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → NO_COVERAGE 2. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → TIMED_OUT 3. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 4. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 5. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED |
findBodyParts(actualEmail, filter, founds, ""); |
| 373 | } | |
| 374 | ||
| 375 | private static <T extends Part> void findBodyParts(Part part, Predicate<Part> filter, List<T> founds, String indent) throws MessagingException { | |
| 376 | try { | |
| 377 |
5
1. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::addPart → NO_COVERAGE 2. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::addPart → TIMED_OUT 3. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::addPart → KILLED 4. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::addPart → KILLED 5. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::addPart → KILLED |
addPart(filter, founds, indent, part); |
| 378 | Object content = part.getContent(); | |
| 379 |
5
1. findBodyParts : negated conditional → NO_COVERAGE 2. findBodyParts : negated conditional → TIMED_OUT 3. findBodyParts : negated conditional → KILLED 4. findBodyParts : negated conditional → KILLED 5. findBodyParts : negated conditional → KILLED |
if (content instanceof Multipart) { |
| 380 | Multipart mp = (Multipart) content; | |
| 381 | LOG.trace("{}find {}", indent, mp.getContentType()); | |
| 382 |
8
1. findBodyParts : changed conditional boundary → NO_COVERAGE 2. findBodyParts : negated conditional → NO_COVERAGE 3. findBodyParts : changed conditional boundary → KILLED 4. findBodyParts : changed conditional boundary → KILLED 5. findBodyParts : changed conditional boundary → KILLED 6. findBodyParts : negated conditional → KILLED 7. findBodyParts : negated conditional → KILLED 8. findBodyParts : negated conditional → KILLED |
for (int i = 0; i < mp.getCount(); i++) { |
| 383 |
4
1. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → NO_COVERAGE 2. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 3. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED 4. findBodyParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findBodyParts → KILLED |
findBodyParts(mp.getBodyPart(i), filter, founds, indent + " "); |
| 384 | } | |
| 385 | } | |
| 386 | } catch (IOException e) { | |
| 387 | throw new MessagingException("Failed to access content of the mail", e); | |
| 388 | } | |
| 389 | } | |
| 390 | ||
| 391 | @SuppressWarnings("unchecked") | |
| 392 | private static <T extends Part> void addPart(Predicate<Part> filter, List<T> founds, String indent, Part part) throws MessagingException { | |
| 393 |
5
1. addPart : negated conditional → NO_COVERAGE 2. addPart : negated conditional → TIMED_OUT 3. addPart : negated conditional → KILLED 4. addPart : negated conditional → KILLED 5. addPart : negated conditional → KILLED |
if (filter.test(part)) { |
| 394 | LOG.trace("{}{}add {}", indent, " ", part.getContentType()); | |
| 395 | founds.add((T) part); | |
| 396 | } | |
| 397 | } | |
| 398 | ||
| 399 | private static void findParts(Part part, StringBuilder structure, String indent) throws IOException, MessagingException { | |
| 400 |
2
1. findParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::addPart → NO_COVERAGE 2. findParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::addPart → KILLED |
addPart(part, structure, indent); |
| 401 | Object content = part.getContent(); | |
| 402 |
2
1. findParts : negated conditional → NO_COVERAGE 2. findParts : negated conditional → KILLED |
if (content instanceof Multipart) { |
| 403 |
2
1. findParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → NO_COVERAGE 2. findParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → KILLED |
findParts((Multipart) content, structure, indent); |
| 404 | } | |
| 405 | } | |
| 406 | ||
| 407 | private static void findParts(Multipart mp, StringBuilder structure, String indent) throws MessagingException, IOException { | |
| 408 |
4
1. findParts : changed conditional boundary → NO_COVERAGE 2. findParts : negated conditional → NO_COVERAGE 3. findParts : changed conditional boundary → KILLED 4. findParts : negated conditional → KILLED |
for (int i = 0; i < mp.getCount(); i++) { |
| 409 | BodyPart subpart = mp.getBodyPart(i); | |
| 410 |
2
1. findParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → NO_COVERAGE 2. findParts : removed call to fr/sii/ogham/testing/assertion/util/EmailUtils::findParts → KILLED |
findParts(subpart, structure, indent + " "); |
| 411 | } | |
| 412 | } | |
| 413 | ||
| 414 | private static void addPart(Part part, StringBuilder structure, String indent) throws MessagingException { | |
| 415 | structure.append(indent).append("[").append(part.getDataHandler().getContentType().split(";")[0]).append("]\n"); | |
| 416 | } | |
| 417 | ||
| 418 | /** | |
| 419 | * According to | |
| 420 | * <a href="https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html">rfc1341 | |
| 421 | * §7.2.3 The Multipart/alternative subtype</a>, the best alternative is the | |
| 422 | * last that can be displayed. | |
| 423 | * | |
| 424 | * @param bodyParts | |
| 425 | * the possible body parts | |
| 426 | * @return | |
| 427 | */ | |
| 428 | private static Part getBestAlternative(List<Part> bodyParts) { | |
| 429 |
5
1. getBestAlternative : negated conditional → NO_COVERAGE 2. getBestAlternative : negated conditional → TIMED_OUT 3. getBestAlternative : negated conditional → KILLED 4. getBestAlternative : negated conditional → KILLED 5. getBestAlternative : negated conditional → KILLED |
if (bodyParts.isEmpty()) { |
| 430 | return null; | |
| 431 | } | |
| 432 |
10
1. getBestAlternative : Replaced integer subtraction with addition → NO_COVERAGE 2. getBestAlternative : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBestAlternative → NO_COVERAGE 3. getBestAlternative : Replaced integer subtraction with addition → TIMED_OUT 4. getBestAlternative : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBestAlternative → TIMED_OUT 5. getBestAlternative : Replaced integer subtraction with addition → KILLED 6. getBestAlternative : Replaced integer subtraction with addition → KILLED 7. getBestAlternative : Replaced integer subtraction with addition → KILLED 8. getBestAlternative : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBestAlternative → KILLED 9. getBestAlternative : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBestAlternative → KILLED 10. getBestAlternative : replaced return value with null for fr/sii/ogham/testing/assertion/util/EmailUtils::getBestAlternative → KILLED |
return bodyParts.get(bodyParts.size() - 1); |
| 433 | } | |
| 434 | ||
| 435 | private static boolean hasParent(Part part) { | |
| 436 |
3
1. hasParent : negated conditional → NO_COVERAGE 2. hasParent : negated conditional → SURVIVED 3. hasParent : negated conditional → TIMED_OUT |
if (part instanceof Message) { |
| 437 |
4
1. hasParent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::hasParent → NO_COVERAGE 2. hasParent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::hasParent → TIMED_OUT 3. hasParent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::hasParent → KILLED 4. hasParent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::hasParent → KILLED |
return false; |
| 438 | } | |
| 439 |
2
1. hasParent : negated conditional → SURVIVED 2. hasParent : negated conditional → NO_COVERAGE |
if (part instanceof BodyPart) { |
| 440 |
4
1. hasParent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::hasParent → SURVIVED 2. hasParent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::hasParent → NO_COVERAGE 3. hasParent : negated conditional → NO_COVERAGE 4. hasParent : negated conditional → SURVIVED |
return ((BodyPart) part).getParent() != null; |
| 441 | } | |
| 442 |
1
1. hasParent : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/EmailUtils::hasParent → NO_COVERAGE |
return false; |
| 443 | } | |
| 444 | ||
| 445 | private EmailUtils() { | |
| 446 | super(); | |
| 447 | } | |
| 448 | ||
| 449 | private static class MessagingRuntimeException extends RuntimeException { | |
| 450 | private static final long serialVersionUID = 1L; | |
| 451 | ||
| 452 | public MessagingRuntimeException(String message, Throwable cause) { | |
| 453 | super(message, cause); | |
| 454 | } | |
| 455 | } | |
| 456 | } | |
Mutations | ||
| 44 |
1.1 2.2 3.3 4.4 5.5 |
|
| 64 |
1.1 2.2 3.3 4.4 5.5 |
|
| 92 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 95 |
1.1 2.2 3.3 |
|
| 108 |
1.1 2.2 3.3 4.4 5.5 |
|
| 128 |
1.1 2.2 3.3 4.4 5.5 |
|
| 132 |
1.1 2.2 3.3 4.4 5.5 |
|
| 137 |
1.1 2.2 3.3 4.4 5.5 |
|
| 169 |
1.1 2.2 3.3 4.4 5.5 |
|
| 170 |
1.1 |
|
| 173 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 11.11 12.12 13.13 14.14 15.15 |
|
| 174 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 176 |
1.1 2.2 3.3 4.4 |
|
| 191 |
1.1 2.2 |
|
| 208 |
1.1 2.2 3.3 4.4 |
|
| 223 |
1.1 2.2 |
|
| 227 |
1.1 2.2 3.3 4.4 |
|
| 229 |
1.1 2.2 |
|
| 233 |
1.1 2.2 |
|
| 246 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 273 |
1.1 2.2 3.3 4.4 |
|
| 274 |
1.1 2.2 3.3 4.4 |
|
| 287 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 304 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 11.11 12.12 13.13 14.14 15.15 16.16 17.17 18.18 |
|
| 324 |
1.1 2.2 |
|
| 325 |
1.1 2.2 |
|
| 342 |
1.1 |
|
| 343 |
1.1 |
|
| 360 |
1.1 |
|
| 361 |
1.1 |
|
| 366 |
1.1 2.2 3.3 4.4 5.5 |
|
| 367 |
1.1 2.2 3.3 4.4 5.5 |
|
| 372 |
1.1 2.2 3.3 4.4 5.5 |
|
| 377 |
1.1 2.2 3.3 4.4 5.5 |
|
| 379 |
1.1 2.2 3.3 4.4 5.5 |
|
| 382 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 383 |
1.1 2.2 3.3 4.4 |
|
| 393 |
1.1 2.2 3.3 4.4 5.5 |
|
| 400 |
1.1 2.2 |
|
| 402 |
1.1 2.2 |
|
| 403 |
1.1 2.2 |
|
| 408 |
1.1 2.2 3.3 4.4 |
|
| 410 |
1.1 2.2 |
|
| 429 |
1.1 2.2 3.3 4.4 5.5 |
|
| 432 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 |
|
| 436 |
1.1 2.2 3.3 |
|
| 437 |
1.1 2.2 3.3 4.4 |
|
| 439 |
1.1 2.2 |
|
| 440 |
1.1 2.2 3.3 4.4 |
|
| 442 |
1.1 |