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 java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Iterator;
22 import java.util.List;
23
24
25
26
27
28
29
30 public class UnmodifiableListTest<E> extends AbstractListTest<E> {
31
32 public UnmodifiableListTest(final String testName) {
33 super(testName);
34 }
35
36
37 @Override
38 public UnmodifiableList<E> makeObject() {
39 return new UnmodifiableList<>(new ArrayList<E>());
40 }
41
42 @Override
43 public UnmodifiableList<E> makeFullCollection() {
44 final ArrayList<E> list = new ArrayList<>();
45 list.addAll(Arrays.asList(getFullElements()));
46 return new UnmodifiableList<>(list);
47 }
48
49 @Override
50 public boolean isSetSupported() {
51 return false;
52 }
53
54 @Override
55 public boolean isAddSupported() {
56 return false;
57 }
58
59 @Override
60 public boolean isRemoveSupported() {
61 return false;
62 }
63
64
65 protected UnmodifiableList<E> list;
66 protected ArrayList<E> array;
67
68 @SuppressWarnings("unchecked")
69 protected void setupList() {
70 list = makeFullCollection();
71 array = new ArrayList<>();
72 array.add((E) Integer.valueOf(1));
73 }
74
75
76
77
78 public void testUnmodifiable() {
79 setupList();
80 verifyUnmodifiable(list);
81 verifyUnmodifiable(list.subList(0, 2));
82 }
83
84 public void testDecorateFactory() {
85 final List<E> list = makeObject();
86 assertSame(list, UnmodifiableList.unmodifiableList(list));
87
88 try {
89 UnmodifiableList.unmodifiableList(null);
90 fail();
91 } catch (final NullPointerException ex) {}
92 }
93
94 @SuppressWarnings("unchecked")
95 protected void verifyUnmodifiable(final List<E> list) {
96 try {
97 list.add(0, (E) Integer.valueOf(0));
98 fail("Expecting UnsupportedOperationException.");
99 } catch (final UnsupportedOperationException e) {
100
101 }
102 try {
103 list.add((E) Integer.valueOf(0));
104 fail("Expecting UnsupportedOperationException.");
105 } catch (final UnsupportedOperationException e) {
106
107 }
108 try {
109 list.addAll(0, array);
110 fail("Expecting UnsupportedOperationException.");
111 } catch (final UnsupportedOperationException e) {
112
113 }
114 try {
115 list.addAll(array);
116 fail("Expecting UnsupportedOperationException.");
117 } catch (final UnsupportedOperationException e) {
118
119 }
120 try {
121 list.clear();
122 fail("Expecting UnsupportedOperationException.");
123 } catch (final UnsupportedOperationException e) {
124
125 }
126 try {
127 list.remove(0);
128 fail("Expecting UnsupportedOperationException.");
129 } catch (final UnsupportedOperationException e) {
130
131 }
132 try {
133 list.remove(Integer.valueOf(0));
134 fail("Expecting UnsupportedOperationException.");
135 } catch (final UnsupportedOperationException e) {
136
137 }
138 try {
139 list.removeAll(array);
140 fail("Expecting UnsupportedOperationException.");
141 } catch (final UnsupportedOperationException e) {
142
143 }
144 try {
145 list.retainAll(array);
146 fail("Expecting UnsupportedOperationException.");
147 } catch (final UnsupportedOperationException e) {
148
149 }
150 try {
151 list.set(0, (E) Integer.valueOf(0));
152 fail("Expecting UnsupportedOperationException.");
153 } catch (final UnsupportedOperationException e) {
154
155 }
156 }
157
158
159
160
161 public void testUnmodifiableIterator() {
162 setupList();
163 final Iterator<E> iterator = list.iterator();
164 try {
165 iterator.next();
166 iterator.remove();
167 fail("Expecting UnsupportedOperationException.");
168 } catch (final UnsupportedOperationException e) {
169
170 }
171 }
172
173
174
175 @Override
176 public String getCompatibilityVersion() {
177 return "4";
178 }
179
180
181
182
183
184
185
186
187 }