Add test for create album page
[Sone.git] / src / test / java / net / pterodactylus / sone / text / PartContainerTest.java
1 package net.pterodactylus.sone.text;
2
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;
7
8 import java.util.Iterator;
9 import java.util.NoSuchElementException;
10
11 import org.junit.Test;
12
13 /**
14  * Unit test for {@link PartContainer}.
15  *
16  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
17  */
18 public class PartContainerTest {
19
20         private final PartContainer container = new PartContainer();
21
22         @Test
23         public void emptyContainerHasSizeZero() {
24                 assertThat(container.size(), is(0));
25         }
26
27         @Test(expected = NullPointerException.class)
28         public void canNotAddNullPart() {
29             container.add(null);
30         }
31
32         @Test
33         public void containerWithSinglePartHasSizeOne() {
34                 container.add(mock(Part.class));
35                 assertThat(container.size(), is(1));
36         }
37
38         @Test
39         public void containerWithSinglePartCanReturnPart() {
40                 Part part = mock(Part.class);
41                 container.add(part);
42                 assertThat(container.getPart(0), is(part));
43         }
44
45         @Test
46         public void containerIsEmptyAfterPartIsAddedAndRemoved() {
47                 container.add(mock(Part.class));
48                 container.removePart(0);
49                 assertThat(container.size(), is(0));
50         }
51
52         @Test
53         public void containerContainsSecondPartIfFirstPartIsRemoved() {
54                 container.add(mock(Part.class));
55                 Part part = mock(Part.class);
56                 container.add(part);
57                 container.removePart(0);
58                 assertThat(container.getPart(0), is(part));
59         }
60
61         @Test
62         public void textOfContainerPartIsTextOfPartsConcatenated() {
63                 container.add(createPartWithText("first"));
64                 container.add(createPartWithText("second"));
65                 assertThat(container.getText(), is("firstsecond"));
66         }
67
68         private Part createPartWithText(String text) {
69                 Part part = mock(Part.class);
70                 when(part.getText()).thenReturn(text);
71                 return part;
72         }
73
74         @Test(expected = NoSuchElementException.class)
75         public void emptyContainerIteratorThrowsOnNext() {
76                 container.iterator().next();
77         }
78
79         @Test
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));
97         }
98
99 }