From d9206828d23e4c0092499c46ffd75f6792c928bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 23 Jun 2016 20:12:56 +0200 Subject: [PATCH] =?utf8?q?Add=20=E2=80=9CrandomImage=E2=80=9D=C2=A0accesso?= =?utf8?q?r=20for=20albums?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/template/AlbumAccessor.java | 7 ++++++ .../sone/template/AlbumAccessorTest.java | 28 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java index e19a89a..5093442 100644 --- a/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java @@ -19,8 +19,10 @@ package net.pterodactylus.sone.template; import java.util.ArrayList; import java.util.List; +import java.util.Random; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; import net.pterodactylus.util.template.Accessor; import net.pterodactylus.util.template.ReflectionAccessor; import net.pterodactylus.util.template.TemplateContext; @@ -34,6 +36,8 @@ import net.pterodactylus.util.template.TemplateContext; */ public class AlbumAccessor extends ReflectionAccessor { + private final Random random = new Random(); + /** * {@inheritDoc} */ @@ -49,6 +53,9 @@ public class AlbumAccessor extends ReflectionAccessor { } backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone()))); return backlinks; + } else if ("randomImage".equals(member)) { + List images = album.getImages(); + return images.isEmpty() ? null : images.get(random.nextInt(images.size())); } return super.get(templateContext, object, member); } diff --git a/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java index afc2a54..b31eabf 100644 --- a/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java +++ b/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java @@ -1,15 +1,22 @@ package net.pterodactylus.sone.template; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import net.pterodactylus.sone.TestUtil; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; @@ -90,4 +97,25 @@ public class AlbumAccessorTest { }; } + @Test + public void albumImageIsGeneratedRandomly() { + Image image = mock(Image.class); + List albumImages = Arrays.asList(mock(Image.class), image); + when(album.getImages()).thenReturn(albumImages); + int matchedImage = 0; + for (int i = 0; i < 1000; i++) { + Image randomImage = (Image) albumAccessor.get(null, album, "randomImage"); + if (randomImage == image) { + matchedImage++; + } + } + assertThat(matchedImage, allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(750))); + } + + @Test + public void albumImageIsNullIfThereAreNoImagesInAnAlbum() { + when(album.getImages()).thenReturn(Collections.emptyList()); + assertThat(albumAccessor.get(null, album, "randomImage"), nullValue()); + } + } -- 2.7.4