2 * Sone - ImageAccessorTest.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 java.util.Arrays.asList;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.nullValue;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import java.util.List;
29 import net.pterodactylus.sone.data.Album;
30 import net.pterodactylus.sone.data.Image;
32 import org.junit.Test;
35 * Unit test for {@link ImageAccessor}.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class ImageAccessorTest {
41 private final ImageAccessor imageAccessor = new ImageAccessor();
44 public void previousImageIsFound() {
45 Image image = createImage("Image3");
46 List<Image> images = createAlbumWithImages(asList(
47 createImage("Image1"),
48 createImage("Image2"),
51 Image previousImage = (Image) imageAccessor.get(null, image, "previous");
52 assertThat(previousImage, is(images.get(1)));
55 private List<Image> createAlbumWithImages(List<Image> images) {
56 Album album = mock(Album.class);
57 when(album.getImages()).thenReturn(images);
58 for (Image image : images) {
59 when(image.getAlbum()).thenReturn(album);
65 public void noPreviousImageIsFound() {
66 Image image = createImage("Image3");
67 createAlbumWithImages(asList(
69 createImage("Image1"),
72 Image previousImage = (Image) imageAccessor.get(null, image, "previous");
73 assertThat(previousImage, nullValue());
77 public void noNextImageIsFound() {
78 Image image = createImage("Image3");
79 List<Image> images = createAlbumWithImages(asList(
80 createImage("Image1"),
81 createImage("Image2"),
84 Image previousImage = (Image) imageAccessor.get(null, image, "next");
85 assertThat(previousImage, nullValue());
89 public void nextImageIsFound() {
90 Image image = createImage("Image3");
91 List<Image> images = createAlbumWithImages(asList(
93 createImage("Image1"),
96 Image previousImage = (Image) imageAccessor.get(null, image, "next");
97 assertThat(previousImage, is(images.get(1)));
101 public void reflectionAccessorIsUsed() {
102 Image image = createImage("imageId");
103 String id = (String) imageAccessor.get(null, image, "id");
104 assertThat(id, is(image.getId()));
107 private static Image createImage(String id) {
108 Image image = mock(Image.class);
109 when(image.getId()).thenReturn(id);