1 package net.pterodactylus.sone.text;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
8 import java.util.Iterator;
9 import java.util.NoSuchElementException;
11 import org.junit.Test;
14 * Unit test for {@link PartContainer}.
16 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
18 public class PartContainerTest {
20 private final PartContainer container = new PartContainer();
23 public void emptyContainerHasSizeZero() {
24 assertThat(container.size(), is(0));
27 @Test(expected = NullPointerException.class)
28 public void canNotAddNullPart() {
33 public void containerWithSinglePartHasSizeOne() {
34 container.add(mock(Part.class));
35 assertThat(container.size(), is(1));
39 public void containerWithSinglePartCanReturnPart() {
40 Part part = mock(Part.class);
42 assertThat(container.getPart(0), is(part));
46 public void containerIsEmptyAfterPartIsAddedAndRemoved() {
47 container.add(mock(Part.class));
48 container.removePart(0);
49 assertThat(container.size(), is(0));
53 public void containerContainsSecondPartIfFirstPartIsRemoved() {
54 container.add(mock(Part.class));
55 Part part = mock(Part.class);
57 container.removePart(0);
58 assertThat(container.getPart(0), is(part));
62 public void textOfContainerPartIsTextOfPartsConcatenated() {
63 container.add(createPartWithText("first"));
64 container.add(createPartWithText("second"));
65 assertThat(container.getText(), is("firstsecond"));
68 private Part createPartWithText(String text) {
69 Part part = mock(Part.class);
70 when(part.getText()).thenReturn(text);
74 @Test(expected = NoSuchElementException.class)
75 public void emptyContainerIteratorThrowsOnNext() {
76 container.iterator().next();
80 public void iteratorIteratesPartsRecursivelyInCorrectOrder() {
81 Part firstPart = mock(Part.class);
82 PartContainer secondPart = new PartContainer();
83 Part thirdPart = mock(Part.class);
84 Part nestedFirstPart = mock(Part.class);
85 Part nestedSecondPart = mock(Part.class);
86 secondPart.add(nestedFirstPart);
87 secondPart.add(nestedSecondPart);
88 container.add(firstPart);
89 container.add(secondPart);
90 container.add(thirdPart);
91 Iterator<Part> parts = container.iterator();
92 assertThat(parts.next(), is(firstPart));
93 assertThat(parts.next(), is(nestedFirstPart));
94 assertThat(parts.next(), is(nestedSecondPart));
95 assertThat(parts.next(), is(thirdPart));
96 assertThat(parts.hasNext(), is(false));