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