2 * Sone - SoneTextParserTest.java - Copyright © 2011–2012 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;
23 import junit.framework.TestCase;
24 import net.pterodactylus.sone.core.SoneProvider;
25 import net.pterodactylus.sone.data.Sone;
28 * JUnit test case for {@link SoneTextParser}.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class SoneTextParserTest extends TestCase {
39 * Tests basic plain-text operation of the parser.
42 * if an I/O error occurs
44 @SuppressWarnings("static-method")
45 public void testPlainText() throws IOException {
46 SoneTextParser soneTextParser = new SoneTextParser(null, null);
49 /* check basic operation. */
50 parts = soneTextParser.parse(null, new StringReader("Test."));
51 assertNotNull("Parts", parts);
52 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
54 /* check empty lines at start and end. */
55 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
56 assertNotNull("Parts", parts);
57 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
59 /* check duplicate empty lines in the text. */
60 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
61 assertNotNull("Parts", parts);
62 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
66 * Tests parsing of KSK links.
69 * if an I/O error occurs
71 @SuppressWarnings("static-method")
72 public void testKSKLinks() throws IOException {
73 SoneTextParser soneTextParser = new SoneTextParser(null, null);
76 /* check basic links. */
77 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
78 assertNotNull("Parts", parts);
79 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
81 /* check embedded links. */
82 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
83 assertNotNull("Parts", parts);
84 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
86 /* check embedded links and line breaks. */
87 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
88 assertNotNull("Parts", parts);
89 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));
93 * Test case for a bug that was discovered in 0.6.7.
96 * if an I/O error occurs
98 @SuppressWarnings({ "synthetic-access", "static-method" })
99 public void testEmptyLinesAndSoneLinks() throws IOException {
100 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
101 Iterable<Part> parts;
103 /* check basic links. */
104 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
105 assertNotNull("Parts", parts);
106 assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
114 * Converts all given {@link Part}s into a string, validating that the
115 * part’s classes match only the expected classes.
118 * The parts to convert to text
119 * @param validClasses
120 * The valid classes; if no classes are given, all classes are
122 * @return The converted text
124 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
125 StringBuilder text = new StringBuilder();
126 for (Part part : parts) {
127 assertNotNull("Part", part);
128 boolean classValid = validClasses.length == 0;
129 for (Class<?> validClass : validClasses) {
130 if (validClass.isAssignableFrom(part.getClass())) {
136 fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
138 if (part instanceof PlainTextPart) {
139 text.append(((PlainTextPart) part).getText());
140 } else if (part instanceof FreenetLinkPart) {
141 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
142 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
143 } else if (part instanceof LinkPart) {
144 LinkPart linkPart = (LinkPart) part;
145 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
146 } else if (part instanceof SonePart) {
147 SonePart sonePart = (SonePart) part;
148 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
151 return text.toString();
155 * Mock Sone provider.
157 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
159 private static class TestSoneProvider implements SoneProvider {
165 public Sone getSone(final String soneId, boolean create) {
166 return new Sone(soneId) {
172 public String getName() {