Change parser to better recognize the end of links
[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         @SuppressWarnings("static-method")
44         @Test
45         public void testPlainText() throws IOException {
46                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
47                 Iterable<Part> parts;
48
49                 /* check basic operation. */
50                 parts = soneTextParser.parse("Test.", null);
51                 assertThat("Parts", parts, notNullValue());
52                 assertThat("Part Text", "Test.", is(convertText(parts, PlainTextPart.class)));
53
54                 /* check empty lines at start and end. */
55                 parts = soneTextParser.parse("\nTest.\n\n", null);
56                 assertThat("Parts", parts, notNullValue());
57                 assertThat("Part Text", "Test.", is(convertText(parts, PlainTextPart.class)));
58
59                 /* check duplicate empty lines in the text. */
60                 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
61                 assertThat("Parts", parts, notNullValue());
62                 assertThat("Part Text", "Test.\n\nTest.", is(convertText(parts, PlainTextPart.class)));
63         }
64
65         @SuppressWarnings("static-method")
66         @Test
67         public void testKSKLinks() throws IOException {
68                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
69                 Iterable<Part> parts;
70
71                 /* check basic links. */
72                 parts = soneTextParser.parse("KSK@gpl.txt", null);
73                 assertThat("Parts", parts, notNullValue());
74                 assertThat("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", is(convertText(parts, FreenetLinkPart.class)));
75
76                 /* check embedded links. */
77                 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
78                 assertThat("Parts", parts, notNullValue());
79                 assertThat("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", is(convertText(parts, PlainTextPart.class, FreenetLinkPart.class)));
80
81                 /* check embedded links and line breaks. */
82                 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
83                 assertThat("Parts", parts, notNullValue());
84                 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)));
85         }
86
87         @SuppressWarnings({ "synthetic-access", "static-method" })
88         @Test
89         public void testEmptyLinesAndSoneLinks() throws IOException {
90                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
91                 Iterable<Part> parts;
92
93                 /* check basic links. */
94                 parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
95                 assertThat("Parts", parts, notNullValue());
96                 assertThat("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", is(convertText(parts, PlainTextPart.class, SonePart.class)));
97         }
98
99         @SuppressWarnings({ "synthetic-access", "static-method" })
100         @Test
101         public void testEmpyHttpLinks() throws IOException {
102                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
103                 Iterable<Part> parts;
104
105                 /* check empty http links. */
106                 parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
107                 assertThat("Parts", parts, notNullValue());
108                 assertThat("Part Text", "Some text. Empty link: http:// – nice!", is(convertText(parts, PlainTextPart.class)));
109         }
110
111         @Test
112         public void httpLinkWithoutParensEndsAtNextClosingParen() {
113                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
114                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
115                 assertThat("Parts", parts, notNullValue());
116                 assertThat("Part Text", "Some text (and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]) – nice!", is(convertText(parts, PlainTextPart.class, LinkPart.class)));
117         }
118
119         @Test
120         public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
121                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
122                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
123                 assertThat("Parts", parts, notNullValue());
124                 assertThat("Part Text", "Some text (and a link: [http://example.sone/abc_(def)|example.sone/abc_(def)|example.sone/abc_(def)]) – nice!", is(convertText(parts, PlainTextPart.class, LinkPart.class)));
125         }
126
127         /**
128          * Converts all given {@link Part}s into a string, validating that the
129          * part’s classes match only the expected classes.
130          *
131          * @param parts
132          *            The parts to convert to text
133          * @param validClasses
134          *            The valid classes; if no classes are given, all classes are
135          *            valid
136          * @return The converted text
137          */
138         private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
139                 StringBuilder text = new StringBuilder();
140                 for (Part part : parts) {
141                         assertThat("Part", part, notNullValue());
142                         if (validClasses.length != 0) {
143                                 assertThat("Part’s class", part.getClass(), isIn(validClasses));
144                         }
145                         if (part instanceof PlainTextPart) {
146                                 text.append(((PlainTextPart) part).getText());
147                         } else if (part instanceof FreenetLinkPart) {
148                                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
149                                 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
150                         } else if (part instanceof LinkPart) {
151                                 LinkPart linkPart = (LinkPart) part;
152                                 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
153                         } else if (part instanceof SonePart) {
154                                 SonePart sonePart = (SonePart) part;
155                                 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
156                         }
157                 }
158                 return text.toString();
159         }
160
161         /**
162          * Mock Sone provider.
163          *
164          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
165          */
166         private static class TestSoneProvider implements SoneProvider {
167
168                 @Override
169                 public Function<String, Optional<Sone>> soneLoader() {
170                         return new Function<String, Optional<Sone>>() {
171                                 @Override
172                                 public Optional<Sone> apply(String soneId) {
173                                         return getSone(soneId);
174                                 }
175                         };
176                 }
177
178                 /**
179                  * {@inheritDoc}
180                  */
181                 @Override
182                 public Optional<Sone> getSone(final String soneId) {
183                         return Optional.<Sone>of(new IdOnlySone(soneId));
184                 }
185
186                 /**
187                  * {@inheritDocs}
188                  */
189                 @Override
190                 public Collection<Sone> getSones() {
191                         return null;
192                 }
193
194                 /**
195                  * {@inheritDocs}
196                  */
197                 @Override
198                 public Collection<Sone> getLocalSones() {
199                         return null;
200                 }
201
202                 /**
203                  * {@inheritDocs}
204                  */
205                 @Override
206                 public Collection<Sone> getRemoteSones() {
207                         return null;
208                 }
209
210         }
211
212 }