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 @Override
37 public String getCompatibilityVersion() {
38 return "4";
39 }
40
41 private FixedSizeList<String> initFixedSizeList() {
42 final List<String> decoratedList = new ArrayList<>();
43 decoratedList.add("item 1");
44 decoratedList.add("item 2");
45
46 return FixedSizeList.fixedSizeList(decoratedList);
47 }
48
49 @Override
50 public boolean isAddSupported() {
51 return false;
52 }
53
54 @Override
55 public boolean isRemoveSupported() {
56 return false;
57 }
58
59 @Override
60 public List<E> makeFullCollection() {
61 final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
62 return FixedSizeList.fixedSizeList(list);
63 }
64
65
66
67
68
69
70
71
72 @Override
73 public List<E> makeObject() {
74 return FixedSizeList.fixedSizeList(new ArrayList<>());
75 }
76
77 @Test
78 public void testAdd() {
79 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
80
81 assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.add(2, "New Value"));
82 }
83
84 @Test
85 public void testAddAll() {
86 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
87
88 final List<String> addList = new ArrayList<>();
89 addList.add("item 3");
90 addList.add("item 4");
91
92 assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.addAll(2, addList));
93 }
94
95 @Test
96 public void testIsFull() {
97 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
98
99 assertTrue(fixedSizeList.isFull());
100 }
101
102 @Test
103 public void testListAllowsMutationOfUnderlyingCollection() {
104
105 final List<String> decoratedList = new ArrayList<>();
106 decoratedList.add("item 1");
107 decoratedList.add("item 2");
108
109 final FixedSizeList<String> fixedSizeList = FixedSizeList.fixedSizeList(decoratedList);
110 final int sizeBefore = fixedSizeList.size();
111
112 final boolean changed = decoratedList.add("New Value");
113 assertTrue(changed);
114
115 assertEquals(sizeBefore + 1, fixedSizeList.size(),
116 "Modifying an the underlying list is allowed");
117 }
118
119 @Test
120 public void testMaxSize() {
121 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
122
123 assertEquals(2, fixedSizeList.maxSize());
124 }
125
126 @Test
127 public void testRemove() {
128 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
129
130 assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.remove(1));
131 }
132
133 @Test
134 public void testSubList() {
135 final FixedSizeList<String> fixedSizeList = initFixedSizeList();
136
137 final List<String> subFixedSizeList = fixedSizeList.subList(1, 1);
138 assertNotNull(subFixedSizeList);
139 assertEquals(0, subFixedSizeList.size());
140 }
141
142 }