X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FSoneTextParserTest.java;h=b8d2fb4f0bfc1b7784fceaf5142b532d6805e4fd;hb=f4dd3d176c5732a7e2111f37cdbb4ea6c83aaf46;hp=f0b0e1bd9464549fb23eb3d2f9530bd99c5b67e9;hpb=6b9c7c806fd1fc168e598d6e4eb6ee46ad2aa6fd;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java
index f0b0e1b..b8d2fb4 100644
--- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java
+++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java
@@ -1,5 +1,5 @@
/*
- * Sone - SoneTextParserTest.java - Copyright © 2011 David Roden
+ * Sone - SoneTextParserTest.java - Copyright © 2011â2016 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,58 +17,211 @@
package net.pterodactylus.sone.text;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.isIn;
+import static org.hamcrest.Matchers.notNullValue;
+
import java.io.IOException;
-import java.io.StringReader;
+import java.util.Collection;
+
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.SoneProvider;
-import junit.framework.TestCase;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import org.junit.Test;
/**
* JUnit test case for {@link SoneTextParser}.
*
* @author David âBombeâ Roden
*/
-public class SoneTextParserTest extends TestCase {
+public class SoneTextParserTest {
+
+ private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
+
+ @SuppressWarnings("static-method")
+ @Test
+ public void testPlainText() throws IOException {
+ /* check basic operation. */
+ Iterable parts = soneTextParser.parse("Test.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
+
+ /* check empty lines at start and end. */
+ parts = soneTextParser.parse("\nTest.\n\n", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
+
+ /* check duplicate empty lines in the text. */
+ parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
+ }
+
+ @SuppressWarnings("static-method")
+ @Test
+ public void testKSKLinks() throws IOException {
+ /* check basic links. */
+ Iterable parts = soneTextParser.parse("KSK@gpl.txt", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
+
+ /* check embedded links. */
+ parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
+
+ /* check embedded links and line breaks. */
+ parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
+ assertThat("Parts", parts, notNullValue());
+ 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]"));
+ }
+
+ @SuppressWarnings({ "synthetic-access", "static-method" })
+ @Test
+ public void testEmptyLinesAndSoneLinks() throws IOException {
+ SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
+
+ /* check basic links. */
+ Iterable parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
+ }
+
+ @SuppressWarnings({ "synthetic-access", "static-method" })
+ @Test
+ public void testEmpyHttpLinks() throws IOException {
+ SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
+
+ /* check empty http links. */
+ Iterable parts = soneTextParser.parse("Some text. Empty link: http:// â nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// â nice!"));
+ }
+
+ @Test
+ public void httpLinkWithoutParensEndsAtNextClosingParen() {
+ Iterable parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) â nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ 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!"));
+ }
+
+ @Test
+ public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
+ Iterable parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) â nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ 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!"));
+ }
+
+ @Test
+ public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
+ SoneTextParser soneTextParser = new SoneTextParser(null, null);
+ Iterable parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ 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!"));
+ }
+
+ @Test
+ public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
+ Iterable parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ 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!"));
+ }
+
+ @Test
+ public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
+ SoneTextParser soneTextParser = new SoneTextParser(null, null);
+ Iterable parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
+ assertThat("Parts", parts, notNullValue());
+ 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!"));
+ }
/**
- * Tests basic plain-text operation of the parser.
+ * Converts all given {@link Part}s into a string, validating that the
+ * partâs classes match only the expected classes.
*
- * @throws IOException
- * if an I/O error occurs
+ * @param parts
+ * The parts to convert to text
+ * @param validClasses
+ * The valid classes; if no classes are given, all classes are
+ * valid
+ * @return The converted text
*/
- public void testPlainText() throws IOException {
- SoneTextParser soneTextParser = new SoneTextParser(null, null);
- Iterable parts;
+ private static String convertText(Iterable parts, Class>... validClasses) {
StringBuilder text = new StringBuilder();
-
- /* check basic operation. */
- text.setLength(0);
- parts = soneTextParser.parse(null, new StringReader("Test."));
- assertNotNull("Parts", parts);
for (Part part : parts) {
- assertTrue("Part is PlainTextPart", part instanceof PlainTextPart);
- text.append(((PlainTextPart) part).getText());
+ assertThat("Part", part, notNullValue());
+ if (validClasses.length != 0) {
+ assertThat("Partâs class", part.getClass(), isIn(validClasses));
+ }
+ if (part instanceof PlainTextPart) {
+ text.append(((PlainTextPart) part).getText());
+ } else if (part instanceof FreenetLinkPart) {
+ FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
+ text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
+ } else if (part instanceof LinkPart) {
+ LinkPart linkPart = (LinkPart) part;
+ text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
+ } else if (part instanceof SonePart) {
+ SonePart sonePart = (SonePart) part;
+ text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
+ }
}
- assertEquals("Part Text", "Test.", text.toString());
+ return text.toString();
+ }
- /* check empty lines at start and end. */
- text.setLength(0);
- parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
- assertNotNull("Parts", parts);
- for (Part part : parts) {
- assertTrue("Part is PlainTextPart", part instanceof PlainTextPart);
- text.append(((PlainTextPart) part).getText());
+ /**
+ * Mock Sone provider.
+ *
+ * @author David âBombeâ Roden
+ */
+ private static class TestSoneProvider implements SoneProvider {
+
+ @Override
+ public Function> soneLoader() {
+ return new Function>() {
+ @Override
+ public Optional apply(String soneId) {
+ return getSone(soneId);
+ }
+ };
}
- assertEquals("Part Text", "Test.", text.toString());
- /* check duplicate empty lines in the text. */
- text.setLength(0);
- parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
- assertNotNull("Parts", parts);
- for (Part part : parts) {
- assertTrue("Part is PlainTextPart", part instanceof PlainTextPart);
- text.append(((PlainTextPart) part).getText());
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Optional getSone(final String soneId) {
+ return Optional.of(new IdOnlySone(soneId));
}
- assertEquals("Part Text", "Test.\n\nTest.", text.toString());
+
+ /**
+ * {@inheritDocs}
+ */
+ @Override
+ public Collection getSones() {
+ return null;
+ }
+
+ /**
+ * {@inheritDocs}
+ */
+ @Override
+ public Collection getLocalSones() {
+ return null;
+ }
+
+ /**
+ * {@inheritDocs}
+ */
+ @Override
+ public Collection getRemoteSones() {
+ return null;
+ }
+
}
}