Add tests for isFreenetLink() and isPlainText().
[Sone.git] / src / test / java / net / pterodactylus / sone / text / PartContainerTest.java
1 /*
2  * Sone - PartContainerTest.java - Copyright © 2013 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.contains;
22 import static org.hamcrest.Matchers.is;
23
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
26
27 import org.hamcrest.Description;
28 import org.hamcrest.Matcher;
29 import org.hamcrest.TypeSafeMatcher;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 /**
34  * Unit test for {@link PartContainer}.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class PartContainerTest {
39
40         private final PartContainer partContainer = new PartContainer();
41
42         @Before
43         public void setup() {
44                 partContainer.add(createPart("Test!"));
45                 partContainer.add(createPart(" More Test!"));
46         }
47
48         @Test
49         public void partContainerIsNotAPlainTextPart() {
50                 assertThat(partContainer.isPlainText(), is(false));
51         }
52
53         @Test
54         public void partContainerIsNotAFreenetLink() {
55                 assertThat(partContainer.isFreenetLink(), is(false));
56         }
57
58         @Test
59         public void partContainerCanReturnContainedParts() {
60                 assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!")));
61         }
62
63         @Test
64         public void partContainerCanReturnSpecificParts() {
65                 assertThat(partContainer.getPart(0), matchesPart("Test!"));
66                 assertThat(partContainer.getPart(1), matchesPart(" More Test!"));
67         }
68
69         @Test
70         public void partContainerKnowsTheNumberOfContainedParts() {
71                 assertThat(partContainer.size(), is(2));
72         }
73
74         @Test
75         public void partContainerReturnsNestedPartsWhenIteratingOverIt() {
76                 addNestedPartContainer();
77                 assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!"), matchesPart(" (nested)")));
78         }
79
80         @Test(expected = NoSuchElementException.class)
81         public void throwsExceptionWhenIteratingOverTheEndOfTheIterator() {
82                 Iterator<Part> parts = partContainer.iterator();
83                 parts.next();
84                 parts.next();
85                 parts.next();
86         }
87
88         @Test
89         public void removingElementsFromTheIteratorDoesNotRemoveThemFromTheContainer() {
90                 Iterator<Part> parts = partContainer.iterator();
91                 parts.remove();
92                 assertThat(partContainer.size(), is(2));
93         }
94
95         @Test
96         public void textOfContainedPartsIsConcatenated() {
97                 addNestedPartContainer();
98                 assertThat(partContainer.getText(), is("Test! More Test! (nested)"));
99         }
100
101         @Test
102         public void partsCanBeRemoved() {
103                 partContainer.removePart(1);
104                 assertThat(partContainer, contains(matchesPart("Test!")));
105         }
106
107         private void addNestedPartContainer() {
108                 Part nestedPart = createPart(" (nested)");
109                 PartContainer nestedContainer = new PartContainer();
110                 nestedContainer.add(nestedPart);
111                 partContainer.add(nestedContainer);
112         }
113
114         private static Matcher<Part> matchesPart(final String text) {
115                 return new TypeSafeMatcher<Part>() {
116                         @Override
117                         protected boolean matchesSafely(Part part) {
118                                 return part.getText().equals(text);
119                         }
120
121                         @Override
122                         public void describeTo(Description description) {
123                                 description.appendValue(text);
124                         }
125                 };
126         }
127
128         private static Part createPart(final String text) {
129                 return new Part() {
130                         @Override
131                         public String getText() {
132                                 return text;
133                         }
134
135                         @Override
136                         public boolean isPlainText() {
137                                 return true;
138                         }
139
140                         @Override
141                         public boolean isFreenetLink() {
142                                 return false;
143                         }
144                 };
145         }
146
147 }