Merge branch 'release-0.9.6'
[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.Post;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.data.impl.IdOnlySone;
31 import net.pterodactylus.sone.database.PostProvider;
32 import net.pterodactylus.sone.database.SoneProvider;
33
34 import com.google.common.base.Function;
35 import com.google.common.base.Optional;
36 import org.junit.Test;
37
38 /**
39  * JUnit test case for {@link SoneTextParser}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class SoneTextParserTest {
44
45         private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
46
47         @SuppressWarnings("static-method")
48         @Test
49         public void testPlainText() throws IOException {
50                 /* check basic operation. */
51                 Iterable<Part> parts = soneTextParser.parse("Test.", null);
52                 assertThat("Parts", parts, notNullValue());
53                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
54
55                 /* check empty lines at start and end. */
56                 parts = soneTextParser.parse("\nTest.\n\n", null);
57                 assertThat("Parts", parts, notNullValue());
58                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
59
60                 /* check duplicate empty lines in the text. */
61                 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
62                 assertThat("Parts", parts, notNullValue());
63                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
64         }
65
66         @Test
67         public void consecutiveLinesAreSeparatedByLinefeed() {
68                 Iterable<Part> parts = soneTextParser.parse("Text.\nText", null);
69                 assertThat("Part Text", convertText(parts), is("Text.\nText"));
70         }
71
72         @Test
73         public void freenetLinksHaveTheFreenetPrefixRemoved() {
74                 Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
75                 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
76         }
77
78         @Test
79         public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
80                 Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
81                 assertThat("Part Text", convertText(parts), is("Text.\n[KSK@gpl.txt|gpl.txt|gpl.txt] and [KSK@gpl.txt|gpl.txt|gpl.txt]"));
82         }
83
84         @Test
85         public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() {
86                 Iterable<Part> parts = soneTextParser.parse("sone://too-short", null);
87                 assertThat("Part Text", convertText(parts), is("sone://too-short"));
88         }
89
90         @Test
91         public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() {
92                 SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null);
93                 Iterable<Part> parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null);
94                 assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"));
95         }
96
97         @Test
98         public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
99                 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
100                 assertThat("Part Text", convertText(parts), is("post://too-short"));
101         }
102
103         @Test
104         public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
105                 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
106                 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
107                 assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"));
108         }
109
110         @Test
111         public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
112                 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
113                 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
114                 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
115         }
116
117         @Test
118         public void nameOfFreenetLinkDoesNotContainUrlParameters() {
119             Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
120                 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|gpl.txt|gpl.txt]"));
121         }
122
123         @Test
124         public void trailingSlashInFreenetLinkIsRemovedForName() {
125                 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
126                 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|gpl.txt|gpl.txt]"));
127         }
128
129         @Test
130         public void lastMetaStringOfFreenetLinkIsUsedAsName() {
131                 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
132                 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING|COPYING]"));
133         }
134
135         @Test
136         public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
137                 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
138                 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm|CHK@qM1nm]"));
139         }
140
141         @Test
142         public void malformedKeyIsRenderedAsPlainText() {
143                 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
144                 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
145         }
146
147         @Test
148         public void httpsLinkHasItsPathsShortened() {
149                 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
150                 assertThat("Part Text", convertText(parts), is("[https://test.test/some-long-path/file.txt|test.test/…/file.txt|test.test/…/file.txt]"));
151         }
152
153         @Test
154         public void httpLinksHaveTheirLastSlashRemoved() {
155             Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
156                 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|test.test/…|test.test/…]"));
157         }
158
159         @Test
160         public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
161                 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
162                 assertThat("Part Text", convertText(parts), is("[http://www.test.test|test.test|test.test]"));
163         }
164
165         @Test
166         public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
167                 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
168                 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|test.test/test.html|test.test/test.html]"));
169         }
170
171         @Test
172         public void hostnameIsKeptIntactIfNotBeginningWithWww() {
173                 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
174                 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|test.test.test/test.html|test.test.test/test.html]"));
175         }
176
177         @Test
178         public void hostnameWithOneDotButNoSlashIsKeptIntact() {
179                 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
180                 assertThat("Part Text", convertText(parts), is("[http://test.test|test.test|test.test]"));
181         }
182
183         @Test
184         public void urlParametersAreRemovedForHttpLinks() {
185                 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
186                 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|test.test|test.test]"));
187         }
188
189         @Test
190         public void emptyStringIsParsedCorrectly() {
191                 Iterable<Part> parts = soneTextParser.parse("", null);
192                 assertThat("Part Text", convertText(parts), is(""));
193         }
194
195         @Test
196         public void linksAreParsedInCorrectOrder() {
197                 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
198                 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
199         }
200
201         @Test
202         public void sskLinkWithoutContextIsNotTrusted() {
203                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
204                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]"));
205         }
206
207         @Test
208         public void sskLinkWithContextWithoutSoneIsNotTrusted() {
209                 SoneTextParserContext context = new SoneTextParserContext(null);
210                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
211                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]"));
212         }
213
214         @Test
215         public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
216                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
217                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
218                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test|test]"));
219         }
220
221         @Test
222         public void sskLinkWithContextWithCorrectSoneIsTrusted() {
223                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
224                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
225                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|trusted|test|test]"));
226         }
227
228         @Test
229         public void uskLinkWithContextWithCorrectSoneIsTrusted() {
230                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
231                 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
232                 assertThat("Part Text", convertText(parts), is("[USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|trusted|test|test]"));
233         }
234
235         @SuppressWarnings("static-method")
236         @Test
237         public void testKSKLinks() throws IOException {
238                 /* check basic links. */
239                 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
240                 assertThat("Parts", parts, notNullValue());
241                 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
242
243                 /* check embedded links. */
244                 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
245                 assertThat("Parts", parts, notNullValue());
246                 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
247
248                 /* check embedded links and line breaks. */
249                 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
250                 assertThat("Parts", parts, notNullValue());
251                 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
252         }
253
254         @SuppressWarnings({ "synthetic-access", "static-method" })
255         @Test
256         public void testEmptyLinesAndSoneLinks() throws IOException {
257                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
258
259                 /* check basic links. */
260                 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
261                 assertThat("Parts", parts, notNullValue());
262                 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
263         }
264
265         @SuppressWarnings({ "synthetic-access", "static-method" })
266         @Test
267         public void testEmpyHttpLinks() throws IOException {
268                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
269
270                 /* check empty http links. */
271                 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
272                 assertThat("Parts", parts, notNullValue());
273                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
274         }
275
276         @Test
277         public void httpLinkWithoutParensEndsAtNextClosingParen() {
278                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
279                 assertThat("Parts", parts, notNullValue());
280                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]) – nice!"));
281         }
282
283         @Test
284         public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
285                 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
286                 assertThat("Parts", parts, notNullValue());
287                 assertThat("Part Text", convertText(parts), is("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test|test]). Nice"));
288         }
289
290         @Test
291         public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
292                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
293                 assertThat("Parts", parts, notNullValue());
294                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc_(def)|example.sone/abc_(def)|example.sone/abc_(def)]) – nice!"));
295         }
296
297         @Test
298         public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
299                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
300                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
301                 assertThat("Parts", parts, notNullValue());
302                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]. Nice!"));
303         }
304
305         @Test
306         public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
307                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
308                 assertThat("Parts", parts, notNullValue());
309                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]... Nice!"));
310         }
311
312         @Test
313         public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
314                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
315                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
316                 assertThat("Parts", parts, notNullValue());
317                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc], nice!"));
318         }
319
320         /**
321          * Converts all given {@link Part}s into a string, validating that the
322          * part’s classes match only the expected classes.
323          *
324          * @param parts
325          *            The parts to convert to text
326          * @param validClasses
327          *            The valid classes; if no classes are given, all classes are
328          *            valid
329          * @return The converted text
330          */
331         private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
332                 StringBuilder text = new StringBuilder();
333                 for (Part part : parts) {
334                         assertThat("Part", part, notNullValue());
335                         if (validClasses.length != 0) {
336                                 assertThat("Part’s class", part.getClass(), isIn(validClasses));
337                         }
338                         if (part instanceof PlainTextPart) {
339                                 text.append(((PlainTextPart) part).getText());
340                         } else if (part instanceof FreenetLinkPart) {
341                                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
342                                 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
343                         } else if (part instanceof LinkPart) {
344                                 LinkPart linkPart = (LinkPart) part;
345                                 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
346                         } else if (part instanceof SonePart) {
347                                 SonePart sonePart = (SonePart) part;
348                                 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
349                         } else if (part instanceof PostPart) {
350                                 PostPart postPart = (PostPart) part;
351                                 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
352                         }
353                 }
354                 return text.toString();
355         }
356
357         /**
358          * Mock Sone provider.
359          *
360          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
361          */
362         private static class TestSoneProvider implements SoneProvider {
363
364                 @Override
365                 public Function<String, Optional<Sone>> soneLoader() {
366                         return new Function<String, Optional<Sone>>() {
367                                 @Override
368                                 public Optional<Sone> apply(String soneId) {
369                                         return getSone(soneId);
370                                 }
371                         };
372                 }
373
374                 /**
375                  * {@inheritDoc}
376                  */
377                 @Override
378                 public Optional<Sone> getSone(final String soneId) {
379                         return Optional.<Sone>of(new IdOnlySone(soneId));
380                 }
381
382                 /**
383                  * {@inheritDocs}
384                  */
385                 @Override
386                 public Collection<Sone> getSones() {
387                         return null;
388                 }
389
390                 /**
391                  * {@inheritDocs}
392                  */
393                 @Override
394                 public Collection<Sone> getLocalSones() {
395                         return null;
396                 }
397
398                 /**
399                  * {@inheritDocs}
400                  */
401                 @Override
402                 public Collection<Sone> getRemoteSones() {
403                         return null;
404                 }
405
406         }
407
408         private static class AbsentSoneProvider extends TestSoneProvider {
409
410                 @Override
411                 public Optional<Sone> getSone(String soneId) {
412                         return Optional.absent();
413                 }
414
415         }
416
417         private static class TestPostProvider implements PostProvider {
418
419                 @Override
420                 public Optional<Post> getPost(final String postId) {
421                         return Optional.<Post>of(new Post() {
422                                 @Override
423                                 public String getId() {
424                                         return postId;
425                                 }
426
427                                 @Override
428                                 public boolean isLoaded() {
429                                         return false;
430                                 }
431
432                                 @Override
433                                 public Sone getSone() {
434                                         return null;
435                                 }
436
437                                 @Override
438                                 public Optional<String> getRecipientId() {
439                                         return null;
440                                 }
441
442                                 @Override
443                                 public Optional<Sone> getRecipient() {
444                                         return null;
445                                 }
446
447                                 @Override
448                                 public long getTime() {
449                                         return 0;
450                                 }
451
452                                 @Override
453                                 public String getText() {
454                                         return "text";
455                                 }
456
457                                 @Override
458                                 public boolean isKnown() {
459                                         return false;
460                                 }
461
462                                 @Override
463                                 public Post setKnown(boolean known) {
464                                         return null;
465                                 }
466                         });
467                 }
468
469                 @Override
470                 public Collection<Post> getPosts(String soneId) {
471                         return null;
472                 }
473
474                 @Override
475                 public Collection<Post> getDirectedPosts(String recipientId) {
476                         return null;
477                 }
478
479         }
480
481         private static class AbsentPostProvider extends TestPostProvider {
482
483                 @Override
484                 public Optional<Post> getPost(String postId) {
485                         return Optional.absent();
486                 }
487
488         }
489
490 }