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 static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.is;
22 import static org.hamcrest.Matchers.isIn;
23 import static org.hamcrest.Matchers.notNullValue;
25 import java.io.IOException;
26 import java.util.Collection;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.data.impl.IdOnlySone;
30 import net.pterodactylus.sone.database.SoneProvider;
32 import com.google.common.base.Function;
33 import com.google.common.base.Optional;
34 import org.junit.Test;
37 * JUnit test case for {@link SoneTextParser}.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class SoneTextParserTest {
48 * Tests basic plain-text operation of the parser.
51 * if an I/O error occurs
53 @SuppressWarnings("static-method")
55 public void testPlainText() throws IOException {
56 SoneTextParser soneTextParser = new SoneTextParser(null, null);
59 /* check basic operation. */
60 parts = soneTextParser.parse("Test.", null);
61 assertThat("Parts", parts, notNullValue());
62 assertThat("Part Text", "Test.", is(convertText(parts, PlainTextPart.class)));
64 /* check empty lines at start and end. */
65 parts = soneTextParser.parse("\nTest.\n\n", null);
66 assertThat("Parts", parts, notNullValue());
67 assertThat("Part Text", "Test.", is(convertText(parts, PlainTextPart.class)));
69 /* check duplicate empty lines in the text. */
70 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
71 assertThat("Parts", parts, notNullValue());
72 assertThat("Part Text", "Test.\n\nTest.", is(convertText(parts, PlainTextPart.class)));
76 * Tests parsing of KSK links.
79 * if an I/O error occurs
81 @SuppressWarnings("static-method")
83 public void testKSKLinks() throws IOException {
84 SoneTextParser soneTextParser = new SoneTextParser(null, null);
87 /* check basic links. */
88 parts = soneTextParser.parse("KSK@gpl.txt", null);
89 assertThat("Parts", parts, notNullValue());
90 assertThat("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", is(convertText(parts, FreenetLinkPart.class)));
92 /* check embedded links. */
93 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
94 assertThat("Parts", parts, notNullValue());
95 assertThat("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", is(convertText(parts, PlainTextPart.class, FreenetLinkPart.class)));
97 /* check embedded links and line breaks. */
98 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
99 assertThat("Parts", parts, notNullValue());
100 assertThat("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]", is(convertText(parts, PlainTextPart.class, FreenetLinkPart.class)));
104 * Test case for a bug that was discovered in 0.6.7.
106 * @throws IOException
107 * if an I/O error occurs
109 @SuppressWarnings({ "synthetic-access", "static-method" })
111 public void testEmptyLinesAndSoneLinks() throws IOException {
112 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
113 Iterable<Part> parts;
115 /* check basic links. */
116 parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
117 assertThat("Parts", parts, notNullValue());
118 assertThat("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", is(convertText(parts, PlainTextPart.class, SonePart.class)));
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
128 @SuppressWarnings({ "synthetic-access", "static-method" })
130 public void testEmpyHttpLinks() throws IOException {
131 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
132 Iterable<Part> parts;
134 /* check empty http links. */
135 parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
136 assertThat("Parts", parts, notNullValue());
137 assertThat("Part Text", "Some text. Empty link: http:// – nice!", is(convertText(parts, PlainTextPart.class)));
145 * Converts all given {@link Part}s into a string, validating that the
146 * part’s classes match only the expected classes.
149 * The parts to convert to text
150 * @param validClasses
151 * The valid classes; if no classes are given, all classes are
153 * @return The converted text
155 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
156 StringBuilder text = new StringBuilder();
157 for (Part part : parts) {
158 assertThat("Part", part, notNullValue());
159 if (validClasses.length != 0) {
160 assertThat("Part’s class", part.getClass(), isIn(validClasses));
162 if (part instanceof PlainTextPart) {
163 text.append(((PlainTextPart) part).getText());
164 } else if (part instanceof FreenetLinkPart) {
165 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
166 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
167 } else if (part instanceof LinkPart) {
168 LinkPart linkPart = (LinkPart) part;
169 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
170 } else if (part instanceof SonePart) {
171 SonePart sonePart = (SonePart) part;
172 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
175 return text.toString();
179 * Mock Sone provider.
181 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
183 private static class TestSoneProvider implements SoneProvider {
186 public Function<String, Optional<Sone>> soneLoader() {
187 return new Function<String, Optional<Sone>>() {
189 public Optional<Sone> apply(String soneId) {
190 return getSone(soneId);
199 public Optional<Sone> getSone(final String soneId) {
200 return Optional.<Sone>of(new IdOnlySone(soneId));
207 public Collection<Sone> getSones() {
215 public Collection<Sone> getLocalSones() {
223 public Collection<Sone> getRemoteSones() {