First basic test case for SoneTextParser.
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
1 /*
2  * Sone - SoneTextParserTest.java - Copyright © 2011 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
25 /**
26  * JUnit test case for {@link SoneTextParser}.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class SoneTextParserTest extends TestCase {
31
32         /**
33          * Tests basic plain-text operation of the parser.
34          *
35          * @throws IOException
36          *             if an I/O error occurs
37          */
38         public void testPlainText() throws IOException {
39                 SoneTextParser soneTextParser = new SoneTextParser(null, null);
40                 Iterable<Part> parts;
41                 StringBuilder text = new StringBuilder();
42
43                 /* check basic operation. */
44                 text.setLength(0);
45                 parts = soneTextParser.parse(null, new StringReader("Test."));
46                 assertNotNull("Parts", parts);
47                 for (Part part : parts) {
48                         assertTrue("Part is PlainTextPart", part instanceof PlainTextPart);
49                         text.append(((PlainTextPart) part).getText());
50                 }
51                 assertEquals("Part Text", "Test.", text.toString());
52
53                 /* check empty lines at start and end. */
54                 text.setLength(0);
55                 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
56                 assertNotNull("Parts", parts);
57                 for (Part part : parts) {
58                         assertTrue("Part is PlainTextPart", part instanceof PlainTextPart);
59                         text.append(((PlainTextPart) part).getText());
60                 }
61                 assertEquals("Part Text", "Test.", text.toString());
62
63                 /* check duplicate empty lines in the text. */
64                 text.setLength(0);
65                 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
66                 assertNotNull("Parts", parts);
67                 for (Part part : parts) {
68                         assertTrue("Part is PlainTextPart", part instanceof PlainTextPart);
69                         text.append(((PlainTextPart) part).getText());
70                 }
71                 assertEquals("Part Text", "Test.\n\nTest.", text.toString());
72         }
73
74 }