@Test public void buildCidMap__onMultipartWithEmptyBodyPart__shouldReturnEmptyMap() throws Exception { Multipart multipartBody = MimeMultipart.newInstance(); BodyPart bodyPart = spy(new MimeBodyPart()); Part multipartPart = new MimeBodyPart(multipartBody); multipartBody.addBodyPart(bodyPart); Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart); verify(bodyPart).getContentId(); assertTrue(result.isEmpty()); }
@VisibleForTesting static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor, Part rootPart) { HashMap<String,Uri> result = new HashMap<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(rootPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (Part bodyPart : multipart.getBodyParts()) { partsToCheck.push(bodyPart); } } else { try { String contentId = part.getContentId(); if (contentId != null) { AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part); result.put(contentId, attachmentInfo.internalUri); } } catch (MessagingException e) { Timber.e(e, "Error extracting attachment info"); } } } return Collections.unmodifiableMap(result); }
AttachmentResolver { @VisibleForTesting static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor, Part rootPart) { HashMap<String,Uri> result = new HashMap<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(rootPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (Part bodyPart : multipart.getBodyParts()) { partsToCheck.push(bodyPart); } } else { try { String contentId = part.getContentId(); if (contentId != null) { AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part); result.put(contentId, attachmentInfo.internalUri); } } catch (MessagingException e) { Timber.e(e, "Error extracting attachment info"); } } } return Collections.unmodifiableMap(result); } }
AttachmentResolver { @VisibleForTesting static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor, Part rootPart) { HashMap<String,Uri> result = new HashMap<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(rootPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (Part bodyPart : multipart.getBodyParts()) { partsToCheck.push(bodyPart); } } else { try { String contentId = part.getContentId(); if (contentId != null) { AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part); result.put(contentId, attachmentInfo.internalUri); } } catch (MessagingException e) { Timber.e(e, "Error extracting attachment info"); } } } return Collections.unmodifiableMap(result); } private  AttachmentResolver(Map<String, Uri> contentIdToAttachmentUriMap); }
AttachmentResolver { @VisibleForTesting static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor, Part rootPart) { HashMap<String,Uri> result = new HashMap<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(rootPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (Part bodyPart : multipart.getBodyParts()) { partsToCheck.push(bodyPart); } } else { try { String contentId = part.getContentId(); if (contentId != null) { AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part); result.put(contentId, attachmentInfo.internalUri); } } catch (MessagingException e) { Timber.e(e, "Error extracting attachment info"); } } } return Collections.unmodifiableMap(result); } private  AttachmentResolver(Map<String, Uri> contentIdToAttachmentUriMap); @Nullable Uri getAttachmentUriForContentId(String cid); @WorkerThread static AttachmentResolver createFromPart(Part part); }
AttachmentResolver { @VisibleForTesting static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor, Part rootPart) { HashMap<String,Uri> result = new HashMap<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(rootPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (Part bodyPart : multipart.getBodyParts()) { partsToCheck.push(bodyPart); } } else { try { String contentId = part.getContentId(); if (contentId != null) { AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part); result.put(contentId, attachmentInfo.internalUri); } } catch (MessagingException e) { Timber.e(e, "Error extracting attachment info"); } } } return Collections.unmodifiableMap(result); } private  AttachmentResolver(Map<String, Uri> contentIdToAttachmentUriMap); @Nullable Uri getAttachmentUriForContentId(String cid); @WorkerThread static AttachmentResolver createFromPart(Part part);  }