MBMessageHTMLFormatUploadHandler implements MBMessageFormatUploadHandler { @Override public String replaceImageReferences( String content, List<MBAttachmentFileEntryReference> mbAttachmentFileEntryReferences) { for (MBAttachmentFileEntryReference mbAttachmentFileEntryReference : mbAttachmentFileEntryReferences) { StringBundler sb = new StringBundler(8); sb.append("<\\s*?img"); sb.append(_ATTRIBUTE_LIST_REGEXP); sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID); sb.append("\\s*?=\\s*?\""); sb.append( mbAttachmentFileEntryReference. getTempMBAttachmentFileEntryId()); sb.append("\""); sb.append(_ATTRIBUTE_LIST_REGEXP); sb.append("/>"); content = content.replaceAll( sb.toString(), _getMBAttachmentFileEntryHTMLImgTag( mbAttachmentFileEntryReference.getMBAttachmentFileEntry())); } return content; }  @Override String replaceImageReferences(
		String content,
		List<MBAttachmentFileEntryReference> mbAttachmentFileEntryReferences);  }
@Test public void testDoesNotReplaceImageReferencesWithoutDataImageIdAttribute() { List<MBAttachmentFileEntryReference> fileEntryReferences = new ArrayList<>(); FileEntry fileEntry = Mockito.mock(FileEntry.class); long tempFileId = 1; String originalContent = "<img src=\"http: String finalURL = "http: Mockito.doReturn( finalURL ).when( _portletFileRepository ).getPortletFileEntryURL( Mockito.isNull(ThemeDisplay.class), Mockito.eq(fileEntry), Mockito.eq(StringPool.BLANK) ); fileEntryReferences.add( new MBAttachmentFileEntryReference(tempFileId, fileEntry)); String finalContent = _mbMessageHTMLFormatUploadHandler.replaceImageReferences( originalContent, fileEntryReferences); Assert.assertEquals(originalContent, finalContent); }
@Test public void testReplaceASingleImageReference() { List<MBAttachmentFileEntryReference> fileEntryReferences = new ArrayList<>(); FileEntry fileEntry = Mockito.mock(FileEntry.class); long tempFileId = 1; String originalContent = String.format( "<img data-image-id=\"%d\" src=\"%s\"/>", tempFileId, "http: String finalURL = "http: Mockito.doReturn( finalURL ).when( _portletFileRepository ).getPortletFileEntryURL( Mockito.isNull(ThemeDisplay.class), Mockito.eq(fileEntry), Mockito.eq(StringPool.BLANK) ); fileEntryReferences.add( new MBAttachmentFileEntryReference(tempFileId, fileEntry)); String finalContent = _mbMessageHTMLFormatUploadHandler.replaceImageReferences( originalContent, fileEntryReferences); Assert.assertEquals("<img src=\"" + finalURL + "\" />", finalContent); }
@Test public void testReplaceSeveralImageReferences() { List<MBAttachmentFileEntryReference> fileEntryReferences = new ArrayList<>(); StringBuilder originalContent = new StringBuilder(); StringBuilder expectedContent = new StringBuilder(); for (int tempFileId = 0; tempFileId < 3; tempFileId++) { FileEntry fileEntry = Mockito.mock(FileEntry.class); String finalURL = "http: String curOriginalContent = String.format( "<img data-image-id=\"%d\" src=\"%s\"/>", tempFileId, "http: Mockito.doReturn( finalURL ).when( _portletFileRepository ).getPortletFileEntryURL( Mockito.isNull(ThemeDisplay.class), Mockito.eq(fileEntry), Mockito.eq(StringPool.BLANK) ); fileEntryReferences.add( new MBAttachmentFileEntryReference(tempFileId, fileEntry)); originalContent.append(curOriginalContent); expectedContent.append("<img src=\"" + finalURL + "\" />"); } String finalContent = _mbMessageHTMLFormatUploadHandler.replaceImageReferences( originalContent.toString(), fileEntryReferences); Assert.assertEquals(expectedContent.toString(), finalContent); }