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