2 * Sone - SoneTextParserTest.java - Copyright © 2011 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;
26 * JUnit test case for {@link SoneTextParser}.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public class SoneTextParserTest extends TestCase {
33 * Tests basic plain-text operation of the parser.
36 * if an I/O error occurs
38 public void testPlainText() throws IOException {
39 SoneTextParser soneTextParser = new SoneTextParser(null, null);
42 /* check basic operation. */
43 parts = soneTextParser.parse(null, new StringReader("Test."));
44 assertNotNull("Parts", parts);
45 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
47 /* check empty lines at start and end. */
48 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
49 assertNotNull("Parts", parts);
50 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
52 /* check duplicate empty lines in the text. */
53 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
54 assertNotNull("Parts", parts);
55 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
59 * Tests parsing of KSK links.
62 * if an I/O error occurs
64 public void testKSKLinks() throws IOException {
65 SoneTextParser soneTextParser = new SoneTextParser(null, null);
68 /* check basic links. */
69 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
70 assertNotNull("Parts", parts);
71 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
73 /* check embedded links. */
74 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
75 assertNotNull("Parts", parts);
76 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
78 /* check embedded links and line breaks. */
79 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
80 assertNotNull("Parts", parts);
81 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));
89 * Converts all given {@link Part}s into a string, validating that the
90 * part’s classes match only the expected classes.
93 * The parts to convert to text
95 * The valid classes; if no classes are given, all classes are
97 * @return The converted text
99 private String convertText(Iterable<Part> parts, Class<?>... validClasses) {
100 StringBuilder text = new StringBuilder();
101 for (Part part : parts) {
102 assertNotNull("Part", part);
103 boolean classValid = validClasses.length == 0;
104 for (Class<?> validClass : validClasses) {
105 if (validClass.isAssignableFrom(part.getClass())) {
111 assertEquals("Part’s Class", null, part.getClass());
113 if (part instanceof PlainTextPart) {
114 text.append(((PlainTextPart) part).getText());
115 } else if (part instanceof FreenetLinkPart) {
116 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
117 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
118 } else if (part instanceof LinkPart) {
119 LinkPart linkPart = (LinkPart) part;
120 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
123 return text.toString();