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