From b52864b39fbef35ffc68c94b441f76e430ec9af4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 11 Nov 2013 07:20:32 +0100 Subject: [PATCH] Add unit test for ImageLinkFilter. --- .../sone/template/ImageLinkFilterTest.java | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java 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..b2cdf13 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/ImageLinkFilterTest.java @@ -0,0 +1,135 @@ +/* + * Sone - ImageLinkFilterTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import static com.google.common.base.Optional.of; +import static net.pterodactylus.util.template.TemplateContextFactory.getInstance; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; + +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.Mocks; +import net.pterodactylus.util.template.HtmlFilter; +import net.pterodactylus.util.template.TemplateContextFactory; + +import com.google.common.collect.ImmutableMap; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link ImageLinkFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class ImageLinkFilterTest { + + private final Mocks mocks = new Mocks(); + private final TemplateContextFactory templateContextFactory = getInstance(); + private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(mocks.core, templateContextFactory); + + @Before + public void setup() { + templateContextFactory.addFilter("css", new CssClassNameFilter()); + templateContextFactory.addFilter("html", new HtmlFilter()); + } + + @Test + public void nullImageReturnsANullLink() { + assertThat(imageLinkFilter.format(null, null, Collections.emptyMap()), nullValue()); + } + + @Test + public void linkForAnImageWithoutAdditionalParametersIsGenerated() { + Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480); + String link = (String) imageLinkFilter.format(null, image, Collections.emptyMap()); + assertThat(link, is("\"Description\"")); + } + + @Test + public void overridingTheTitleChangesBothAltAndTitleAttributes() { + Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480); + String link = (String) imageLinkFilter.format(null, image, ImmutableMap.of("title", "Other Title")); + assertThat(link, is("\"Other")); + } + + @Test + public void classAttributeIsFilteredAndSet() { + Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480); + String link = (String) imageLinkFilter.format(null, image, ImmutableMap.of("class", "some class")); + assertThat(link, is("\"Description\"")); + } + + @Test + public void resizingImageToHalfItsSize() { + Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480); + String link = (String) imageLinkFilter.format(null, image, ImmutableMap.of("max-width", "320", "max-height", "320")); + assertThat(link, is("\"Description\"")); + } + + @Test + public void enlargingAnImage() { + Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480); + String link = (String) imageLinkFilter.format(null, image, ImmutableMap.of("mode", "enlarge", "max-width", "960", "max-height", "960")); + assertThat(link, is("\"Description\"")); + } + + @Test + public void notInsertedImage() { + Image image = createNotInsertedImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480); + String link = (String) imageLinkFilter.format(null, image, Collections.emptyMap()); + assertThat(link, is("\"Description\"")); + } + + @Test + public void creatingALinkFromAnImageId() { + Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480); + when(mocks.core.getImage(eq("ImageId"))).thenReturn(of(image)); + String link = (String) imageLinkFilter.format(null, image.getId(), Collections.emptyMap()); + assertThat(link, is("\"Description\"")); + } + + private Image createNotInsertedImage(String id, String key, String title, String description, int width, int height) { + Image image = createBasicImage(id, key, title, description, width, height); + when(image.isInserted()).thenReturn(false); + return image; + } + + private Image createImage(String id, String key, String title, String description, int width, int height) { + Image image = createBasicImage(id, key, title, description, width, height); + when(image.isInserted()).thenReturn(true); + return image; + } + + private Image createBasicImage(String id, String key, String title, String description, int width, int height) { + Image image = mock(Image.class); + when(image.getId()).thenReturn(id); + when(image.getKey()).thenReturn(key); + when(image.getTitle()).thenReturn(title); + when(image.getDescription()).thenReturn(description); + when(image.getWidth()).thenReturn(width); + when(image.getHeight()).thenReturn(height); + return image; + } + +} -- 2.7.4