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