From: David ‘Bombe’ Roden Date: Sat, 9 Mar 2019 22:42:19 +0000 (+0100) Subject: ♻️ Create custom context factory in image link filter X-Git-Tag: v79^2~74 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6940f7d3df4f6164bfc69a9393a0f5796627b880;p=Sone.git ♻️ Create custom context factory in image link filter --- diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java index d230f24..fc4b803 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -28,6 +28,7 @@ import java.util.Map; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Image; import net.pterodactylus.util.template.Filter; +import net.pterodactylus.util.template.HtmlFilter; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateContextFactory; @@ -49,19 +50,12 @@ public class ImageLinkFilter implements Filter { private final Core core; /** The template context factory. */ - private final TemplateContextFactory templateContextFactory; + private final TemplateContextFactory templateContextFactory = new TemplateContextFactory(); - /** - * Creates a new image link filter. - * - * @param core - * The core - * @param templateContextFactory - * The template context factory - */ - public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) { + public ImageLinkFilter(Core core) { this.core = core; - this.templateContextFactory = templateContextFactory; + templateContextFactory.addFilter("html", new HtmlFilter()); + templateContextFactory.addFilter("css", new CssClassNameFilter()); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index fb17e93..0b25ccd 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -354,7 +354,7 @@ public class WebInterface implements SessionProvider { templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate")); templateContextFactory.addFilter("format", new FormatFilter()); templateContextFactory.addFilter("sort", new CollectionSortFilter()); - templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory)); + templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore())); templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter()); templateContextFactory.addFilter("in", new ContainsFilter()); templateContextFactory.addFilter("unique", new UniqueElementFilter()); diff --git a/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java b/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java index 7cc88b1..5d95423 100644 --- a/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java +++ b/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java @@ -9,9 +9,6 @@ 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; @@ -27,17 +24,10 @@ import org.junit.Test; 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 ImageLinkFilter imageLinkFilter = new ImageLinkFilter(core); 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); } @@ -56,7 +46,7 @@ public class ImageLinkFilterTest { @Test public void imageLinkIsGeneratedCorrectlyForNotInsertedImages() { when(image.isInserted()).thenReturn(false); - String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of())); + String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.of())); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("class"), is("")); assertThat(imageElement.attr("src"), is("getImage.html?image=image-id")); @@ -68,7 +58,7 @@ public class ImageLinkFilterTest { @Test public void imageLinkIsGeneratedCorrectlyForInsertedImages() { - String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of())); + String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.of())); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("class"), is("")); assertThat(imageElement.attr("src"), is("/image-key")); @@ -80,7 +70,7 @@ public class ImageLinkFilterTest { @Test public void imageTitleAndDescriptionAreOverriddenCorrectly() { - String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of("title", "Test Title"))); + String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.of("title", "Test Title"))); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("title"), is("Test Title")); assertThat(imageElement.attr("alt"), is("Test Title")); @@ -88,7 +78,7 @@ public class ImageLinkFilterTest { @Test public void imageIsScaledByWidthCorrectly() { - String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of("max-width", "320"))); + String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.of("max-width", "320"))); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("width"), is("320")); assertThat(imageElement.attr("height"), is("135")); @@ -96,7 +86,7 @@ public class ImageLinkFilterTest { @Test public void imageIsScaledByHeightCorrectly() { - String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.of("max-height", "135"))); + String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.of("max-height", "135"))); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("width"), is("320")); assertThat(imageElement.attr("height"), is("135")); @@ -104,7 +94,7 @@ public class ImageLinkFilterTest { @Test public void wideImageIsEnlargedCorrectly() { - String result = String.valueOf(imageLinkFilter.format(templateContext, image, + String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.of("mode", "enlarge", "max-width", "100", "max-height", "100"))); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("width"), is("237")); @@ -117,7 +107,7 @@ public class ImageLinkFilterTest { public void highImageIsEnlargedCorrectly() { when(image.getWidth()).thenReturn(270); when(image.getHeight()).thenReturn(640); - String result = String.valueOf(imageLinkFilter.format(templateContext, image, + String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.of("mode", "enlarge", "max-width", "100", "max-height", "100"))); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("width"), is("100")); @@ -128,12 +118,12 @@ public class ImageLinkFilterTest { @Test public void nullImageIsReturnedAsNull() { - assertThat(imageLinkFilter.format(templateContext, null, null), nullValue()); + assertThat(imageLinkFilter.format(null, null, null), nullValue()); } @Test public void stringIsUsedToLoadImageFromCore() { - String result = String.valueOf(imageLinkFilter.format(templateContext, "image-id", ImmutableMap.of())); + String result = String.valueOf(imageLinkFilter.format(null, "image-id", ImmutableMap.of())); Element imageElement = getSingleElement(result); assertThat(imageElement.attr("class"), is("")); assertThat(imageElement.attr("src"), is("/image-key"));