From 7141f806ad1a6db1e4c84215b728e78319437e4b 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:21 +0100 Subject: [PATCH] Add unit test for ImageAccessor. --- .../sone/template/ImageAccessorTest.java | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/ImageAccessorTest.java diff --git a/src/test/java/net/pterodactylus/sone/template/ImageAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/ImageAccessorTest.java new file mode 100644 index 0000000..70b3133 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/ImageAccessorTest.java @@ -0,0 +1,113 @@ +/* + * Sone - ImageAccessorTest.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 java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.List; + +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; + +import org.junit.Test; + +/** + * Unit test for {@link ImageAccessor}. + * + * @author David ‘Bombe’ Roden + */ +public class ImageAccessorTest { + + private final ImageAccessor imageAccessor = new ImageAccessor(); + + @Test + public void previousImageIsFound() { + Image image = createImage("Image3"); + List images = createAlbumWithImages(asList( + createImage("Image1"), + createImage("Image2"), + image + )); + Image previousImage = (Image) imageAccessor.get(null, image, "previous"); + assertThat(previousImage, is(images.get(1))); + } + + private List createAlbumWithImages(List images) { + Album album = mock(Album.class); + when(album.getImages()).thenReturn(images); + for (Image image : images) { + when(image.getAlbum()).thenReturn(album); + } + return images; + } + + @Test + public void noPreviousImageIsFound() { + Image image = createImage("Image3"); + createAlbumWithImages(asList( + image, + createImage("Image1"), + createImage("Image2") + )); + Image previousImage = (Image) imageAccessor.get(null, image, "previous"); + assertThat(previousImage, nullValue()); + } + + @Test + public void noNextImageIsFound() { + Image image = createImage("Image3"); + List images = createAlbumWithImages(asList( + createImage("Image1"), + createImage("Image2"), + image + )); + Image previousImage = (Image) imageAccessor.get(null, image, "next"); + assertThat(previousImage, nullValue()); + } + + @Test + public void nextImageIsFound() { + Image image = createImage("Image3"); + List images = createAlbumWithImages(asList( + image, + createImage("Image1"), + createImage("Image2") + )); + Image previousImage = (Image) imageAccessor.get(null, image, "next"); + assertThat(previousImage, is(images.get(1))); + } + + @Test + public void reflectionAccessorIsUsed() { + Image image = createImage("imageId"); + String id = (String) imageAccessor.get(null, image, "id"); + assertThat(id, is(image.getId())); + } + + private static Image createImage(String id) { + Image image = mock(Image.class); + when(image.getId()).thenReturn(id); + return image; + } + +} -- 2.7.4