2 * Sone - PartContainerTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.text;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.contains;
22 import static org.hamcrest.Matchers.is;
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
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;
34 * Unit test for {@link PartContainer}.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class PartContainerTest {
40 private final PartContainer partContainer = new PartContainer();
44 partContainer.add(createPart("Test!"));
45 partContainer.add(createPart(" More Test!"));
49 public void partContainerIsNotAPlainTextPart() {
50 assertThat(partContainer.isPlainText(), is(false));
54 public void partContainerIsNotAFreenetLink() {
55 assertThat(partContainer.isFreenetLink(), is(false));
59 public void partContainerCanReturnContainedParts() {
60 assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!")));
64 public void partContainerCanReturnSpecificParts() {
65 assertThat(partContainer.getPart(0), matchesPart("Test!"));
66 assertThat(partContainer.getPart(1), matchesPart(" More Test!"));
70 public void partContainerKnowsTheNumberOfContainedParts() {
71 assertThat(partContainer.size(), is(2));
75 public void partContainerReturnsNestedPartsWhenIteratingOverIt() {
76 addNestedPartContainer();
77 assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!"), matchesPart(" (nested)")));
80 @Test(expected = NoSuchElementException.class)
81 public void throwsExceptionWhenIteratingOverTheEndOfTheIterator() {
82 Iterator<Part> parts = partContainer.iterator();
89 public void removingElementsFromTheIteratorDoesNotRemoveThemFromTheContainer() {
90 Iterator<Part> parts = partContainer.iterator();
92 assertThat(partContainer.size(), is(2));
96 public void textOfContainedPartsIsConcatenated() {
97 addNestedPartContainer();
98 assertThat(partContainer.getText(), is("Test! More Test! (nested)"));
102 public void partsCanBeRemoved() {
103 partContainer.removePart(1);
104 assertThat(partContainer, contains(matchesPart("Test!")));
107 private void addNestedPartContainer() {
108 Part nestedPart = createPart(" (nested)");
109 PartContainer nestedContainer = new PartContainer();
110 nestedContainer.add(nestedPart);
111 partContainer.add(nestedContainer);
114 private static Matcher<Part> matchesPart(final String text) {
115 return new TypeSafeMatcher<Part>() {
117 protected boolean matchesSafely(Part part) {
118 return part.getText().equals(text);
122 public void describeTo(Description description) {
123 description.appendValue(text);
128 private static Part createPart(final String text) {
131 public String getText() {
136 public boolean isPlainText() {
141 public boolean isFreenetLink() {