♻️ Create custom context factory in image link filter
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 9 Mar 2019 22:42:19 +0000 (23:42 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 9 Mar 2019 22:42:19 +0000 (23:42 +0100)
src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java

index d230f24..fc4b803 100644 (file)
@@ -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());
        }
 
        /**
index fb17e93..0b25ccd 100644 (file)
@@ -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());
index 7cc88b1..5d95423 100644 (file)
@@ -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.<String, Object>of()));
+               String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>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.<String, Object>of()));
+               String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>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.<String, Object>of("title", "Test Title")));
+               String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>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.<String, Object>of("max-width", "320")));
+               String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>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.<String, Object>of("max-height", "135")));
+               String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>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.<String, Object>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.<String, Object>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.<String, Object>of()));
+               String result = String.valueOf(imageLinkFilter.format(null, "image-id", ImmutableMap.<String, Object>of()));
                Element imageElement = getSingleElement(result);
                assertThat(imageElement.attr("class"), is(""));
                assertThat(imageElement.attr("src"), is("/image-key"));