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;
*/
public class AlbumAccessor extends ReflectionAccessor {
+ private final Random random = new Random();
+
/**
* {@inheritDoc}
*/
}
backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
return backlinks;
+ } else if ("randomImage".equals(member)) {
+ List<Image> images = album.getImages();
+ return images.isEmpty() ? null : images.get(random.nextInt(images.size()));
}
return super.get(templateContext, object, member);
}
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;
};
}
+ @Test
+ public void albumImageIsGeneratedRandomly() {
+ Image image = mock(Image.class);
+ List<Image> 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.<Image>emptyList());
+ assertThat(albumAccessor.get(null, album, "randomImage"), nullValue());
+ }
+
}