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