HtmlContentTransformerImpl extends BaseRegexStringContentTransformer { @Override public String transform(String html) throws PortalException { if (html == null) { return null; } if (!html.contains(AMImageHTMLConstants.ATTRIBUTE_NAME_FILE_ENTRY_ID)) { return html; } return super.transform(html); }  @Override ContentTransformerContentType<String> getContentTransformerContentType(); @Override String transform(String html);  }
@Test public void testAlsoReplacesSeveralImagesInAMultilineString() throws Exception { Mockito.when( _amImageHTMLTagFactory.create( "<img data-fileentryid=\"1989\" src=\"adaptable\"/>", _fileEntry) ).thenReturn( "<whatever></whatever>" ); StringBundler expectedSB = new StringBundler(3); expectedSB.append("<div><div>"); expectedSB.append("<whatever></whatever>"); expectedSB.append("</div></div><br/>"); StringBundler originalSB = new StringBundler(4); originalSB.append("<div><div>"); originalSB.append("<img data-fileentryid=\"1989\" "); originalSB.append("src=\"adaptable\"/>"); originalSB.append("</div></div><br/>"); Assert.assertEquals( _duplicateWithNewLine(expectedSB.toString()), _htmlContentTransformerImpl.transform( _duplicateWithNewLine(originalSB.toString()))); }
@Test public void testReplacesAnAdaptableImgAfterANonadaptableOne() throws Exception { Mockito.when( _amImageHTMLTagFactory.create( "<img data-fileentryid=\"1989\" src=\"adaptable\"/>", _fileEntry) ).thenReturn( "<whatever></whatever>" ); Assert.assertEquals( "<img src=\"not-adaptable\"/><whatever></whatever>", _htmlContentTransformerImpl.transform( "<img src=\"not-adaptable\"/>" + "<img data-fileentryid=\"1989\" src=\"adaptable\"/>")); }
@Test public void testReplacesTheAdaptableImagesWithTheAdaptiveTag() throws Exception { Mockito.when( _amImageHTMLTagFactory.create( "<img data-fileentryid=\"1989\" src=\"adaptable\"/>", _fileEntry) ).thenReturn( "<whatever></whatever>" ); Assert.assertEquals( "<whatever></whatever>", _htmlContentTransformerImpl.transform( "<img data-fileentryid=\"1989\" src=\"adaptable\"/>")); }
@Test public void testReplacesTwoConsecutiveImageTags() throws Exception { Mockito.when( _amImageHTMLTagFactory.create( "<img data-fileentryid=\"1989\" src=\"adaptable\"/>", _fileEntry) ).thenReturn( "<whatever></whatever>" ); Assert.assertEquals( "<whatever></whatever><whatever></whatever>", _htmlContentTransformerImpl.transform( "<img data-fileentryid=\"1989\" src=\"adaptable\"/>" + "<img data-fileentryid=\"1989\" src=\"adaptable\"/>")); }
@Test public void testReturnsNullForNullContent() throws Exception { Assert.assertNull(_htmlContentTransformerImpl.transform(null)); }
@Test public void testReturnsTheSameHTMLIfNoImagesArePresent() throws Exception { Assert.assertEquals( "<div><div>some <a>stuff</a></div></div>", _htmlContentTransformerImpl.transform( "<div><div>some <a>stuff</a></div></div>")); }
@Test public void testReturnsTheSameHTMLIfThereAreNoAdaptableImagesPresent() throws Exception { Assert.assertEquals( "<div><div><img src=\"no.adaptable\"/></div></div>", _htmlContentTransformerImpl.transform( "<div><div><img src=\"no.adaptable\"/></div></div>")); }
@Test public void testSupportsImageTagsWithNewLineCharacters() throws Exception { Mockito.when( _amImageHTMLTagFactory.create( "<img data-fileentryid=\"1989\" \nsrc=\"adaptable\"/>", _fileEntry) ).thenReturn( "<whatever></whatever>" ); StringBundler originalSB = new StringBundler(3); originalSB.append("<img data-fileentryid=\"1989\" "); originalSB.append(CharPool.NEW_LINE); originalSB.append("src=\"adaptable\"/>"); Assert.assertEquals( "<whatever></whatever>", _htmlContentTransformerImpl.transform(originalSB.toString())); }
@Test public void testTheAttributeIsCaseInsensitive() throws Exception { Mockito.when( _amImageHTMLTagFactory.create( "<img data-fileentryid=\"1989\" src=\"adaptable\"/>", _fileEntry) ).thenReturn( "<whatever></whatever>" ); StringBundler expectedSB = new StringBundler(1); expectedSB.append("<div><div><whatever></whatever></div></div><br/>"); StringBundler originalSB = new StringBundler(4); originalSB.append("<div><div>"); originalSB.append("<img data-fileentryid=\"1989\" "); originalSB.append("src=\"adaptable\"/>"); originalSB.append("</div></div><br/>"); Assert.assertEquals( expectedSB.toString(), _htmlContentTransformerImpl.transform( StringUtil.toLowerCase(originalSB.toString()))); }