Merge branch 'release-0.8.3'
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
1 /*
2  * Sone - SoneTextParserTest.java - Copyright © 2011–2012 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 import net.pterodactylus.sone.core.SoneProvider;
25 import net.pterodactylus.sone.data.Sone;
26
27 /**
28  * JUnit test case for {@link SoneTextParser}.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class SoneTextParserTest extends TestCase {
33
34         //
35         // ACTIONS
36         //
37
38         /**
39          * Tests basic plain-text operation of the parser.
40          *
41          * @throws IOException
42          *             if an I/O error occurs
43          */
44         @SuppressWarnings("static-method")
45         public void testPlainText() throws IOException {
46                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
47                 Iterable<Part> parts;
48
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));
53
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));
58
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));
63         }
64
65         /**
66          * Tests parsing of KSK links.
67          *
68          * @throws IOException
69          *             if an I/O error occurs
70          */
71         @SuppressWarnings("static-method")
72         public void testKSKLinks() throws IOException {
73                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
74                 Iterable<Part> parts;
75
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));
80
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));
85
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));
90         }
91
92         /**
93          * Test case for a bug that was discovered in 0.6.7.
94          *
95          * @throws IOException
96          *             if an I/O error occurs
97          */
98         @SuppressWarnings({ "synthetic-access", "static-method" })
99         public void testEmptyLinesAndSoneLinks() throws IOException {
100                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
101                 Iterable<Part> parts;
102
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));
107         }
108
109         //
110         // PRIVATE METHODS
111         //
112
113         /**
114          * Converts all given {@link Part}s into a string, validating that the
115          * part’s classes match only the expected classes.
116          *
117          * @param parts
118          *            The parts to convert to text
119          * @param validClasses
120          *            The valid classes; if no classes are given, all classes are
121          *            valid
122          * @return The converted text
123          */
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())) {
131                                         classValid = true;
132                                         break;
133                                 }
134                         }
135                         if (!classValid) {
136                                 assertEquals("Part’s Class", null, part.getClass());
137                         }
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(']');
149                         }
150                 }
151                 return text.toString();
152         }
153
154         /**
155          * Mock Sone provider.
156          *
157          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
158          */
159         private static class TestSoneProvider implements SoneProvider {
160
161                 /**
162                  * {@inheritDoc}
163                  */
164                 @Override
165                 public Sone getSone(final String soneId, boolean create) {
166                         return new Sone(soneId) {
167
168                                 /**
169                                  * {@inheritDoc}
170                                  */
171                                 @Override
172                                 public String getName() {
173                                         return soneId;
174                                 }
175                         };
176                 }
177
178         }
179
180 }