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 java.io.IOException;
21 import java.io.StringReader;
22 import java.util.Arrays;
24 import com.google.common.base.Optional;
26 import junit.framework.TestCase;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.database.SoneProvider;
31 * JUnit test case for {@link SoneTextParser}.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class SoneTextParserTest extends TestCase {
42 * Tests basic plain-text operation of the parser.
45 * if an I/O error occurs
47 @SuppressWarnings("static-method")
48 public void testPlainText() throws IOException {
49 SoneTextParser soneTextParser = new SoneTextParser(null, null);
52 /* check basic operation. */
53 parts = soneTextParser.parse(null, new StringReader("Test."));
54 assertNotNull("Parts", parts);
55 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
57 /* check empty lines at start and end. */
58 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
59 assertNotNull("Parts", parts);
60 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
62 /* check duplicate empty lines in the text. */
63 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
64 assertNotNull("Parts", parts);
65 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
69 * Tests parsing of KSK links.
72 * if an I/O error occurs
74 @SuppressWarnings("static-method")
75 public void testKSKLinks() throws IOException {
76 SoneTextParser soneTextParser = new SoneTextParser(null, null);
79 /* check basic links. */
80 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
81 assertNotNull("Parts", parts);
82 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
84 /* check embedded links. */
85 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
86 assertNotNull("Parts", parts);
87 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
89 /* check embedded links and line breaks. */
90 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
91 assertNotNull("Parts", parts);
92 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
96 * Test case for a bug that was discovered in 0.6.7.
99 * if an I/O error occurs
101 @SuppressWarnings({ "synthetic-access", "static-method" })
102 public void testEmptyLinesAndSoneLinks() throws IOException {
103 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
104 Iterable<Part> parts;
106 /* check basic links. */
107 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
108 assertNotNull("Parts", parts);
109 assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
113 * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
114 * parsed into a link.
116 * @throws IOException
117 * if an I/O error occurs
119 @SuppressWarnings({ "synthetic-access", "static-method" })
120 public void testEmpyHttpLinks() throws IOException {
121 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
122 Iterable<Part> parts;
124 /* check empty http links. */
125 parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
126 assertNotNull("Parts", parts);
127 assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
135 * Converts all given {@link Part}s into a string, validating that the
136 * part’s classes match only the expected classes.
139 * The parts to convert to text
140 * @param validClasses
141 * The valid classes; if no classes are given, all classes are
143 * @return The converted text
145 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
146 StringBuilder text = new StringBuilder();
147 for (Part part : parts) {
148 assertNotNull("Part", part);
149 boolean classValid = validClasses.length == 0;
150 for (Class<?> validClass : validClasses) {
151 if (validClass.isAssignableFrom(part.getClass())) {
157 fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
159 if (part instanceof PlainTextPart) {
160 text.append(((PlainTextPart) part).getText());
161 } else if (part instanceof FreenetLinkPart) {
162 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
163 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
164 } else if (part instanceof LinkPart) {
165 LinkPart linkPart = (LinkPart) part;
166 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
167 } else if (part instanceof SonePart) {
168 SonePart sonePart = (SonePart) part;
169 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
172 return text.toString();
176 * Mock Sone provider.
178 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
180 private static class TestSoneProvider implements SoneProvider {
186 public Optional<Sone> getSone(final String soneId) {
187 return Optional.<Sone> fromNullable(new Sone(soneId, false) {
193 public String getName() {