1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.list;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33
34 public class FixedSizeListTest<E> extends AbstractListTest<E> {
35
36 public FixedSizeListTest() {
37 super(FixedSizeListTest.class.getSimpleName());
38 }
39
40 @Override
41 public String getCompatibilityVersion() {
42 return "4";
43 }
44
45 private FixedSizeList<String> initFixedSizeList() {
46 final List<String> decoratedList = new ArrayList<>();
47 decoratedList.add("item 1");
48 decoratedList.add("item 2");
49
50 return FixedSizeList.fixedSizeList(decoratedList);
51 }
52
53 @Override
54 public boolean isAddSupported() {
55 return false;
56 }
57
58 @Override
59 public boolean isRemoveSupported() {
60 return false;
61 }
62
63 @Override
64 public List<E> makeFullCollection() {
65 final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
66 return FixedSizeList.fixedSizeList(list);
67 }
68
69
70
71
72
73
74
75
76 @Override
77 public List<E> makeObject() {
78 return FixedSizeList.fixedSizeList(new ArrayList<>());
79 }
80
81 @Test
82 public void testAdd() {
83 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
84
85 assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.add(2, "New Value"));
86 }
87
88 @Test
89 public void testAddAll() {
90 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
91
92 final List<String> addList = new ArrayList<>();
93 addList.add("item 3");
94 addList.add("item 4");
95
96 assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.addAll(2, addList));
97 }
98
99 @Test
100 public void testIsFull() {
101 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
102
103 assertTrue(fixedSizeList.isFull());
104 }
105
106 @Test
107 public void testListAllowsMutationOfUnderlyingCollection() {
108
109 final List<String> decoratedList = new ArrayList<>();
110 decoratedList.add("item 1");
111 decoratedList.add("item 2");
112
113 final FixedSizeList<String> fixedSizeList = FixedSizeList.fixedSizeList(decoratedList);
114 final int sizeBefore = fixedSizeList.size();
115
116 final boolean changed = decoratedList.add("New Value");
117 assertTrue(changed);
118
119 assertEquals(sizeBefore + 1, fixedSizeList.size(),
120 "Modifying an the underlying list is allowed");
121 }
122
123 @Test
124 public void testMaxSize() {
125 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
126
127 assertEquals(2, fixedSizeList.maxSize());
128 }
129
130 @Test
131 public void testRemove() {
132 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
133
134 assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.remove(1));
135 }
136
137 @Test
138 public void testSubList() {
139 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
140
141 final List<String> subFixedSizeList = fixedSizeList.subList(1, 1);
142 assertNotNull(subFixedSizeList);
143 assertEquals(0, subFixedSizeList.size());
144 }
145
146 }