001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.AbstractCollection;
021: import java.util.Arrays;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import junit.framework.TestCase;
025:
026: public class AbstractCollectionTest extends TestCase {
027:
028: /**
029: * @tests java.util.AbstractCollection#add(java.lang.Object)
030: */
031: public void test_addLjava_lang_Object() {
032: AbstractCollection<Object> ac = new AbstractCollection<Object>() {
033:
034: @Override
035: public Iterator<Object> iterator() {
036: fail("iterator should not get called");
037: return null;
038: }
039:
040: @Override
041: public int size() {
042: fail("size should not get called");
043: return 0;
044: }
045:
046: };
047: try {
048: ac.add(null);
049: } catch (UnsupportedOperationException e) {
050: }
051: }
052:
053: /**
054: * @tests java.util.AbstractCollection#addAll(java.util.Collection)
055: */
056: public void test_addAllLjava_util_Collection() {
057: final Collection<String> fixtures = Arrays
058: .asList("0", "1", "2");
059: AbstractCollection<String> ac = new AbstractCollection<String>() {
060:
061: @Override
062: public boolean add(String object) {
063: assertTrue(fixtures.contains(object));
064: return true;
065: }
066:
067: @Override
068: public Iterator<String> iterator() {
069: fail("iterator should not get called");
070: return null;
071: }
072:
073: @Override
074: public int size() {
075: fail("size should not get called");
076: return 0;
077: }
078:
079: };
080: assertTrue(ac.addAll(fixtures));
081: }
082:
083: /**
084: * @tests java.util.AbstractCollection#containsAll(java.util.Collection)
085: */
086: public void test_containsAllLjava_util_Collection() {
087: final Collection<String> fixtures = Arrays
088: .asList("0", "1", "2");
089: AbstractCollection<String> ac = new AbstractCollection<String>() {
090:
091: @Override
092: public boolean contains(Object object) {
093: assertTrue(fixtures.contains(object));
094: return true;
095: }
096:
097: @Override
098: public Iterator<String> iterator() {
099: fail("iterator should not get called");
100: return null;
101: }
102:
103: @Override
104: public int size() {
105: fail("size should not get called");
106: return 0;
107: }
108:
109: };
110: assertTrue(ac.containsAll(fixtures));
111: }
112:
113: /**
114: * @tests java.util.AbstractCollection#isEmpty()
115: */
116: public void test_isEmpty() {
117: final boolean[] sizeCalled = new boolean[1];
118: AbstractCollection<Object> ac = new AbstractCollection<Object>() {
119: @Override
120: public Iterator<Object> iterator() {
121: fail("iterator should not get called");
122: return null;
123: }
124:
125: @Override
126: public int size() {
127: sizeCalled[0] = true;
128: return 0;
129: }
130: };
131: assertTrue(ac.isEmpty());
132: assertTrue(sizeCalled[0]);
133: }
134:
135: /**
136: * @tests java.util.AbstractCollection#removeAll(java.util.Collection)
137: */
138: public void test_removeAllLjava_util_Collection() {
139: final String[] removed = new String[3];
140: AbstractCollection<String> ac = new AbstractCollection<String>() {
141:
142: @Override
143: public Iterator<String> iterator() {
144: return new Iterator<String>() {
145: String[] values = new String[] { "0", "1", "2" };
146: int index;
147:
148: public boolean hasNext() {
149: return index < values.length;
150: }
151:
152: public String next() {
153: return values[index++];
154: }
155:
156: public void remove() {
157: removed[index - 1] = values[index - 1];
158: }
159:
160: };
161: }
162:
163: @Override
164: public int size() {
165: fail("size should not get called");
166: return 0;
167: }
168:
169: };
170: assertTrue(ac.removeAll(Arrays.asList("0", "1", "2")));
171: for (String r : removed) {
172: if (!"0".equals(r) && !"1".equals(r) && !"2".equals(r)) {
173: fail("an unexpected element was removed");
174: }
175: }
176: }
177:
178: /**
179: * @tests java.util.AbstractCollection#retainAll(java.util.Collection)
180: */
181: public void test_retainAllLjava_util_Collection() {
182: final String[] removed = new String[1];
183: AbstractCollection<String> ac = new AbstractCollection<String>() {
184:
185: @Override
186: public Iterator<String> iterator() {
187: return new Iterator<String>() {
188: String[] values = new String[] { "0", "1", "2" };
189: int index;
190:
191: public boolean hasNext() {
192: return index < values.length;
193: }
194:
195: public String next() {
196: return values[index++];
197: }
198:
199: public void remove() {
200: removed[index - 1] = values[index - 1];
201: }
202:
203: };
204: }
205:
206: @Override
207: public int size() {
208: fail("size should not get called");
209: return 0;
210: }
211:
212: };
213: assertTrue(ac.retainAll(Arrays.asList("1", "2")));
214: assertEquals("0", removed[0]);
215: }
216:
217: /**
218: * @tests java.util.AbstractCollection#toArray()
219: */
220: public void test_toArray() {
221: AbstractCollection<String> ac = new AbstractCollection<String>() {
222: @Override
223: public Iterator<String> iterator() {
224: return new Iterator<String>() {
225: String[] values = new String[] { "0", "1", "2" };
226: int index;
227:
228: public boolean hasNext() {
229: return index < values.length;
230: }
231:
232: public String next() {
233: return values[index++];
234: }
235:
236: public void remove() {
237: fail("remove should not get called");
238: }
239:
240: };
241: }
242:
243: @Override
244: public int size() {
245: return 3;
246: }
247: };
248:
249: Object[] array = ac.toArray();
250: assertEquals(3, array.length);
251: for (Object o : array) {
252: if (!"0".equals(o) && !"1".equals(o) && !"2".equals(o)) {
253: fail("an unexpected element was removed");
254: }
255: }
256: }
257:
258: /**
259: * @tests java.util.AbstractCollection#toArray(java.lang.Object[])
260: */
261: public void test_toArray$Ljava_lang_Object() {
262: AbstractCollection<String> ac = new AbstractCollection<String>() {
263: @Override
264: public Iterator<String> iterator() {
265: return new Iterator<String>() {
266: String[] values = new String[] { "0", "1", "2" };
267: int index;
268:
269: public boolean hasNext() {
270: return index < values.length;
271: }
272:
273: public String next() {
274: return values[index++];
275: }
276:
277: public void remove() {
278: fail("remove should not get called");
279: }
280:
281: };
282: }
283:
284: @Override
285: public int size() {
286: return 3;
287: }
288: };
289: try {
290: ac.toArray(null);
291: fail("No expected NullPointerException");
292: } catch (NullPointerException e) {
293: // expected
294: }
295:
296: try {
297: ac.toArray(new StringBuffer[ac.size()]);
298: fail("No expected ArrayStoreException");
299: } catch (ArrayStoreException e) {
300: // expected
301: }
302:
303: String[] a = new String[3];
304: assertSame(a, ac.toArray(a));
305:
306: a = new String[0];
307: assertNotSame(a, ac.toArray(a));
308: a = ac.toArray(a);
309: assertEquals(3, a.length);
310:
311: CharSequence[] csa = new CharSequence[3];
312: ac.toArray(csa);
313: assertEquals(3, csa.length);
314: assertEquals("0", csa[0]);
315: assertEquals("1", csa[1]);
316: assertEquals("2", csa[2]);
317: }
318:
319: /**
320: * @tests java.util.AbstractCollection#toString()
321: */
322: public void test_toString() {
323: // see HARMONY-1522
324: // collection that returns null iterator(this is against the spec.)
325: AbstractCollection<?> c = new AbstractCollection<Object>() {
326: @Override
327: public int size() {
328: // return non-zero value to pass 'isEmpty' check
329: return 1;
330: }
331:
332: @Override
333: public Iterator<Object> iterator() {
334: // this violates the spec.
335: return null;
336: }
337: };
338:
339: try {
340: // AbstractCollection.toString() doesn't verify
341: // whether iterator() returns null value or not
342: c.toString();
343: fail("No expected NullPointerException");
344: } catch (NullPointerException e) {
345: }
346: }
347: }
|