93c99d6fa7af7209d5b268d9df298a43a0359210
[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 partContainerCanReturnContainedParts() {
50                 assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!")));
51         }
52
53         @Test
54         public void partContainerCanReturnSpecificParts() {
55                 assertThat(partContainer.getPart(0), matchesPart("Test!"));
56                 assertThat(partContainer.getPart(1), matchesPart(" More Test!"));
57         }
58
59         @Test
60         public void partContainerKnowsTheNumberOfContainedParts() {
61                 assertThat(partContainer.size(), is(2));
62         }
63
64         @Test
65         public void partContainerReturnsNestedPartsWhenIteratingOverIt() {
66                 addNestedPartContainer();
67                 assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!"), matchesPart(" (nested)")));
68         }
69
70         @Test(expected = NoSuchElementException.class)
71         public void throwsExceptionWhenIteratingOverTheEndOfTheIterator() {
72                 Iterator<Part> parts = partContainer.iterator();
73                 parts.next();
74                 parts.next();
75                 parts.next();
76         }
77
78         @Test
79         public void removingElementsFromTheIteratorDoesNotRemoveThemFromTheContainer() {
80                 Iterator<Part> parts = partContainer.iterator();
81                 parts.remove();
82                 assertThat(partContainer.size(), is(2));
83         }
84
85         @Test
86         public void textOfContainedPartsIsConcatenated() {
87                 addNestedPartContainer();
88                 assertThat(partContainer.getText(), is("Test! More Test! (nested)"));
89         }
90
91         @Test
92         public void partsCanBeRemoved() {
93                 partContainer.removePart(1);
94                 assertThat(partContainer, contains(matchesPart("Test!")));
95         }
96
97         private void addNestedPartContainer() {
98                 Part nestedPart = createPart(" (nested)");
99                 PartContainer nestedContainer = new PartContainer();
100                 nestedContainer.add(nestedPart);
101                 partContainer.add(nestedContainer);
102         }
103
104         private static Matcher<Part> matchesPart(final String text) {
105                 return new TypeSafeMatcher<Part>() {
106                         @Override
107                         protected boolean matchesSafely(Part part) {
108                                 return part.getText().equals(text);
109                         }
110
111                         @Override
112                         public void describeTo(Description description) {
113                                 description.appendValue(text);
114                         }
115                 };
116         }
117
118         private static Part createPart(final String text) {
119                 return new Part() {
120                         @Override
121                         public String getText() {
122                                 return text;
123                         }
124
125                         @Override
126                         public boolean isPlainText() {
127                                 return true;
128                         }
129
130                         @Override
131                         public boolean isFreenetLink() {
132                                 return false;
133                         }
134                 };
135         }
136
137 }