2 * Sone - SoneTextParserTest.java - Copyright © 2011–2016 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.isIn;
23 import static org.hamcrest.Matchers.notNullValue;
25 import java.io.IOException;
26 import java.util.Collection;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.data.impl.IdOnlySone;
31 import net.pterodactylus.sone.database.PostProvider;
32 import net.pterodactylus.sone.database.SoneProvider;
34 import com.google.common.base.Function;
35 import com.google.common.base.Optional;
36 import org.junit.Test;
39 * JUnit test case for {@link SoneTextParser}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class SoneTextParserTest {
45 private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
47 @SuppressWarnings("static-method")
49 public void testPlainText() throws IOException {
50 /* check basic operation. */
51 Iterable<Part> parts = soneTextParser.parse("Test.", null);
52 assertThat("Parts", parts, notNullValue());
53 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
55 /* check empty lines at start and end. */
56 parts = soneTextParser.parse("\nTest.\n\n", null);
57 assertThat("Parts", parts, notNullValue());
58 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
60 /* check duplicate empty lines in the text. */
61 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
62 assertThat("Parts", parts, notNullValue());
63 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
67 public void consecutiveLinesAreSeparatedByLinefeed() {
68 Iterable<Part> parts = soneTextParser.parse("Text.\nText", null);
69 assertThat("Part Text", convertText(parts), is("Text.\nText"));
73 public void freenetLinksHaveTheFreenetPrefixRemoved() {
74 Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
75 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
79 public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
80 Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
81 assertThat("Part Text", convertText(parts), is("Text.\n[KSK@gpl.txt|gpl.txt|gpl.txt] and [KSK@gpl.txt|gpl.txt|gpl.txt]"));
85 public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() {
86 Iterable<Part> parts = soneTextParser.parse("sone://too-short", null);
87 assertThat("Part Text", convertText(parts), is("sone://too-short"));
91 public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() {
92 SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null);
93 Iterable<Part> parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null);
94 assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"));
98 public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
99 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
100 assertThat("Part Text", convertText(parts), is("post://too-short"));
104 public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
105 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
106 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
107 assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"));
111 public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
112 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
113 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
114 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
118 public void nameOfFreenetLinkDoesNotContainUrlParameters() {
119 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
120 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|gpl.txt|gpl.txt]"));
124 public void trailingSlashInFreenetLinkIsRemovedForName() {
125 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
126 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|gpl.txt|gpl.txt]"));
130 public void lastMetaStringOfFreenetLinkIsUsedAsName() {
131 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
132 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING|COPYING]"));
136 public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
137 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
138 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm|CHK@qM1nm]"));
142 public void malformedKeyIsRenderedAsPlainText() {
143 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
144 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
148 public void httpsLinkHasItsPathsShortened() {
149 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
150 assertThat("Part Text", convertText(parts), is("[https://test.test/some-long-path/file.txt|test.test/…/file.txt|test.test/…/file.txt]"));
154 public void httpLinksHaveTheirLastSlashRemoved() {
155 Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
156 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|test.test/…|test.test/…]"));
160 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
161 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
162 assertThat("Part Text", convertText(parts), is("[http://www.test.test|test.test|test.test]"));
166 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
167 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
168 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|test.test/test.html|test.test/test.html]"));
172 public void hostnameIsKeptIntactIfNotBeginningWithWww() {
173 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
174 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|test.test.test/test.html|test.test.test/test.html]"));
178 public void hostnameWithOneDotButNoSlashIsKeptIntact() {
179 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
180 assertThat("Part Text", convertText(parts), is("[http://test.test|test.test|test.test]"));
184 public void urlParametersAreRemovedForHttpLinks() {
185 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
186 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|test.test|test.test]"));
190 public void emptyStringIsParsedCorrectly() {
191 Iterable<Part> parts = soneTextParser.parse("", null);
192 assertThat("Part Text", convertText(parts), is(""));
196 public void linksAreParsedInCorrectOrder() {
197 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
198 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
202 public void sskLinkWithoutContextIsNotTrusted() {
203 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
204 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]"));
208 public void sskLinkWithContextWithoutSoneIsNotTrusted() {
209 SoneTextParserContext context = new SoneTextParserContext(null);
210 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
211 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]"));
215 public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
216 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
217 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
218 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]"));
222 public void sskLinkWithContextWithCorrectSoneIsTrusted() {
223 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
224 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
225 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|trusted|test|test]"));
229 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
230 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
231 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
232 assertThat("Part Text", convertText(parts), is("[USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|trusted|test|test]"));
235 @SuppressWarnings("static-method")
237 public void testKSKLinks() throws IOException {
238 /* check basic links. */
239 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
240 assertThat("Parts", parts, notNullValue());
241 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
243 /* check embedded links. */
244 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
245 assertThat("Parts", parts, notNullValue());
246 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
248 /* check embedded links and line breaks. */
249 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
250 assertThat("Parts", parts, notNullValue());
251 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
254 @SuppressWarnings({ "synthetic-access", "static-method" })
256 public void testEmptyLinesAndSoneLinks() throws IOException {
257 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
259 /* check basic links. */
260 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
261 assertThat("Parts", parts, notNullValue());
262 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
265 @SuppressWarnings({ "synthetic-access", "static-method" })
267 public void testEmpyHttpLinks() throws IOException {
268 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
270 /* check empty http links. */
271 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
272 assertThat("Parts", parts, notNullValue());
273 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
277 public void httpLinkWithoutParensEndsAtNextClosingParen() {
278 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
279 assertThat("Parts", parts, notNullValue());
280 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]) – nice!"));
284 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
285 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
286 assertThat("Parts", parts, notNullValue());
287 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc_(def)|example.sone/abc_(def)|example.sone/abc_(def)]) – nice!"));
291 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
292 SoneTextParser soneTextParser = new SoneTextParser(null, null);
293 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
294 assertThat("Parts", parts, notNullValue());
295 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]. Nice!"));
299 public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
300 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
301 assertThat("Parts", parts, notNullValue());
302 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]... Nice!"));
306 public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
307 SoneTextParser soneTextParser = new SoneTextParser(null, null);
308 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
309 assertThat("Parts", parts, notNullValue());
310 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc], nice!"));
314 * Converts all given {@link Part}s into a string, validating that the
315 * part’s classes match only the expected classes.
318 * The parts to convert to text
319 * @param validClasses
320 * The valid classes; if no classes are given, all classes are
322 * @return The converted text
324 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
325 StringBuilder text = new StringBuilder();
326 for (Part part : parts) {
327 assertThat("Part", part, notNullValue());
328 if (validClasses.length != 0) {
329 assertThat("Part’s class", part.getClass(), isIn(validClasses));
331 if (part instanceof PlainTextPart) {
332 text.append(((PlainTextPart) part).getText());
333 } else if (part instanceof FreenetLinkPart) {
334 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
335 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
336 } else if (part instanceof LinkPart) {
337 LinkPart linkPart = (LinkPart) part;
338 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
339 } else if (part instanceof SonePart) {
340 SonePart sonePart = (SonePart) part;
341 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
342 } else if (part instanceof PostPart) {
343 PostPart postPart = (PostPart) part;
344 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
347 return text.toString();
351 * Mock Sone provider.
353 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
355 private static class TestSoneProvider implements SoneProvider {
358 public Function<String, Optional<Sone>> soneLoader() {
359 return new Function<String, Optional<Sone>>() {
361 public Optional<Sone> apply(String soneId) {
362 return getSone(soneId);
371 public Optional<Sone> getSone(final String soneId) {
372 return Optional.<Sone>of(new IdOnlySone(soneId));
379 public Collection<Sone> getSones() {
387 public Collection<Sone> getLocalSones() {
395 public Collection<Sone> getRemoteSones() {
401 private static class AbsentSoneProvider extends TestSoneProvider {
404 public Optional<Sone> getSone(String soneId) {
405 return Optional.absent();
410 private static class TestPostProvider implements PostProvider {
413 public Optional<Post> getPost(final String postId) {
414 return Optional.<Post>of(new Post() {
416 public String getId() {
421 public boolean isLoaded() {
426 public Sone getSone() {
431 public Optional<String> getRecipientId() {
436 public Optional<Sone> getRecipient() {
441 public long getTime() {
446 public String getText() {
451 public boolean isKnown() {
456 public Post setKnown(boolean known) {
463 public Collection<Post> getPosts(String soneId) {
468 public Collection<Post> getDirectedPosts(String recipientId) {
474 private static class AbsentPostProvider extends TestPostProvider {
477 public Optional<Post> getPost(String postId) {
478 return Optional.absent();