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.assertSame;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.junit.jupiter.api.Test;
28
29
30
31
32
33 public class UnmodifiableListTest<E> extends AbstractListTest<E> {
34
35 protected UnmodifiableList<E> list;
36
37 protected ArrayList<E> array;
38
39 @Override
40 public String getCompatibilityVersion() {
41 return "4";
42 }
43
44 @Override
45 public boolean isAddSupported() {
46 return false;
47 }
48
49 @Override
50 public boolean isRemoveSupported() {
51 return false;
52 }
53
54 @Override
55 public boolean isSetSupported() {
56 return false;
57 }
58 @Override
59 public UnmodifiableList<E> makeFullCollection() {
60 final ArrayList<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
61 return new UnmodifiableList<>(list);
62 }
63
64 @Override
65 public UnmodifiableList<E> makeObject() {
66 return new UnmodifiableList<>(new ArrayList<>());
67 }
68
69 @SuppressWarnings("unchecked")
70 protected void setupList() {
71 list = makeFullCollection();
72 array = new ArrayList<>();
73 array.add((E) Integer.valueOf(1));
74 }
75
76 @Test
77 public void testDecorateFactory() {
78 final List<E> list = makeObject();
79 assertSame(list, UnmodifiableList.unmodifiableList(list));
80
81 assertThrows(NullPointerException.class, () -> UnmodifiableList.unmodifiableList(null));
82 }
83
84
85
86
87 @Test
88 public void testUnmodifiable() {
89 setupList();
90 verifyUnmodifiable(list);
91 verifyUnmodifiable(list.subList(0, 2));
92 }
93
94
95
96
97 @Test
98 public void testUnmodifiableIterator() {
99 setupList();
100 final Iterator<E> iterator = list.iterator();
101 iterator.next();
102
103 assertThrows(UnsupportedOperationException.class, () -> iterator.remove(),
104 "Expecting UnsupportedOperationException.");
105 }
106
107 @SuppressWarnings("unchecked")
108 protected void verifyUnmodifiable(final List<E> list) {
109 assertThrows(UnsupportedOperationException.class, () -> list.add(0, (E) Integer.valueOf(0)));
110 assertThrows(UnsupportedOperationException.class, () -> list.add((E) Integer.valueOf(0)));
111 assertThrows(UnsupportedOperationException.class, () -> list.addAll(0, array));
112 assertThrows(UnsupportedOperationException.class, () -> list.addAll(array));
113 assertThrows(UnsupportedOperationException.class, () -> list.clear());
114 assertThrows(UnsupportedOperationException.class, () -> list.remove(0));
115 assertThrows(UnsupportedOperationException.class, () -> list.remove(Integer.valueOf(0)));
116 assertThrows(UnsupportedOperationException.class, () -> list.removeAll(array));
117 assertThrows(UnsupportedOperationException.class, () -> list.retainAll(array));
118 assertThrows(UnsupportedOperationException.class, () -> list.set(0, (E) Integer.valueOf(0)));
119 }
120
121
122
123
124
125
126
127
128 }