Add code comment.
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
1 /*
2  * Sone - SoneTextParserTest.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.text;
19
20 import java.io.IOException;
21 import java.io.StringReader;
22
23 import junit.framework.TestCase;
24
25 /**
26  * JUnit test case for {@link SoneTextParser}.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class SoneTextParserTest extends TestCase {
31
32         //
33         // ACTIONS
34         //
35
36         /**
37          * Tests basic plain-text operation of the parser.
38          *
39          * @throws IOException
40          *             if an I/O error occurs
41          */
42         public void testPlainText() throws IOException {
43                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
44                 Iterable<Part> parts;
45
46                 /* check basic operation. */
47                 parts = soneTextParser.parse(null, new StringReader("Test."));
48                 assertNotNull("Parts", parts);
49                 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
50
51                 /* check empty lines at start and end. */
52                 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
53                 assertNotNull("Parts", parts);
54                 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
55
56                 /* check duplicate empty lines in the text. */
57                 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
58                 assertNotNull("Parts", parts);
59                 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
60         }
61
62         /**
63          * Tests parsing of KSK links.
64          *
65          * @throws IOException
66          *             if an I/O error occurs
67          */
68         public void testKSKLinks() throws IOException {
69                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
70                 Iterable<Part> parts;
71
72                 /* check basic links. */
73                 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
74                 assertNotNull("Parts", parts);
75                 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
76
77                 /* check embedded links. */
78                 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
79                 assertNotNull("Parts", parts);
80                 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
81
82                 /* check embedded links and line breaks. */
83                 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
84                 assertNotNull("Parts", parts);
85                 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));
86         }
87
88         //
89         // PRIVATE METHODS
90         //
91
92         /**
93          * Converts all given {@link Part}s into a string, validating that the
94          * part’s classes match only the expected classes.
95          *
96          * @param parts
97          *            The parts to convert to text
98          * @param validClasses
99          *            The valid classes; if no classes are given, all classes are
100          *            valid
101          * @return The converted text
102          */
103         private String convertText(Iterable<Part> parts, Class<?>... validClasses) {
104                 StringBuilder text = new StringBuilder();
105                 for (Part part : parts) {
106                         assertNotNull("Part", part);
107                         boolean classValid = validClasses.length == 0;
108                         for (Class<?> validClass : validClasses) {
109                                 if (validClass.isAssignableFrom(part.getClass())) {
110                                         classValid = true;
111                                         break;
112                                 }
113                         }
114                         if (!classValid) {
115                                 assertEquals("Part’s Class", null, part.getClass());
116                         }
117                         if (part instanceof PlainTextPart) {
118                                 text.append(((PlainTextPart) part).getText());
119                         } else if (part instanceof FreenetLinkPart) {
120                                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
121                                 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
122                         } else if (part instanceof LinkPart) {
123                                 LinkPart linkPart = (LinkPart) part;
124                                 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
125                         }
126                 }
127                 return text.toString();
128         }
129
130 }