From 709fc458b939bc47634f8c9eb625f07968725307 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 24 Jul 2015 07:19:20 +0200 Subject: [PATCH] Add unit test for image link filter --- pom.xml | 6 + .../sone/template/ImageLinkFilterTest.java | 162 +++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java diff --git a/pom.xml b/pom.xml index a76b3b2..13937ce 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,12 @@ 1.3 + org.jsoup + jsoup + 1.7.1 + test + + org.freenetproject fred 0.7.5.1467.99.3 diff --git a/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java b/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java new file mode 100644 index 0000000..20d2362 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java @@ -0,0 +1,162 @@ +package net.pterodactylus.sone.template; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.util.template.HtmlFilter; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateContextFactory; + +import com.google.common.collect.ImmutableMap; +import org.hamcrest.Matchers; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link ImageLinkFilterTest}. + * + * @author David ‘Bombe’ Roden + */ +public class ImageLinkFilterTest { + + private final Core core = mock(Core.class); + private final TemplateContextFactory templateContextFactory = new TemplateContextFactory(); + private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(core, templateContextFactory); + private final TemplateContext templateContext = null; + private final Image image = mock(Image.class); + + @Before + public void setupTemplateContextFactory() { + templateContextFactory.addFilter("html", new HtmlFilter()); + } + + @Before + public void setupCore() { + when(core.getImage("image-id", false)).thenReturn(image); + } + + @Before + public void setupImage() { + when(image.getId()).thenReturn("image-id"); + when(image.getKey()).thenReturn("image-key"); + when(image.isInserted()).thenReturn(true); + when(image.getWidth()).thenReturn(640); + when(image.getHeight()).thenReturn(270); + when(image.getTitle()).thenReturn("image title"); + when(image.getDescription()).thenReturn("image description"); + } + + @Test + public void imageLinkIsGeneratedCorrectlyForNotInsertedImages() { + when(image.isInserted()).thenReturn(false); + String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of())); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("class"), is("")); + assertThat(imageElement.attr("src"), is("getImage.html?image=image-id")); + assertThat(imageElement.attr("title"), is("image title")); + assertThat(imageElement.attr("alt"), is("image description")); + assertThat(imageElement.attr("width"), is("640")); + assertThat(imageElement.attr("height"), is("270")); + } + + @Test + public void imageLinkIsGeneratedCorrectlyForInsertedImages() { + String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of())); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("class"), is("")); + assertThat(imageElement.attr("src"), is("/image-key?forcedownload=true")); + assertThat(imageElement.attr("title"), is("image title")); + assertThat(imageElement.attr("alt"), is("image description")); + assertThat(imageElement.attr("width"), is("640")); + assertThat(imageElement.attr("height"), is("270")); + } + + @Test + public void imageTitleAndDescriptionAreOverriddenCorrectly() { + String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of("title", "Test Title"))); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("title"), is("Test Title")); + assertThat(imageElement.attr("alt"), is("Test Title")); + } + + @Test + public void imageIsScaledByWidthCorrectly() { + String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of("max-width", "320"))); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("width"), is("320")); + assertThat(imageElement.attr("height"), is("135")); + } + + @Test + public void imageIsScaledByHeightCorrectly() { + String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of("max-height", "135"))); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("width"), is("320")); + assertThat(imageElement.attr("height"), is("135")); + } + + @Test + public void wideImageIsEnlargedCorrectly() { + String result = String.valueOf(imageLinkFilter.format(templateContext, image, + ImmutableMap.of("mode", "enlarge", "max-width", "100", "max-height", "100"))); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("width"), is("237")); + assertThat(imageElement.attr("height"), is("100")); + assertThat(imageElement.attr("style"), containsString("left: -68px")); + assertThat(imageElement.attr("style"), containsString("top: 0px")); + } + + @Test + public void highImageIsEnlargedCorrectly() { + when(image.getWidth()).thenReturn(270); + when(image.getHeight()).thenReturn(640); + String result = String.valueOf(imageLinkFilter.format(templateContext, image, + ImmutableMap.of("mode", "enlarge", "max-width", "100", "max-height", "100"))); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("width"), is("100")); + assertThat(imageElement.attr("height"), is("237")); + assertThat(imageElement.attr("style"), containsString("left: 0px")); + assertThat(imageElement.attr("style"), containsString("top: -68px")); + } + + @Test + public void nullImageIsReturnedAsNull() { + assertThat(imageLinkFilter.format(templateContext, null, null), nullValue()); + } + + @Test + public void stringIsUsedToLoadImageFromCore() { + String result = String.valueOf(imageLinkFilter.format(templateContext, "image-id", ImmutableMap.of())); + Element imageElement = getSingleElement(result); + assertThat(imageElement.attr("class"), is("")); + assertThat(imageElement.attr("src"), is("/image-key?forcedownload=true")); + assertThat(imageElement.attr("title"), is("image title")); + assertThat(imageElement.attr("alt"), is("image description")); + assertThat(imageElement.attr("width"), is("640")); + assertThat(imageElement.attr("height"), is("270")); + } + + private Element getSingleElement(String result) { + Document document = Jsoup.parseBodyFragment(result); + assertThatBodyHasASingleElement(document); + return getSingleElement(document); + } + + private void assertThatBodyHasASingleElement(Document document) { + assertThat(document.body().select("> *"), Matchers.hasSize(1)); + } + + private Element getSingleElement(Document document) { + return document.body().select("> *").get(0); + } + +} -- 2.7.4