2 * Sone - SoneTextParserTest.java - Copyright © 2011–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.text;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.is;
22 import static org.hamcrest.Matchers.notNullValue;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.HashSet;
36 import net.pterodactylus.sone.data.Post;
37 import net.pterodactylus.sone.data.Sone;
38 import net.pterodactylus.sone.data.impl.IdOnlySone;
39 import net.pterodactylus.sone.database.PostProvider;
40 import net.pterodactylus.sone.database.SoneProvider;
42 import com.google.common.base.Function;
43 import com.google.common.base.Optional;
44 import org.junit.Test;
45 import org.mockito.Mockito;
46 import org.mockito.stubbing.OngoingStubbing;
49 * JUnit test case for {@link SoneTextParser}.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class SoneTextParserTest {
55 private final SoneProvider soneProvider = new TestSoneProvider();
56 private final TestPostProvider postProvider = new TestPostProvider();
57 private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider);
60 * Tests basic plain-text operation of the parser.
63 * if an I/O error occurs
66 public void testPlainText() throws IOException {
69 /* check basic operation. */
70 parts = soneTextParser.parse(null, new StringReader("Test."));
71 assertThat(parts, notNullValue());
72 assertThat(convertText(parts, PlainTextPart.class), is("Test."));
74 /* check empty lines at start and end. */
75 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
76 assertThat(parts, notNullValue());
77 assertThat(convertText(parts, PlainTextPart.class), is("Test."));
79 /* check duplicate empty lines in the text. */
80 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
81 assertThat(parts, notNullValue());
82 assertThat(convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
86 * Tests parsing of KSK links.
89 * if an I/O error occurs
92 public void testKSKLinks() throws IOException {
95 /* check basic links. */
96 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
97 assertThat(parts, notNullValue());
98 assertThat(convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
100 /* check embedded links. */
101 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
102 assertThat(parts, notNullValue());
103 assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
105 /* check embedded links and line breaks. */
106 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
107 assertThat(parts, notNullValue());
108 assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class),
109 is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
113 * Test case for a bug that was discovered in 0.6.7.
115 * @throws IOException
116 * if an I/O error occurs
119 public void testEmptyLinesAndSoneLinks() throws IOException {
120 Iterable<Part> parts;
122 /* check basic links. */
123 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
124 assertThat(parts, notNullValue());
125 assertThat(convertText(parts, PlainTextPart.class, SonePart.class),
126 is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
130 * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
131 * parsed into a link.
133 * @throws IOException
134 * if an I/O error occurs
137 public void testEmpyHttpLinks() throws IOException {
138 Iterable<Part> parts;
140 /* check empty http links. */
141 parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
142 assertThat(parts, notNullValue());
143 assertThat(convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
147 public void linksToPostAreParsedCorrectly() throws IOException {
148 postProvider.addValidPostId("foo", "internal", "Post about foo...");
149 Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This post://foo is awesome."));
150 assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|new|foo|Post about foo...] is awesome."));
154 public void linksToPostsWithOldIdsAreParsedCorrectly() throws IOException {
155 postProvider.addValidPostId("foo", "internal", "Post about foo...");
156 Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This post://internal is awesome."));
157 assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|old|foo|Post about foo...] is awesome."));
165 * Converts all given {@link Part}s into a string, validating that the
166 * part’s classes match only the expected classes.
169 * The parts to convert to text
170 * @param validClasses
171 * The valid classes; if no classes are given, all classes are
173 * @return The converted text
175 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
176 StringBuilder text = new StringBuilder();
177 for (Part part : parts) {
178 assertThat(part, notNullValue());
179 boolean classValid = validClasses.length == 0;
180 for (Class<?> validClass : validClasses) {
181 if (validClass.isAssignableFrom(part.getClass())) {
186 assertThat("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses), classValid, is(true));
187 if (part instanceof PlainTextPart) {
188 text.append(part.getText());
189 } else if (part instanceof FreenetLinkPart) {
190 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
192 .append(freenetLinkPart.getLink())
194 .append(freenetLinkPart.isTrusted() ? "trusted|" : "")
195 .append(freenetLinkPart.getTitle())
197 .append(freenetLinkPart.getText())
199 } else if (part instanceof LinkPart) {
200 LinkPart linkPart = (LinkPart) part;
202 .append(linkPart.getLink())
204 .append(linkPart.getTitle())
206 .append(linkPart.getText())
208 } else if (part instanceof SonePart) {
209 SonePart sonePart = (SonePart) part;
210 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
211 } else if (part instanceof PostPart) {
212 PostPart postPart = (PostPart) part;
213 text.append("[post|")
214 .append(postPart.usesDeprecatedLink() ? "old" : "new")
216 .append(postPart.getPost().getId())
218 .append(postPart.getPost().getText())
222 return text.toString();
226 * Mock Sone provider.
228 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
230 private static class TestSoneProvider implements SoneProvider {
233 public Function<String, Optional<Sone>> soneLoader() {
234 return new Function<String, Optional<Sone>>() {
236 public Optional<Sone> apply(String soneId) {
237 return getSone(soneId);
246 public Optional<Sone> getSone(final String soneId) {
247 return Optional.<Sone>of(new IdOnlySone(soneId));
254 public Collection<Sone> getSones() {
262 public Collection<Sone> getLocalSones() {
270 public Collection<Sone> getRemoteSones() {
276 private static class TestPostProvider implements PostProvider {
278 private final Map<String, String> postTexts = new HashMap<String, String>();
279 private final Map<String, String> postInternalIds = new HashMap<String, String>();
280 private final Map<String, String> internalIdPosts = new HashMap<String, String>();
282 private void addValidPostId(String validPostId, String internalId, String text) {
283 postTexts.put(validPostId, text);
284 postInternalIds.put(validPostId, internalId);
285 internalIdPosts.put(internalId, validPostId);
289 public Collection<Post> getDirectedPosts(String recipientId) {
290 return Collections.emptyList();
294 public Collection<Post> getPosts(String soneId) {
295 return Collections.emptyList();
299 public Optional<Post> getPost(String postId) {
300 if (postTexts.containsKey(postId)) {
301 Post post = mock(Post.class);
302 when(post.getId()).thenReturn(postId);
303 when(post.getInternalId()).thenReturn(postInternalIds.get(postId));
304 when(post.getText()).thenReturn(postTexts.get(postId));
305 return Optional.of(post);
306 } else if (internalIdPosts.containsKey(postId)) {
307 Post post = mock(Post.class);
308 when(post.getId()).thenReturn(internalIdPosts.get(postId));
309 when(post.getInternalId()).thenReturn(postId);
310 when(post.getText()).thenReturn(postTexts.get(internalIdPosts.get(postId)));
311 return Optional.of(post);
313 return Optional.absent();