From: David ‘Bombe’ Roden Date: Thu, 23 Jun 2016 18:12:56 +0000 (+0200) Subject: Add “randomImage” accessor for albums X-Git-Tag: 0.9.5^2~28 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=d9206828d23e4c0092499c46ffd75f6792c928bc;p=Sone.git Add “randomImage” accessor for albums --- 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()); + } + }