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 com.google.common.base.Optional;
27 import junit.framework.TestCase;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.database.SoneProvider;
32 * JUnit test case for {@link SoneTextParser}.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class SoneTextParserTest extends TestCase {
43 * Tests basic plain-text operation of the parser.
46 * if an I/O error occurs
48 @SuppressWarnings("static-method")
49 public void testPlainText() throws IOException {
50 SoneTextParser soneTextParser = new SoneTextParser(null, null);
53 /* check basic operation. */
54 parts = soneTextParser.parse(null, new StringReader("Test."));
55 assertNotNull("Parts", parts);
56 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
58 /* check empty lines at start and end. */
59 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
60 assertNotNull("Parts", parts);
61 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
63 /* check duplicate empty lines in the text. */
64 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
65 assertNotNull("Parts", parts);
66 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
70 * Tests parsing of KSK links.
73 * if an I/O error occurs
75 @SuppressWarnings("static-method")
76 public void testKSKLinks() throws IOException {
77 SoneTextParser soneTextParser = new SoneTextParser(null, null);
80 /* check basic links. */
81 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
82 assertNotNull("Parts", parts);
83 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
85 /* check embedded links. */
86 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
87 assertNotNull("Parts", parts);
88 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
90 /* check embedded links and line breaks. */
91 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
92 assertNotNull("Parts", parts);
93 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));
97 * Test case for a bug that was discovered in 0.6.7.
100 * if an I/O error occurs
102 @SuppressWarnings({ "synthetic-access", "static-method" })
103 public void testEmptyLinesAndSoneLinks() throws IOException {
104 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
105 Iterable<Part> parts;
107 /* check basic links. */
108 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
109 assertNotNull("Parts", parts);
110 assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
114 * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
115 * parsed into a link.
117 * @throws IOException
118 * if an I/O error occurs
120 @SuppressWarnings({ "synthetic-access", "static-method" })
121 public void testEmpyHttpLinks() throws IOException {
122 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
123 Iterable<Part> parts;
125 /* check empty http links. */
126 parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
127 assertNotNull("Parts", parts);
128 assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
136 * Converts all given {@link Part}s into a string, validating that the
137 * part’s classes match only the expected classes.
140 * The parts to convert to text
141 * @param validClasses
142 * The valid classes; if no classes are given, all classes are
144 * @return The converted text
146 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
147 StringBuilder text = new StringBuilder();
148 for (Part part : parts) {
149 assertNotNull("Part", part);
150 boolean classValid = validClasses.length == 0;
151 for (Class<?> validClass : validClasses) {
152 if (validClass.isAssignableFrom(part.getClass())) {
158 fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
160 if (part instanceof PlainTextPart) {
161 text.append(((PlainTextPart) part).getText());
162 } else if (part instanceof FreenetLinkPart) {
163 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
164 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
165 } else if (part instanceof LinkPart) {
166 LinkPart linkPart = (LinkPart) part;
167 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
168 } else if (part instanceof SonePart) {
169 SonePart sonePart = (SonePart) part;
170 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
173 return text.toString();
177 * Mock Sone provider.
179 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
181 private static class TestSoneProvider implements SoneProvider {
187 public Optional<Sone> getSone(final String soneId) {
188 return Optional.<Sone> fromNullable(new Sone(soneId, false) {
194 public String getName() {
204 public Collection<Sone> getSones() {
212 public Collection<Sone> getLocalSones() {
220 public Collection<Sone> getRemoteSones() {