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