72860012d04ea0c66e8fd0ca495ce8a5e74b47be
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
1 /*
2  * Sone - SoneTextParserTest.java - Copyright © 2011–2013 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 import java.util.Arrays;
23 import java.util.Collection;
24
25 import com.google.common.base.Optional;
26
27 import junit.framework.TestCase;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.database.SoneProvider;
30
31 /**
32  * JUnit test case for {@link SoneTextParser}.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class SoneTextParserTest extends TestCase {
37
38         //
39         // ACTIONS
40         //
41
42         /**
43          * Tests basic plain-text operation of the parser.
44          *
45          * @throws IOException
46          *             if an I/O error occurs
47          */
48         @SuppressWarnings("static-method")
49         public void testPlainText() throws IOException {
50                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
51                 Iterable<Part> parts;
52
53                 /* check basic operation. */
54                 parts = soneTextParser.parse(null, new StringReader("Test."));
55                 assertNotNull("Parts", parts);
56                 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
57
58                 /* check empty lines at start and end. */
59                 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
60                 assertNotNull("Parts", parts);
61                 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
62
63                 /* check duplicate empty lines in the text. */
64                 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
65                 assertNotNull("Parts", parts);
66                 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
67         }
68
69         /**
70          * Tests parsing of KSK links.
71          *
72          * @throws IOException
73          *             if an I/O error occurs
74          */
75         @SuppressWarnings("static-method")
76         public void testKSKLinks() throws IOException {
77                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
78                 Iterable<Part> parts;
79
80                 /* check basic links. */
81                 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
82                 assertNotNull("Parts", parts);
83                 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
84
85                 /* check embedded links. */
86                 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
87                 assertNotNull("Parts", parts);
88                 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
89
90                 /* check embedded links and line breaks. */
91                 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
92                 assertNotNull("Parts", parts);
93                 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));
94         }
95
96         /**
97          * Test case for a bug that was discovered in 0.6.7.
98          *
99          * @throws IOException
100          *             if an I/O error occurs
101          */
102         @SuppressWarnings({ "synthetic-access", "static-method" })
103         public void testEmptyLinesAndSoneLinks() throws IOException {
104                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
105                 Iterable<Part> parts;
106
107                 /* check basic links. */
108                 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
109                 assertNotNull("Parts", parts);
110                 assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
111         }
112
113         /**
114          * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
115          * parsed into a link.
116          *
117          * @throws IOException
118          *             if an I/O error occurs
119          */
120         @SuppressWarnings({ "synthetic-access", "static-method" })
121         public void testEmpyHttpLinks() throws IOException {
122                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
123                 Iterable<Part> parts;
124
125                 /* check empty http links. */
126                 parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
127                 assertNotNull("Parts", parts);
128                 assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
129         }
130
131         //
132         // PRIVATE METHODS
133         //
134
135         /**
136          * Converts all given {@link Part}s into a string, validating that the
137          * part’s classes match only the expected classes.
138          *
139          * @param parts
140          *            The parts to convert to text
141          * @param validClasses
142          *            The valid classes; if no classes are given, all classes are
143          *            valid
144          * @return The converted text
145          */
146         private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
147                 StringBuilder text = new StringBuilder();
148                 for (Part part : parts) {
149                         assertNotNull("Part", part);
150                         boolean classValid = validClasses.length == 0;
151                         for (Class<?> validClass : validClasses) {
152                                 if (validClass.isAssignableFrom(part.getClass())) {
153                                         classValid = true;
154                                         break;
155                                 }
156                         }
157                         if (!classValid) {
158                                 fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
159                         }
160                         if (part instanceof PlainTextPart) {
161                                 text.append(((PlainTextPart) part).getText());
162                         } else if (part instanceof FreenetLinkPart) {
163                                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
164                                 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
165                         } else if (part instanceof LinkPart) {
166                                 LinkPart linkPart = (LinkPart) part;
167                                 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
168                         } else if (part instanceof SonePart) {
169                                 SonePart sonePart = (SonePart) part;
170                                 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
171                         }
172                 }
173                 return text.toString();
174         }
175
176         /**
177          * Mock Sone provider.
178          *
179          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
180          */
181         private static class TestSoneProvider implements SoneProvider {
182
183                 /**
184                  * {@inheritDoc}
185                  */
186                 @Override
187                 public Optional<Sone> getSone(final String soneId) {
188                         return Optional.<Sone> fromNullable(new Sone(soneId, false) {
189
190                                 /**
191                                  * {@inheritDoc}
192                                  */
193                                 @Override
194                                 public String getName() {
195                                         return soneId;
196                                 }
197                         });
198                 }
199
200                 /**
201                  * {@inheritDocs}
202                  */
203                 @Override
204                 public Collection<Sone> getSones() {
205                         return null;
206                 }
207
208                 /**
209                  * {@inheritDocs}
210                  */
211                 @Override
212                 public Collection<Sone> getLocalSones() {
213                         return null;
214                 }
215
216                 /**
217                  * {@inheritDocs}
218                  */
219                 @Override
220                 public Collection<Sone> getRemoteSones() {
221                         return null;
222                 }
223
224         }
225
226 }