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;
23 import java.util.Collection;
25 import net.pterodactylus.sone.data.LocalSone;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.data.impl.IdOnlySone;
28 import net.pterodactylus.sone.database.SoneProvider;
30 import com.google.common.base.Function;
31 import com.google.common.base.Optional;
32 import junit.framework.TestCase;
35 * JUnit test case for {@link SoneTextParser}.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class SoneTextParserTest extends TestCase {
46 * Tests basic plain-text operation of the parser.
49 * if an I/O error occurs
51 @SuppressWarnings("static-method")
52 public void testPlainText() throws IOException {
53 SoneTextParser soneTextParser = new SoneTextParser(null, null);
56 /* check basic operation. */
57 parts = soneTextParser.parse(null, new StringReader("Test."));
58 assertNotNull("Parts", parts);
59 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
61 /* check empty lines at start and end. */
62 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
63 assertNotNull("Parts", parts);
64 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
66 /* check duplicate empty lines in the text. */
67 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
68 assertNotNull("Parts", parts);
69 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
73 * Tests parsing of KSK links.
76 * if an I/O error occurs
78 @SuppressWarnings("static-method")
79 public void testKSKLinks() throws IOException {
80 SoneTextParser soneTextParser = new SoneTextParser(null, null);
83 /* check basic links. */
84 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
85 assertNotNull("Parts", parts);
86 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
88 /* check embedded links. */
89 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
90 assertNotNull("Parts", parts);
91 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
93 /* check embedded links and line breaks. */
94 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
95 assertNotNull("Parts", parts);
96 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));
100 * Test case for a bug that was discovered in 0.6.7.
102 * @throws IOException
103 * if an I/O error occurs
105 @SuppressWarnings({ "synthetic-access", "static-method" })
106 public void testEmptyLinesAndSoneLinks() throws IOException {
107 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
108 Iterable<Part> parts;
110 /* check basic links. */
111 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
112 assertNotNull("Parts", parts);
113 assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
117 * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
118 * parsed into a link.
120 * @throws IOException
121 * if an I/O error occurs
123 @SuppressWarnings({ "synthetic-access", "static-method" })
124 public void testEmpyHttpLinks() throws IOException {
125 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
126 Iterable<Part> parts;
128 /* check empty http links. */
129 parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
130 assertNotNull("Parts", parts);
131 assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
139 * Converts all given {@link Part}s into a string, validating that the
140 * part’s classes match only the expected classes.
143 * The parts to convert to text
144 * @param validClasses
145 * The valid classes; if no classes are given, all classes are
147 * @return The converted text
149 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
150 StringBuilder text = new StringBuilder();
151 for (Part part : parts) {
152 assertNotNull("Part", part);
153 boolean classValid = validClasses.length == 0;
154 for (Class<?> validClass : validClasses) {
155 if (validClass.isAssignableFrom(part.getClass())) {
161 fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
163 if (part instanceof PlainTextPart) {
164 text.append(((PlainTextPart) part).getText());
165 } else if (part instanceof FreenetLinkPart) {
166 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
167 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
168 } else if (part instanceof LinkPart) {
169 LinkPart linkPart = (LinkPart) part;
170 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
171 } else if (part instanceof SonePart) {
172 SonePart sonePart = (SonePart) part;
173 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
176 return text.toString();
180 * Mock Sone provider.
182 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
184 private static class TestSoneProvider implements SoneProvider {
187 public Function<String, Optional<Sone>> soneLoader() {
188 return new Function<String, Optional<Sone>>() {
190 public Optional<Sone> apply(String soneId) {
191 return getSone(soneId);
200 public Optional<Sone> getSone(final String soneId) {
201 return Optional.<Sone>of(new IdOnlySone(soneId));
208 public Collection<Sone> getSones() {
216 public Collection<LocalSone> getLocalSones() {
224 public Collection<Sone> getRemoteSones() {