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", "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|foo|Post about foo...] is awesome."));
158 * Converts all given {@link Part}s into a string, validating that the
159 * part’s classes match only the expected classes.
162 * The parts to convert to text
163 * @param validClasses
164 * The valid classes; if no classes are given, all classes are
166 * @return The converted text
168 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
169 StringBuilder text = new StringBuilder();
170 for (Part part : parts) {
171 assertThat(part, notNullValue());
172 boolean classValid = validClasses.length == 0;
173 for (Class<?> validClass : validClasses) {
174 if (validClass.isAssignableFrom(part.getClass())) {
179 assertThat("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses), classValid, is(true));
180 if (part instanceof PlainTextPart) {
181 text.append(part.getText());
182 } else if (part instanceof FreenetLinkPart) {
183 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
185 .append(freenetLinkPart.getLink())
187 .append(freenetLinkPart.isTrusted() ? "trusted|" : "")
188 .append(freenetLinkPart.getTitle())
190 .append(freenetLinkPart.getText())
192 } else if (part instanceof LinkPart) {
193 LinkPart linkPart = (LinkPart) part;
195 .append(linkPart.getLink())
197 .append(linkPart.getTitle())
199 .append(linkPart.getText())
201 } else if (part instanceof SonePart) {
202 SonePart sonePart = (SonePart) part;
203 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
204 } else if (part instanceof PostPart) {
205 PostPart postPart = (PostPart) part;
206 text.append("[post|").append(postPart.getPost().getId()).append('|').append(postPart.getPost().getText()).append(']');
209 return text.toString();
213 * Mock Sone provider.
215 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
217 private static class TestSoneProvider implements SoneProvider {
220 public Function<String, Optional<Sone>> soneLoader() {
221 return new Function<String, Optional<Sone>>() {
223 public Optional<Sone> apply(String soneId) {
224 return getSone(soneId);
233 public Optional<Sone> getSone(final String soneId) {
234 return Optional.<Sone>of(new IdOnlySone(soneId));
241 public Collection<Sone> getSones() {
249 public Collection<Sone> getLocalSones() {
257 public Collection<Sone> getRemoteSones() {
263 private static class TestPostProvider implements PostProvider {
265 private final Map<String, String> posts = new HashMap<String, String>();
267 private void addValidPostId(String validPostId, String text) {
268 posts.put(validPostId, text);
272 public Collection<Post> getDirectedPosts(String recipientId) {
273 return Collections.emptyList();
277 public Collection<Post> getPosts(String soneId) {
278 return Collections.emptyList();
282 public Optional<Post> getPost(String postId) {
283 if (posts.containsKey(postId)) {
284 Post post = mock(Post.class);
285 when(post.getId()).thenReturn(postId);
286 when(post.getText()).thenReturn(posts.get(postId));
287 return Optional.of(post);
289 return Optional.absent();