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 java.io.IOException;
21 import java.util.Arrays;
22 import java.util.Collection;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.data.impl.IdOnlySone;
26 import net.pterodactylus.sone.database.SoneProvider;
28 import com.google.common.base.Function;
29 import com.google.common.base.Optional;
30 import junit.framework.TestCase;
33 * JUnit test case for {@link SoneTextParser}.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class SoneTextParserTest extends TestCase {
44 * Tests basic plain-text operation of the parser.
47 * if an I/O error occurs
49 @SuppressWarnings("static-method")
50 public void testPlainText() throws IOException {
51 SoneTextParser soneTextParser = new SoneTextParser(null, null);
54 /* check basic operation. */
55 parts = soneTextParser.parse("Test.", null);
56 assertNotNull("Parts", parts);
57 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
59 /* check empty lines at start and end. */
60 parts = soneTextParser.parse("\nTest.\n\n", null);
61 assertNotNull("Parts", parts);
62 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
64 /* check duplicate empty lines in the text. */
65 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
66 assertNotNull("Parts", parts);
67 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
71 * Tests parsing of KSK links.
74 * if an I/O error occurs
76 @SuppressWarnings("static-method")
77 public void testKSKLinks() throws IOException {
78 SoneTextParser soneTextParser = new SoneTextParser(null, null);
81 /* check basic links. */
82 parts = soneTextParser.parse("KSK@gpl.txt", null);
83 assertNotNull("Parts", parts);
84 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
86 /* check embedded links. */
87 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
88 assertNotNull("Parts", parts);
89 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
91 /* check embedded links and line breaks. */
92 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
93 assertNotNull("Parts", parts);
94 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));
98 * Test case for a bug that was discovered in 0.6.7.
100 * @throws IOException
101 * if an I/O error occurs
103 @SuppressWarnings({ "synthetic-access", "static-method" })
104 public void testEmptyLinesAndSoneLinks() throws IOException {
105 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
106 Iterable<Part> parts;
108 /* check basic links. */
109 parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
110 assertNotNull("Parts", parts);
111 assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
115 * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
116 * parsed into a link.
118 * @throws IOException
119 * if an I/O error occurs
121 @SuppressWarnings({ "synthetic-access", "static-method" })
122 public void testEmpyHttpLinks() throws IOException {
123 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
124 Iterable<Part> parts;
126 /* check empty http links. */
127 parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
128 assertNotNull("Parts", parts);
129 assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
137 * Converts all given {@link Part}s into a string, validating that the
138 * part’s classes match only the expected classes.
141 * The parts to convert to text
142 * @param validClasses
143 * The valid classes; if no classes are given, all classes are
145 * @return The converted text
147 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
148 StringBuilder text = new StringBuilder();
149 for (Part part : parts) {
150 assertNotNull("Part", part);
151 boolean classValid = validClasses.length == 0;
152 for (Class<?> validClass : validClasses) {
153 if (validClass.isAssignableFrom(part.getClass())) {
159 fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
161 if (part instanceof PlainTextPart) {
162 text.append(((PlainTextPart) part).getText());
163 } else if (part instanceof FreenetLinkPart) {
164 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
165 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
166 } else if (part instanceof LinkPart) {
167 LinkPart linkPart = (LinkPart) part;
168 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
169 } else if (part instanceof SonePart) {
170 SonePart sonePart = (SonePart) part;
171 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
174 return text.toString();
178 * Mock Sone provider.
180 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
182 private static class TestSoneProvider implements SoneProvider {
185 public Function<String, Optional<Sone>> soneLoader() {
186 return new Function<String, Optional<Sone>>() {
188 public Optional<Sone> apply(String soneId) {
189 return getSone(soneId);
198 public Optional<Sone> getSone(final String soneId) {
199 return Optional.<Sone>of(new IdOnlySone(soneId));
206 public Collection<Sone> getSones() {
214 public Collection<Sone> getLocalSones() {
222 public Collection<Sone> getRemoteSones() {