View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.collections;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.util.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  class ControlledArrayList_Test extends TestBase {
27  
28  	//-----------------------------------------------------------------------------------------------------------------
29  	// test - Basic tests
30  	//-----------------------------------------------------------------------------------------------------------------
31  
32  	@Test void a01_constructors() {
33  		var x = new ControlledArrayList<>(false);
34  		assertTrue(x.isModifiable());
35  
36  		x = new ControlledArrayList<>(true);
37  		assertFalse(x.isModifiable());
38  
39  		x = new ControlledArrayList<>(false, Arrays.asList(1));
40  		assertTrue(x.isModifiable());
41  
42  		x = new ControlledArrayList<>(true, Arrays.asList(1));
43  		assertFalse(x.isModifiable());
44  	}
45  
46  	@Test void a02_basicMethods() {
47  		var x1 = new ControlledArrayList<>(false, Arrays.asList(1));
48  		var x2 = new ControlledArrayList<>(true, Arrays.asList(1));
49  
50  		x1.set(0, 2);
51  		assertThrows(UnsupportedOperationException.class, () -> x2.set(0, 2));
52  		x2.overrideSet(0, 2);
53  		assertEquals(x2, x1);
54  
55  		x1.add(0, 2);
56  		assertThrows(UnsupportedOperationException.class, () -> x2.add(0, 2));
57  		x2.overrideAdd(0, 2);
58  		assertEquals(x2, x1);
59  
60  		x1.remove(0);
61  		assertThrows(UnsupportedOperationException.class, () -> x2.remove(0));
62  		x2.overrideRemove(0);
63  		assertEquals(x2, x1);
64  
65  		x1.addAll(0, Arrays.asList(3));
66  		assertThrows(UnsupportedOperationException.class, () -> x2.addAll(0, Arrays.asList(3)));
67  		x2.overrideAddAll(0, Arrays.asList(3));
68  		assertEquals(x2, x1);
69  
70  		x1.replaceAll(x -> x);
71  		assertThrows(UnsupportedOperationException.class, () -> x2.replaceAll(x -> x));
72  		x2.overrideReplaceAll(x -> x);
73  		assertEquals(x2, x1);
74  
75  		x1.sort(null);
76  		assertThrows(UnsupportedOperationException.class, () -> x2.sort(null));
77  		x2.overrideSort(null);
78  		assertEquals(x2, x1);
79  
80  		x1.add(1);
81  		assertThrows(UnsupportedOperationException.class, () -> x2.add(1));
82  		x2.overrideAdd(1);
83  		assertEquals(x2, x1);
84  
85  		x1.remove((Integer)1);
86  		assertThrows(UnsupportedOperationException.class, () -> x2.remove((Integer)1));
87  		x2.overrideRemove((Integer)1);
88  		assertEquals(x2, x1);
89  
90  		x1.addAll(Arrays.asList(3));
91  		assertThrows(UnsupportedOperationException.class, () -> x2.addAll(Arrays.asList(3)));
92  		x2.overrideAddAll(Arrays.asList(3));
93  		assertEquals(x2, x1);
94  
95  		x1.removeAll(Arrays.asList(3));
96  		assertThrows(UnsupportedOperationException.class, () -> x2.removeAll(Arrays.asList(3)));
97  		x2.overrideRemoveAll(Arrays.asList(3));
98  		assertEquals(x2, x1);
99  
100 		x1.retainAll(Arrays.asList(2));
101 		assertThrows(UnsupportedOperationException.class, () -> x2.retainAll(Arrays.asList(2)));
102 		x2.overrideRetainAll(Arrays.asList(2));
103 		assertEquals(x2, x1);
104 
105 		x1.clear();
106 		assertThrows(UnsupportedOperationException.class, x2::clear);
107 		x2.overrideClear();
108 		assertEquals(x2, x1);
109 
110 		x1.add(1);
111 		x2.overrideAdd(1);
112 
113 		x1.removeIf(x -> x == 1);
114 		assertThrows(UnsupportedOperationException.class, () -> x2.removeIf(x -> x == 1));
115 		x2.overrideRemoveIf(x -> x == 1);
116 		assertEquals(x2, x1);
117 
118 		x1.add(1);
119 		x2.overrideAdd(1);
120 
121 		var x1a = (ControlledArrayList<Integer>) x1.subList(0, 0);
122 		var x2a = (ControlledArrayList<Integer>) x2.subList(0, 0);
123 		assertTrue(x1a.isModifiable());
124 		assertFalse(x2a.isModifiable());
125 	}
126 
127 	@Test void a03_iterator() {
128 		var x1 = new ControlledArrayList<>(false, Arrays.asList(1));
129 		var x2 = new ControlledArrayList<>(true, Arrays.asList(1));
130 
131 		var i1 = x1.iterator();
132 		var i2 = x2.iterator();
133 
134 		assertTrue(i1.hasNext());
135 		assertTrue(i2.hasNext());
136 
137 		assertEquals(1, i1.next().intValue());
138 		assertEquals(1, i2.next().intValue());
139 
140 		i1.remove();
141 		assertThrows(UnsupportedOperationException.class, i2::remove);
142 
143 		i1.forEachRemaining(x -> {});
144 		i2.forEachRemaining(x -> {});
145 	}
146 
147 	@Test void a04_listIterator() {
148 		var x1 = new ControlledArrayList<>(false, Arrays.asList(1));
149 		var x2 = new ControlledArrayList<>(true, Arrays.asList(1));
150 
151 		var i1a = x1.listIterator();
152 		var i2a = x2.listIterator();
153 
154 		assertTrue(i1a.hasNext());
155 		assertTrue(i2a.hasNext());
156 
157 		assertEquals(1, i1a.next().intValue());
158 		assertEquals(1, i2a.next().intValue());
159 
160 		assertTrue(i1a.hasPrevious());
161 		assertTrue(i2a.hasPrevious());
162 
163 		assertEquals(1, i1a.nextIndex());
164 		assertEquals(1, i2a.nextIndex());
165 
166 		assertEquals(0, i1a.previousIndex());
167 		assertEquals(0, i2a.previousIndex());
168 
169 		i1a.previous();
170 		i2a.previous();
171 
172 		i1a.set(1);
173 		assertThrows(UnsupportedOperationException.class, () -> i2a.set(1));
174 
175 		i1a.add(1);
176 		assertThrows(UnsupportedOperationException.class, () -> i2a.add(1));
177 
178 		i1a.next();
179 		i2a.next();
180 
181 		i1a.remove();
182 		assertThrows(UnsupportedOperationException.class, i2a::remove);
183 
184 		i1a.forEachRemaining(x -> {});
185 		i2a.forEachRemaining(x -> {});
186 	}
187 }