2 * Sone - ImageLinkFilterTest.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.template;
20 import static com.google.common.base.Optional.of;
21 import static net.pterodactylus.util.template.TemplateContextFactory.getInstance;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.is;
24 import static org.hamcrest.Matchers.nullValue;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.util.Collections;
31 import net.pterodactylus.sone.data.Image;
32 import net.pterodactylus.sone.data.Mocks;
33 import net.pterodactylus.util.template.HtmlFilter;
34 import net.pterodactylus.util.template.TemplateContextFactory;
36 import com.google.common.collect.ImmutableMap;
37 import org.junit.Before;
38 import org.junit.Test;
41 * Unit test for {@link ImageLinkFilter}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class ImageLinkFilterTest {
47 private final Mocks mocks = new Mocks();
48 private final TemplateContextFactory templateContextFactory = getInstance();
49 private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(mocks.core, templateContextFactory);
53 templateContextFactory.addFilter("css", new CssClassNameFilter());
54 templateContextFactory.addFilter("html", new HtmlFilter());
58 public void nullImageReturnsANullLink() {
59 assertThat(imageLinkFilter.format(null, null, Collections.<String, Object>emptyMap()), nullValue());
63 public void linkForAnImageWithoutAdditionalParametersIsGenerated() {
64 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
65 String link = (String) imageLinkFilter.format(null, image, Collections.<String, Object>emptyMap());
66 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
70 public void overridingTheTitleChangesBothAltAndTitleAttributes() {
71 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
72 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("title", "Other Title"));
73 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Other Title\" title=\"Other Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
77 public void classAttributeIsFilteredAndSet() {
78 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
79 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("class", "some class"));
80 assertThat(link, is("<img class=\"some_class\" src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
84 public void resizingImageToHalfItsSize() {
85 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
86 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("max-width", "320", "max-height", "320"));
87 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"320\" height=\"240\" style=\"position: relative;\"/>"));
91 public void enlargingAnImage() {
92 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
93 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "960", "max-height", "960"));
94 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"1280\" height=\"960\" style=\"position: relative;top: 0px;left: -160px;\"/>"));
98 public void notInsertedImage() {
99 Image image = createNotInsertedImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
100 String link = (String) imageLinkFilter.format(null, image, Collections.<String, Object>emptyMap());
101 assertThat(link, is("<img src=\"getImage.html?image=ImageId\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
105 public void creatingALinkFromAnImageId() {
106 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
107 when(mocks.core.getImage(eq("ImageId"))).thenReturn(of(image));
108 String link = (String) imageLinkFilter.format(null, image.getId(), Collections.<String, Object>emptyMap());
109 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
112 private Image createNotInsertedImage(String id, String key, String title, String description, int width, int height) {
113 Image image = createBasicImage(id, key, title, description, width, height);
114 when(image.isInserted()).thenReturn(false);
118 private Image createImage(String id, String key, String title, String description, int width, int height) {
119 Image image = createBasicImage(id, key, title, description, width, height);
120 when(image.isInserted()).thenReturn(true);
124 private Image createBasicImage(String id, String key, String title, String description, int width, int height) {
125 Image image = mock(Image.class);
126 when(image.getId()).thenReturn(id);
127 when(image.getKey()).thenReturn(key);
128 when(image.getTitle()).thenReturn(title);
129 when(image.getDescription()).thenReturn(description);
130 when(image.getWidth()).thenReturn(width);
131 when(image.getHeight()).thenReturn(height);