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 static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.is;
22 import static org.hamcrest.Matchers.notNullValue;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.Arrays;
27 import java.util.Collection;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.data.impl.IdOnlySone;
31 import net.pterodactylus.sone.database.SoneProvider;
33 import com.google.common.base.Function;
34 import com.google.common.base.Optional;
35 import org.junit.Test;
38 * JUnit test case for {@link SoneTextParser}.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class SoneTextParserTest {
49 * Tests basic plain-text operation of the parser.
52 * if an I/O error occurs
55 public void testPlainText() throws IOException {
56 SoneTextParser soneTextParser = new SoneTextParser(null, null);
59 /* check basic operation. */
60 parts = soneTextParser.parse(null, new StringReader("Test."));
61 assertThat(parts, notNullValue());
62 assertThat(convertText(parts, PlainTextPart.class), is("Test."));
64 /* check empty lines at start and end. */
65 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
66 assertThat(parts, notNullValue());
67 assertThat(convertText(parts, PlainTextPart.class), is("Test."));
69 /* check duplicate empty lines in the text. */
70 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
71 assertThat(parts, notNullValue());
72 assertThat(convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
76 * Tests parsing of KSK links.
79 * if an I/O error occurs
82 public void testKSKLinks() throws IOException {
83 SoneTextParser soneTextParser = new SoneTextParser(null, null);
86 /* check basic links. */
87 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
88 assertThat(parts, notNullValue());
89 assertThat(convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
91 /* check embedded links. */
92 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
93 assertThat(parts, notNullValue());
94 assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
96 /* check embedded links and line breaks. */
97 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
98 assertThat(parts, notNullValue());
99 assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class),
100 is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
104 * Test case for a bug that was discovered in 0.6.7.
106 * @throws IOException
107 * if an I/O error occurs
110 public void testEmptyLinesAndSoneLinks() throws IOException {
111 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
112 Iterable<Part> parts;
114 /* check basic links. */
115 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
116 assertThat(parts, notNullValue());
117 assertThat(convertText(parts, PlainTextPart.class, SonePart.class),
118 is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
122 * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
123 * parsed into a link.
125 * @throws IOException
126 * if an I/O error occurs
129 public void testEmpyHttpLinks() throws IOException {
130 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
131 Iterable<Part> parts;
133 /* check empty http links. */
134 parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
135 assertThat(parts, notNullValue());
136 assertThat(convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
144 * Converts all given {@link Part}s into a string, validating that the
145 * part’s classes match only the expected classes.
148 * The parts to convert to text
149 * @param validClasses
150 * The valid classes; if no classes are given, all classes are
152 * @return The converted text
154 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
155 StringBuilder text = new StringBuilder();
156 for (Part part : parts) {
157 assertThat(part, notNullValue());
158 boolean classValid = validClasses.length == 0;
159 for (Class<?> validClass : validClasses) {
160 if (validClass.isAssignableFrom(part.getClass())) {
165 assertThat("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses), classValid, is(true));
166 if (part instanceof PlainTextPart) {
167 text.append(part.getText());
168 } else if (part instanceof FreenetLinkPart) {
169 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
171 .append(freenetLinkPart.getLink())
173 .append(freenetLinkPart.isTrusted() ? "trusted|" : "")
174 .append(freenetLinkPart.getTitle())
176 .append(freenetLinkPart.getText())
178 } else if (part instanceof LinkPart) {
179 LinkPart linkPart = (LinkPart) part;
181 .append(linkPart.getLink())
183 .append(linkPart.getTitle())
185 .append(linkPart.getText())
187 } else if (part instanceof SonePart) {
188 SonePart sonePart = (SonePart) part;
189 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
192 return text.toString();
196 * Mock Sone provider.
198 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
200 private static class TestSoneProvider implements SoneProvider {
203 public Function<String, Optional<Sone>> soneLoader() {
204 return new Function<String, Optional<Sone>>() {
206 public Optional<Sone> apply(String soneId) {
207 return getSone(soneId);
216 public Optional<Sone> getSone(final String soneId) {
217 return Optional.<Sone>of(new IdOnlySone(soneId));
224 public Collection<Sone> getSones() {
232 public Collection<Sone> getLocalSones() {
240 public Collection<Sone> getRemoteSones() {