Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 16bcbdfc81101d6fd28296ab43e633afc3551ca6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
 * Copyright (c) 2015 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0, which accompanies this distribution
 * and is available at https://www.eclipse.org/legal/epl-2.0/.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.NullList;
import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;
import junit.framework.TestCase;

@SuppressWarnings("nls")
public class NullListTests
	extends TestCase
{
	public NullListTests(String name) {
		super(name);
	}

	public void testAddObject() {
		List<String> list = NullList.<String>instance();
		assertFalse(list.add("foo"));
		assertTrue(list.isEmpty());
	}

	public void testAddIntObject() {
		List<String> list = NullList.<String>instance();
		list.add(0, "foo");
		assertTrue(list.isEmpty());
	}

	public void testAddAllCollection() {
		List<String> list = NullList.<String>instance();
		Collection<String> collection = new ArrayList<String>();
		collection.add("foo");
		collection.add("bar");
		assertFalse(list.addAll(collection));
		assertTrue(list.isEmpty());
	}

	public void testAddAllIntCollection() {
		List<String> list = NullList.<String>instance();
		Collection<String> collection = new ArrayList<String>();
		collection.add("foo");
		collection.add("bar");
		assertFalse(list.addAll(0, collection));
		assertTrue(list.isEmpty());
	}

	public void testClear() {
		List<String> list = NullList.<String>instance();
		list.clear();
		assertTrue(list.isEmpty());
	}

	public void testContainsObject() {
		List<String> list = NullList.<String>instance();
		Collection<String> collection = new ArrayList<String>();
		collection.add("foo");
		collection.add("bar");
		assertFalse(list.addAll(collection));
		assertFalse(list.contains("foo"));
		assertFalse(list.contains("bar"));
		assertFalse(list.contains("XXX"));
	}

	public void testContainsAllCollection() {
		List<String> list = NullList.<String>instance();
		Collection<String> collection = new ArrayList<String>();
		collection.add("foo");
		collection.add("bar");
		assertFalse(list.addAll(collection));
		assertFalse(list.containsAll(collection));
		collection.clear();
		assertTrue(list.containsAll(collection));
	}

	public void testGetInt() {
		List<String> list = NullList.<String>instance();
		boolean exCaught = false;
		try {
			list.get(0);
			fail();
		} catch (IndexOutOfBoundsException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testIndexOfObject() {
		List<String> list = NullList.<String>instance();
		assertEquals(-1, list.indexOf("foo"));
	}

	public void testIsEmpty() {
		List<String> list = NullList.<String>instance();
		assertTrue(list.isEmpty());
	}

	public void testIterator() {
		List<String> list = NullList.<String>instance();
		assertTrue(IteratorTools.isEmpty(list.iterator()));
	}

	public void testLastIndexOfObject() {
		List<String> list = NullList.<String>instance();
		assertEquals(-1, list.lastIndexOf("foo"));
	}

	public void testListIterator() {
		List<String> list = NullList.<String>instance();
		assertTrue(IteratorTools.isEmpty(list.listIterator()));
	}

	public void testListIteratorInt() {
		List<String> list = NullList.<String>instance();
		ListIterator<String> iterator = list.listIterator(0);
		assertTrue(IteratorTools.isEmpty(iterator));
		boolean exCaught = false;
		try {
			iterator = list.listIterator(1);
			fail("bogus list iterator: " + iterator);
		} catch (IndexOutOfBoundsException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testRemoveObject() {
		List<String> list = NullList.<String>instance();
		assertFalse(list.remove("foo"));
		assertTrue(list.isEmpty());
	}

	public void testRemoveInt() {
		List<String> list = NullList.<String>instance();
		boolean exCaught = false;
		try {
			String object = list.remove(0);
			fail("bogus element: " + object);
		} catch (IndexOutOfBoundsException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
		assertTrue(list.isEmpty());
	}

	public void testRemoveAllCollection() {
		List<String> list = NullList.<String>instance();
		Collection<String> collection = new ArrayList<String>();
		collection.add("foo");
		collection.add("bar");
		assertFalse(list.removeAll(collection));
		assertTrue(list.isEmpty());
	}

	public void testRetainAllCollection() {
		List<String> list = NullList.<String>instance();
		Collection<String> collection = new ArrayList<String>();
		collection.add("foo");
		collection.add("bar");
		assertFalse(list.retainAll(collection));
		assertTrue(list.isEmpty());
	}

	public void testSetIntObject() {
		List<String> list = NullList.<String>instance();
		boolean exCaught = false;
		try {
			String element = list.set(0, "foo");
			fail("bogus set: " + element);
		} catch (IndexOutOfBoundsException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testSize() {
		List<String> list = NullList.<String>instance();
		assertEquals(0, list.size());
	}

	public void testSubList() {
		List<String> list = NullList.<String>instance();
		List<String> subList = list.subList(0, 0);
		assertTrue(subList.isEmpty());
		boolean exCaught = false;
		try {
			subList = list.subList(0, 3);
			fail("bogus sub list: " + subList);
		} catch (IndexOutOfBoundsException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);

		exCaught = false;
		try {
			subList = list.subList(3, 0);
			fail("bogus sub list: " + subList);
		} catch (IndexOutOfBoundsException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);

		exCaught = false;
		try {
			subList = list.subList(3, 3);
			fail("bogus sub list: " + subList);
		} catch (IndexOutOfBoundsException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);

		subList = list.subList(0, 0);
		assertTrue(subList.isEmpty());
	}

	public void testToArray() {
		List<String> list = NullList.<String>instance();
		assertEquals(0, list.toArray().length);
	}

	public void testToArrayObjectArray() {
		List<String> list = NullList.<String>instance();
		assertEquals(0, list.toArray(ObjectTools.EMPTY_OBJECT_ARRAY).length);
	}

	public void testSerialization() throws Exception {
		List<String> list = NullList.<String>instance();
		assertSame(list, TestTools.serialize(list));
	}

	public void testToString() {
		List<String> list = NullList.<String>instance();
		assertEquals("[]", list.toString());
	}
}

Back to the top