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
18package org.apache.harmony.luni.tests.java.util;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import java.util.AbstractCollection;
26import java.util.Arrays;
27import java.util.Collection;
28import java.util.Iterator;
29import junit.framework.TestCase;
30
31@TestTargetClass(java.util.AbstractCollection.class)
32public class AbstractCollectionTest extends TestCase {
33
34    /**
35     * @tests java.util.AbstractCollection#add(java.lang.Object)
36     */
37    @TestTargetNew(
38        level = TestLevel.COMPLETE,
39        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
40        method = "add",
41        args = {java.lang.Object.class}
42    )
43    public void test_addLjava_lang_Object() {
44        AbstractCollection<Object> ac = new AbstractCollection<Object>() {
45
46            @Override
47            public Iterator<Object> iterator() {
48                fail("iterator should not get called");
49                return null;
50            }
51
52            @Override
53            public int size() {
54                fail("size should not get called");
55                return 0;
56            }
57
58        };
59        try {
60            ac.add(null);
61        } catch (UnsupportedOperationException e) {
62        }
63    }
64
65    /**
66     * @tests java.util.AbstractCollection#addAll(java.util.Collection)
67     */
68    @TestTargetNew(
69        level = TestLevel.COMPLETE,
70        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
71        method = "addAll",
72        args = {java.util.Collection.class}
73    )
74    public void test_addAllLjava_util_Collection() {
75        final Collection<String> fixtures = Arrays.asList("0", "1", "2");
76        AbstractCollection<String> ac = new AbstractCollection<String>() {
77
78            @Override
79            public boolean add(String object) {
80                assertTrue(fixtures.contains(object));
81                return true;
82            }
83
84            @Override
85            public Iterator<String> iterator() {
86                fail("iterator should not get called");
87                return null;
88            }
89
90            @Override
91            public int size() {
92                fail("size should not get called");
93                return 0;
94            }
95
96        };
97        assertTrue(ac.addAll(fixtures));
98    }
99
100    /**
101     * @tests java.util.AbstractCollection#containsAll(java.util.Collection)
102     */
103    @TestTargetNew(
104        level = TestLevel.COMPLETE,
105        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
106        method = "containsAll",
107        args = {java.util.Collection.class}
108    )
109    public void test_containsAllLjava_util_Collection() {
110        final Collection<String> fixtures = Arrays.asList("0", "1", "2");
111        AbstractCollection<String> ac = new AbstractCollection<String>() {
112
113            @Override
114            public boolean contains(Object object) {
115                assertTrue(fixtures.contains(object));
116                return true;
117            }
118
119            @Override
120            public Iterator<String> iterator() {
121                fail("iterator should not get called");
122                return null;
123            }
124
125            @Override
126            public int size() {
127                fail("size should not get called");
128                return 0;
129            }
130
131        };
132        assertTrue(ac.containsAll(fixtures));
133    }
134
135    /**
136     * @tests java.util.AbstractCollection#isEmpty()
137     */
138    @TestTargetNew(
139        level = TestLevel.COMPLETE,
140        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
141        method = "isEmpty",
142        args = {}
143    )
144    public void test_isEmpty() {
145        final boolean[] sizeCalled = new boolean[1];
146        AbstractCollection<Object> ac = new AbstractCollection<Object>(){
147            @Override
148            public Iterator<Object> iterator() {
149                fail("iterator should not get called");
150                return null;
151            }
152            @Override
153            public int size() {
154                sizeCalled[0] = true;
155                return 0;
156            }
157        };
158        assertTrue(ac.isEmpty());
159        assertTrue(sizeCalled[0]);
160    }
161
162    /**
163     * @tests java.util.AbstractCollection#removeAll(java.util.Collection)
164     */
165    @TestTargetNew(
166        level = TestLevel.COMPLETE,
167        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
168        method = "removeAll",
169        args = {java.util.Collection.class}
170    )
171    public void test_removeAllLjava_util_Collection() {
172        final String[] removed = new String[3];
173        AbstractCollection<String> ac = new AbstractCollection<String>() {
174
175            @Override
176            public Iterator<String> iterator() {
177                return new Iterator<String>() {
178                    String[] values = new String[] {"0", "1", "2"};
179                    int index;
180                    public boolean hasNext() {
181                        return index < values.length;
182                    }
183
184                    public String next() {
185                        return values[index++];
186                    }
187
188                    public void remove() {
189                        removed[index - 1] = values[index - 1];
190                    }
191
192                };
193            }
194
195            @Override
196            public int size() {
197                fail("size should not get called");
198                return 0;
199            }
200
201        };
202        assertTrue(ac.removeAll(Arrays.asList("0", "1", "2")));
203        for (String r : removed) {
204            if (!"0".equals(r) && !"1".equals(r) && !"2".equals(r)) {
205                fail("an unexpected element was removed");
206            }
207        }
208    }
209
210    /**
211     * @tests java.util.AbstractCollection#retainAll(java.util.Collection)
212     */
213    @TestTargetNew(
214        level = TestLevel.COMPLETE,
215        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
216        method = "retainAll",
217        args = {java.util.Collection.class}
218    )
219    public void test_retainAllLjava_util_Collection() {
220        final String[] removed = new String[1];
221        AbstractCollection<String> ac = new AbstractCollection<String>() {
222
223            @Override
224            public Iterator<String> iterator() {
225                return new Iterator<String>() {
226                    String[] values = new String[] {"0", "1", "2"};
227                    int index;
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                        removed[index - 1] = values[index - 1];
238                    }
239
240                };
241            }
242
243            @Override
244            public int size() {
245                fail("size should not get called");
246                return 0;
247            }
248
249        };
250        assertTrue(ac.retainAll(Arrays.asList("1", "2")));
251        assertEquals("0", removed[0]);
252    }
253
254    /**
255     * @tests java.util.AbstractCollection#toArray()
256     */
257    @TestTargetNew(
258        level = TestLevel.COMPLETE,
259        notes = "",
260        method = "toArray",
261        args = {}
262    )
263    public void test_toArray() {
264        AbstractCollection<String> ac = new AbstractCollection<String>() {
265            @Override
266            public Iterator<String> iterator() {
267                return new Iterator<String>() {
268                    String[] values = new String[] {"0", "1", "2"};
269                    int index;
270                    public boolean hasNext() {
271                        return index < values.length;
272                    }
273
274                    public String next() {
275                        return values[index++];
276                    }
277
278                    public void remove() {
279                        fail("remove should not get called");
280                    }
281
282                };
283            }
284
285            @Override
286            public int size() {
287                return 3;
288            }
289        };
290
291        Object[] array = ac.toArray();
292        assertEquals(3, array.length);
293        for (Object o : array) {
294            if (!"0".equals(o) && !"1".equals(o) && !"2".equals(o)) {
295                fail("an unexpected element was removed");
296            }
297        }
298    }
299
300    /**
301     * @tests java.util.AbstractCollection#toArray(java.lang.Object[])
302     */
303    @TestTargetNew(
304        level = TestLevel.COMPLETE,
305        notes = "",
306        method = "toArray",
307        args = {java.lang.Object[].class}
308    )
309    public void test_toArray$Ljava_lang_Object() {
310        AbstractCollection<String> ac = new AbstractCollection<String>() {
311            @Override
312            public Iterator<String> iterator() {
313                return new Iterator<String>() {
314                    String[] values = new String[] {"0", "1", "2"};
315                    int index;
316                    public boolean hasNext() {
317                        return index < values.length;
318                    }
319
320                    public String next() {
321                        return values[index++];
322                    }
323
324                    public void remove() {
325                        fail("remove should not get called");
326                    }
327
328                };
329            }
330            @Override
331            public int size() {
332                return 3;
333            }
334        };
335        try {
336            ac.toArray(null);
337            fail("No expected NullPointerException");
338        } catch (NullPointerException e) {
339            // expected
340        }
341
342        try {
343            ac.toArray(new StringBuffer[ac.size()]);
344            fail("No expected ArrayStoreException");
345        } catch (ArrayStoreException e) {
346            // expected
347        }
348
349        String[] a = new String[3];
350        assertSame(a, ac.toArray(a));
351
352        a = new String[0];
353        assertNotSame(a, ac.toArray(a));
354        a = ac.toArray(a);
355        assertEquals(3, a.length);
356
357        CharSequence[] csa = new CharSequence[3];
358        ac.toArray(csa);
359        assertEquals(3, csa.length);
360        assertEquals("0", csa[0]);
361        assertEquals("1", csa[1]);
362        assertEquals("2", csa[2]);
363    }
364
365    /**
366     * @tests java.util.AbstractCollection#toString()
367     */
368    @TestTargetNew(
369        level = TestLevel.COMPLETE,
370        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
371        method = "toString",
372        args = {}
373    )
374    public void test_toString() {
375        // see HARMONY-1522
376        // collection that returns null iterator(this is against the spec.)
377        AbstractCollection<?> c = new AbstractCollection<Object>() {
378            @Override
379            public int size() {
380                // return non-zero value to pass 'isEmpty' check
381                return 1;
382            }
383
384            @Override
385            public Iterator<Object> iterator() {
386                // this violates the spec.
387                return null;
388            }
389        };
390
391        try {
392            // AbstractCollection.toString() doesn't verify
393            // whether iterator() returns null value or not
394            c.toString();
395            fail("No expected NullPointerException");
396        } catch (NullPointerException e) {
397        }
398    }
399
400    @TestTargetNew(
401        level = TestLevel.COMPLETE,
402        notes = "",
403        method = "AbstractCollection",
404        args = {}
405    )
406    public void test_Constructor() {
407        AbstractCollection<?> ac = new AbstractCollection<Object>() {
408            @Override
409            public Iterator<Object> iterator() {
410                return null;
411            }
412
413            @Override
414            public int size() {
415                return 0;
416            }
417        };
418
419        assertNotNull(ac);
420    }
421
422    @TestTargetNew(
423        level = TestLevel.COMPLETE,
424        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
425        method = "clear",
426        args = {}
427    )
428    public void test_clear() {
429        AbstractCollection<?> ac = new AbstractCollection<Object>() {
430            @Override
431            public Iterator<Object> iterator() {
432                return new Iterator<Object>() {
433
434                    public boolean hasNext() {
435                        return false;
436                    }
437
438                    public Object next() {
439                        return null;
440                    }
441
442                    public void remove() {
443                    }
444                };
445            }
446
447            @Override
448            public int size() {
449                return 0;
450            }
451        };
452
453        ac.clear();
454    }
455
456    @TestTargetNew(
457        level = TestLevel.COMPLETE,
458        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
459        method = "contains",
460        args = {java.lang.Object.class}
461    )
462    public void test_containsLjava_lang_Object() {
463        AbstractCollection<?> ac = new AbstractCollection<Object>() {
464            @Override
465            public Iterator<Object> iterator() {
466                return new Iterator<Object>() {
467
468                    public boolean hasNext() {
469                        return false;
470                    }
471
472                    public Object next() {
473                        return null;
474                    }
475
476                    public void remove() {
477                    }
478                };
479            }
480
481            @Override
482            public int size() {
483                return 0;
484            }
485        };
486
487        assertFalse(ac.contains(this));
488    }
489
490    @TestTargetNew(
491        level = TestLevel.COMPLETE,
492        notes = "Class is abstract. Functionality tested in subclasses for example in java.util.Vector.",
493        method = "remove",
494        args = {java.lang.Object.class}
495    )
496    public void test_removeLjava_lang_Object() {
497        AbstractCollection<?> ac = new AbstractCollection<Object>() {
498            @Override
499            public Iterator<Object> iterator() {
500                return new Iterator<Object>() {
501
502                    public boolean hasNext() {
503                        return false;
504                    }
505
506                    public Object next() {
507                        return null;
508                    }
509
510                    public void remove() {
511                    }
512                };
513            }
514
515            @Override
516            public int size() {
517                return 0;
518            }
519        };
520
521        assertFalse(ac.remove(this));
522    }
523}
524