--- /dev/null
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+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 java.util.Map;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.AlbumPart;
+import net.pterodactylus.sone.text.FreenetLinkPart;
+import net.pterodactylus.sone.text.LinkPart;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.PostPart;
+import net.pterodactylus.sone.text.SonePart;
+import net.pterodactylus.util.template.HtmlFilter;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link RenderFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RenderFilterTest {
+
+ private static final Map<String, Object> EMPTY_MAP = Collections.emptyMap();
+ private final Core core = mock(Core.class);
+ private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
+ private final RenderFilter renderFilter = new RenderFilter(core, templateContextFactory);
+ private final TemplateContext templateContext = new TemplateContext();
+ private final Sone sone = mock(Sone.class);
+ private final Post post = mock(Post.class);
+
+ @Before
+ public void setupTemplateContextFactory() {
+ templateContextFactory.addFilter("html", new HtmlFilter());
+ }
+
+ @Before
+ public void setupSone() {
+ when(sone.getId()).thenReturn("sone-id");
+ when(sone.getName()).thenReturn("SoneName");
+ when(sone.getProfile()).thenReturn(new Profile(sone));
+ }
+
+ @Before
+ public void setupPost() {
+ when(post.getId()).thenReturn("post-id");
+ when(post.getSone()).thenReturn(sone);
+ }
+
+ @Test
+ public void filterCanRenderPlainText() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "<Text>"));
+ }
+
+ @Test
+ public void filterCanRenderMultiplePlainTextParts() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"), new PlainTextPart("<Foo>"));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "<Text><Foo>"));
+ }
+
+ @Test
+ public void filterCanRenderUntrustedFreenetLinks() {
+ List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", false));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"freenet\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
+ }
+
+ @Test
+ public void filterCanRenderTrustedFreenetLinks() {
+ List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", true));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"freenet-trusted\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
+ }
+
+ @Test
+ public void filterCanRenderInternetLinks() {
+ List<Part> parts = Arrays.<Part>asList(new LinkPart("http://link.sone", "link.sone"));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"internet\" href=\"/external-link/?_CHECKED_HTTP_=http%3A%2F%2Flink.sone\" title=\"link.sone\">link.sone</a>"));
+ }
+
+ @Test
+ public void filterCanRenderSonePartsForUnknownSones() {
+ Sone unknownSone = mock(Sone.class);
+ when(unknownSone.getId()).thenReturn("sone-id");
+ List<Part> parts = Arrays.<Part>asList(new SonePart(unknownSone));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"/WebOfTrust/ShowIdentity?id=sone-id\" title=\"sone-id\">sone-id</a>"));
+ }
+
+ @Test
+ public void filterCanRenderSonePartsForKnownSones() {
+ List<Part> parts = Arrays.<Part>asList(new SonePart(sone));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewSone.html?sone=sone-id\" title=\"SoneName\">SoneName</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostParts() {
+ when(post.getText()).thenReturn("123456789012345678901234567890");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345678901234567890…</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostPartsWithoutBreakingWords() {
+ when(post.getText()).thenReturn("12345 12345 12345 12345 12345 12345");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345 12345…</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostPartsWithShortText() {
+ when(post.getText()).thenReturn("12345 12345");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
+ }
+
+ @Test
+ public void filterCanRenderPostPartsWithOldPostIds() {
+ when(post.getText()).thenReturn("12345 12345");
+ List<Part> parts = Arrays.<Part>asList(new PostPart(post, true));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"internet\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
+ }
+
+ @Test
+ public void filterCanRenderAlbumParts() {
+ Album album = mock(Album.class);
+ when(album.getId()).thenReturn("album-id");
+ when(album.getTitle()).thenReturn("Title");
+ when(album.getDescription()).thenReturn("Description");
+ List<Part> parts = Arrays.<Part>asList(new AlbumPart(album));
+ assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+ is((Object) "<a class=\"in-sone\" href=\"imageBrowser.html?album=album-id\" title=\"Description\">Title</a>"));
+ }
+
+ @Test
+ public void filterHonorsLength() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
+ assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10")),
+ is((Object) "1234567890…"));
+ }
+
+ @Test
+ public void filterHonorsCutOffLength() {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
+ assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10", "cut-off-length", "5")),
+ is((Object) "12345…"));
+ }
+
+}