Rewrite test to use JUnit 4
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
1 /*
2  * Sone - SoneTextParserTest.java - Copyright © 2011–2016 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.isIn;
23 import static org.hamcrest.Matchers.notNullValue;
24
25 import java.io.IOException;
26 import java.util.Collection;
27
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.data.impl.IdOnlySone;
30 import net.pterodactylus.sone.database.SoneProvider;
31
32 import com.google.common.base.Function;
33 import com.google.common.base.Optional;
34 import org.junit.Test;
35
36 /**
37  * JUnit test case for {@link SoneTextParser}.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class SoneTextParserTest {
42
43         //
44         // ACTIONS
45         //
46
47         /**
48          * Tests basic plain-text operation of the parser.
49          *
50          * @throws IOException
51          *             if an I/O error occurs
52          */
53         @SuppressWarnings("static-method")
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("Test.", null);
61                 assertThat("Parts", parts, notNullValue());
62                 assertThat("Part Text", "Test.", is(convertText(parts, PlainTextPart.class)));
63
64                 /* check empty lines at start and end. */
65                 parts = soneTextParser.parse("\nTest.\n\n", null);
66                 assertThat("Parts", parts, notNullValue());
67                 assertThat("Part Text", "Test.", is(convertText(parts, PlainTextPart.class)));
68
69                 /* check duplicate empty lines in the text. */
70                 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
71                 assertThat("Parts", parts, notNullValue());
72                 assertThat("Part Text", "Test.\n\nTest.", is(convertText(parts, PlainTextPart.class)));
73         }
74
75         /**
76          * Tests parsing of KSK links.
77          *
78          * @throws IOException
79          *             if an I/O error occurs
80          */
81         @SuppressWarnings("static-method")
82         @Test
83         public void testKSKLinks() throws IOException {
84                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
85                 Iterable<Part> parts;
86
87                 /* check basic links. */
88                 parts = soneTextParser.parse("KSK@gpl.txt", null);
89                 assertThat("Parts", parts, notNullValue());
90                 assertThat("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", is(convertText(parts, FreenetLinkPart.class)));
91
92                 /* check embedded links. */
93                 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
94                 assertThat("Parts", parts, notNullValue());
95                 assertThat("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", is(convertText(parts, PlainTextPart.class, FreenetLinkPart.class)));
96
97                 /* check embedded links and line breaks. */
98                 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
99                 assertThat("Parts", parts, notNullValue());
100                 assertThat("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]", is(convertText(parts, PlainTextPart.class, FreenetLinkPart.class)));
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         @SuppressWarnings({ "synthetic-access", "static-method" })
110         @Test
111         public void testEmptyLinesAndSoneLinks() throws IOException {
112                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
113                 Iterable<Part> parts;
114
115                 /* check basic links. */
116                 parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
117                 assertThat("Parts", parts, notNullValue());
118                 assertThat("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", is(convertText(parts, PlainTextPart.class, SonePart.class)));
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         @SuppressWarnings({ "synthetic-access", "static-method" })
129         @Test
130         public void testEmpyHttpLinks() throws IOException {
131                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
132                 Iterable<Part> parts;
133
134                 /* check empty http links. */
135                 parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
136                 assertThat("Parts", parts, notNullValue());
137                 assertThat("Part Text", "Some text. Empty link: http:// – nice!", is(convertText(parts, PlainTextPart.class)));
138         }
139
140         //
141         // PRIVATE METHODS
142         //
143
144         /**
145          * Converts all given {@link Part}s into a string, validating that the
146          * part’s classes match only the expected classes.
147          *
148          * @param parts
149          *            The parts to convert to text
150          * @param validClasses
151          *            The valid classes; if no classes are given, all classes are
152          *            valid
153          * @return The converted text
154          */
155         private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
156                 StringBuilder text = new StringBuilder();
157                 for (Part part : parts) {
158                         assertThat("Part", part, notNullValue());
159                         if (validClasses.length != 0) {
160                                 assertThat("Part’s class", part.getClass(), isIn(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 }