MBMessageBBCodeFormatUploadHandler implements MBMessageFormatUploadHandler { @Override public String replaceImageReferences( String content, List<MBAttachmentFileEntryReference> mbAttachmentFileEntryReferences) { for (MBAttachmentFileEntryReference mbAttachmentFileEntryReference : mbAttachmentFileEntryReferences) { Pattern pattern = _getTempImagePattern( mbAttachmentFileEntryReference. getTempMBAttachmentFileEntryId()); Matcher matcher = pattern.matcher(content); content = matcher.replaceAll( _getMBAttachmentFileEntryBBCodeImgTag( 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]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 = _mbMessageBBCodeFormatUploadHandler.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\"]%s[/img]", 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 = _mbMessageBBCodeFormatUploadHandler.replaceImageReferences( originalContent, fileEntryReferences); Assert.assertEquals("[img]" + finalURL + "[/img]", 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\"]%s[/img]", 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]" + finalURL + "[/img]"); } String finalContent = _mbMessageBBCodeFormatUploadHandler.replaceImageReferences( originalContent.toString(), fileEntryReferences); Assert.assertEquals(expectedContent.toString(), finalContent); }